How can I have unique build numbers across branches with Jenkins & the Pipeline Multibranch Plugin
Asked Answered
A

4

17

We are using Jenkins Pipeline Multibranch Plugin with Blue Ocean.

Through my reading, I believe it is quite common to tie your project's build number to the Jenkins run, as this allows traceability from an installed application through to the CI system, then to the change in source control, and then onto the issue that prompted the change.

The problem is that for each branch, the run number begins at 0. For a project with multiple branches, it seems impossible to guarantee a unique build number.

Acceleration answered 16/1, 2018 at 13:14 Comment(2)
I'm not following how the build number enables this traceability from deployed artifact back to build that created it--could you provide a reference or explain a bit more?Extempore
Why would you ever want to release branches? If your version number depends on the build number of say the master branch everything would be traceable as you described.Ichthyornis
F
7

You can get the Git branch name from $GIT_BRANCH and add this to $BUILD_NUMBER to make an ID that's unique across branches (as long as your company doesn't do something like get themselves taken over by a large corporation that migrates you to another Jenkins server and resets all the build numbers: to protect against that, you might want to use $BUILD_URL).

Only snag is $GIT_BRANCH contains the / character, plus any characters you used when naming the branch, and these may or may not be permitted in all the places where you want an ID. ($BUILD_URL is also going to contain characters like : and /) If this is an issue, one workaround would be to delete unwanted characters with tr:

export MY_ID=$(echo $GIT_BRANCH-$BUILD_NUMBER | tr -dc [A-Za-z0-9-])

(-dc means delete the complement of these characters, so A-Z, a-z, 0-9 and - are the characters you want to keep.)

Finalist answered 31/3, 2020 at 14:4 Comment(0)
A
5

Maybe instead of a unique (global numeric) build number you might want to try a unique (global) build display name?

According to "pipeline syntax: global variables reference" currentBuild.displayName is a writable property. So you could e.g. add additional information to the build number (in order to make it globally unique) and use that string in subsequent artifact/application build steps (to incorporate that in the application's version output for your desired traceability), e.g. something like:

currentBuild.displayName = "${env.BRANCH_NAME}-${currentBuild.id}"

Using the build's schedule or start time formatted (currentBuild.timeInMillis) as a readable date, or using the SCM revision might be also useful, e.g. resulting in "20180119-091439-rev149923".

See also:

Ascensive answered 24/1, 2018 at 12:5 Comment(2)
I'm still very interested to hear more about how renaming builds creates traceability from an installed app to the build that generated it. -- Oh, docker images use the build name?Extempore
This won't work if you need sequential builds with Integer as build number and you work with multibranch pipeline (Fabric Beta Android builds for instance). The number of commits is also not the option. I don't know a clean solution to this yet and researching one as well.Suffrage
G
5

One way is to have a Job that is being called from all branches and using it's build number. That job can be just a normal pipeline job with a dummy Jenkinsfile like echo "hello". Then just call it like this

def job = build job: 'build number generator', quietPeriod: 0, parameters: [
        string(value: "${BRANCH_NAME}-${BUILD_NUMBER}", name: 'UID')  
]
def BNUMBER = job.getNumber().toString()
currentBuild.displayName = "build #"+BNUMBER
echo BNUMBER

Not sure if that UID parameter is needed but it forces all calls into "build number generator" job to be unique so Jenkins wouldn't optimize builds that happen at same time to use same "build number generator" job.

Groundsel answered 26/1, 2021 at 13:11 Comment(0)
B
-3

You can use an external service to manage a unique build number for your project. It helps to get unique build numbers across branches and across CI servers too. https://www.nextbuildnumber.net/

Bibliology answered 16/12, 2022 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.