Set Jenkins Build Number to Version of App

Let's set Jenkins' build number for a specific job to the version of the app we're building. Historical logs will show (the version of the app) over #100 (the number of builds done + 1).

Set Jenkins Build Number to Version of App

Here's the scenario: you have a Jenkins build job for your app - let's call it TravelWeather - and you're building version 1.2.1.x of it.  Unfortunately, you wouldn't realize what version you are building until it has been built (or if you looked at the console output) as the build number that Jenkins displays is a count of how many builds you have completed in the past plus one.  Instead, we can tell Jenkins what version the build is creating and set that as the build number.  This makes it easier to go back in time and view specific Jenkins build jobs based on the app's version.  Let's find out how!

For this example, we'll be using Jenkins Pipelines.  Here's a portion of the Jenkinsfile for our app's build job.  As you can see, we're generating/setting the version number and then taking that value and setting the Jenkins build number.

stage('Build') {
            steps {
                script {
                   VERSION_NUMBER = VersionNumber(versionNumberString: '1.2.1.${BUILDS_ALL_TIME}')
                   currentBuild.displayName = "${VERSION_NUMBER}"
                   sh "ant -Dversion=${VERSION_NUMBER} build"

Please keep in mind that you'd probably want to generate your version number in an earlier stage (for example, I call the early stage Preparation) and that this snippet is just for example purposes.

Running this snippet results in the following screenshot.  As you can see, the latest build was "renamed" to the version of the app.  The previous builds were using Jenkins' default "build_number++ method."

And that's it!  Easily rename and organize your builds based on the version of the code you're building.

Here are some additional resources:

Share Tweet Send