Auto-increment release version Jenkins

我是研究僧i 提交于 2020-06-09 11:38:48

问题


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 release version I have to give a number (ex: "C:\Octopus\Octo.exe" create-release --project APP --version 4.8 --packageversion=4.8)

How can I make so that version number will be auto-incremented ? (ex: I have build the application and created in Octopus the release version number 4.8, the next time I build the application I want to create the release version number 4.9)

Thank you


回答1:


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('.')
}



回答2:


Jenkins provides some environment variables available to each build command; one of those is the build number, an increasing number.

If you can accommodate with holes in your version numbers, change your version to include the build number, like 4.8-142 where 142 is the build number from jenkins. This gives you increasing version numbers in the semver sense and still let you control the real version in a more functional way.

If you are not happy with the build number in your version, then you should have a post build script that updates the version number and stores that in a file somewhere. You could then inject that file content with either the EnvInject plugin or using a parametrized build.




回答3:


Use version Number plugin from Jenkins Plugins, It should give you an environment variable to use as virsion number see more in this URL




回答4:


If you are doing deployments from a git repo, you are probably already creating tags for each release (or you should at least, so that you can more easily track what was deployed). If that's the case, you can just derive the next release version from those tags, without having to store the version anywhere else (just make sure Jenkins is fetching tags in advanced clone behaviours).

Then, you can calculate the next version with something like this:

def nextVersionFromGit(scope) {
    def latestVersion = sh returnStdout: true, script: 'git describe --tags "$(git rev-list --tags=*.*.* --max-count=1 2> /dev/null)" 2> /dev/null || echo 0.0.0'
    def (major, minor, patch) = latestVersion.tokenize('.').collect { it.toInteger() }
    def nextVersion
    switch (scope) {
        case 'major':
            nextVersion = "${major + 1}.0.0"
            break
        case 'minor':
            nextVersion = "${major}.${minor + 1}.0"
            break
        case 'patch':
            nextVersion = "${major}.${minor}.${patch + 1}"
            break
    }
    nextVersion
}

This method uses Semantic Versioning format, but it can be easily adapted if you want versions with only 2 numbers.



来源:https://stackoverflow.com/questions/37701362/auto-increment-release-version-jenkins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!