Auto-increment release version Jenkins

后端 未结 4 1493
心在旅途
心在旅途 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:35

    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.

提交回复
热议问题