Auto-increment release version Jenkins

后端 未结 4 1497
心在旅途
心在旅途 2021-02-03 10:40

I have an application that builds in Jenkins and that I want to deploy to Octopus. When I am doing this I have to create a release version that is send to Octopus. For this rele

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-03 11:29

    You can use a job property to store the version, and then update it on each run with the following script (executed by "Execute system groovy script" build step):

    import jenkins.model.Jenkins
    import hudson.model.*
    
    def jenkins = Jenkins.getInstance()
    def jobName = "yourJobName"
    String versionType = "minor"
    def job = jenkins.getItem(jobName)
    
    //get the current version parameter and update its default value
    paramsDef = job.getProperty(ParametersDefinitionProperty.class)
    if (paramsDef) {
       paramsDef.parameterDefinitions.each{
           if("version".equals(it.name)){
               println "Current version is ${it.defaultValue}"
               it.defaultValue = getUpdatedVersion(versionType, it.defaultValue)
               println "Next version is ${it.defaultValue}"
           }
       }
    }
    
    //determine the next version by the required type 
    //and incrementing the current version
    
    def getUpdatedVersion(String versionType, String currentVersion){
    
        def split = currentVersion.split('\\.')
        switch (versionType){
            case "minor.minor":
                split[2]=++Integer.parseInt(split[2])
                break
            case "minor":
                split[1]=++Integer.parseInt(split[1])
                break;
            case "major":
               split[0]=++Integer.parseInt(split[0])
               break;
        }
        return split.join('.')
    }
    

提交回复
热议问题