Changing Jenkins build number

Jenkins

Jenkins Problem Overview


Is there a way to change the build number that is sent via email after a job completes? The problem is that are product builds are NOT being done by Jenkins, so we want to be able to get the build number(ie. from a text file) and update the build number in Jenkins to match it. I have tried to set the build number:

set BUILD_NUMBER=45

But the email is still showing the build number that Jenkins originally set.

Jenkins Solutions


Solution 1 - Jenkins

If you have access to the script console (Manage Jenkins -> Script Console), then you can do this following:

Jenkins.instance.getItemByFullName("YourJobName").updateNextBuildNumber(45)

Solution 2 - Jenkins

can be done with the plugin: https://wiki.jenkins-ci.org/display/JENKINS/Next+Build+Number+Plugin

more info: http://www.alexlea.me/2010/10/howto-set-hudson-next-build-number.html

if you don't like the plugin:

> If you want to change build number via nextBuildNumber file you should > "Reload Configuration from Disk" from "Manage Jenkins" page.

Solution 3 - Jenkins

Under the job workspace folder, like:

C:\Program Files (x86)\Jenkins\jobs\job_name

there is a file named nextBuildNumber.

Setting the build number in the file and reloading the configuration from disk (Manage Jenkins menu) will force the next build you start to have the value from the file as BUILD_NUMBER.

Solution 4 - Jenkins

If you have branch name including Forward Slash (using git flow for example), you will need to replace the Forward Slash with its Unicode character %2F within the branch name.

Here is an example for the pipeline My-Pipeline-Name and the branch release/my-release-branch-name

Jenkins.instance.getItemByFullName("My-Pipeline-Name/release%2Fmy-release-branch-name").updateNextBuildNumber(BUILD_NUMBER)

I was able to find out about this by running the following command which will list the different jobs (branches) for your pipeline

Jenkins.instance.getItem("My-Pipeline-Name").getAllJobs()

Hope it helps.

Solution 5 - Jenkins

Perhaps a combination of these plugins may come in handy:

Solution 6 - Jenkins

You can change build number by updating file ${JENKINS_HOME}/jobs/job_name/nextBuildNumber on Jenkins server.

You can also install plugin Next Build Number plugin to change build number using CLI or UI

Solution 7 - Jenkins

For multibranch pipeline projects, do this in the script console:

def project = Jenkins.instance.getItemByFullName("YourMultibranchPipelineProjectName")    
project.getAllJobs().each{ item ->   
    
	if(item.name == 'jobName'){ // master, develop, feature/......
      
      item.updateNextBuildNumber(#Number);
      item.saveNextBuildNumber();
      
      println('new build: ' + item.getNextBuildNumber())
    }
}

Solution 8 - Jenkins

By using environmental variables:

$BUILD_NUMBER =4

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionerman8View Question on Stackoverflow
Solution 1 - Jenkinsl8niteView Answer on Stackoverflow
Solution 2 - JenkinsmighqView Answer on Stackoverflow
Solution 3 - JenkinsvezenkovView Answer on Stackoverflow
Solution 4 - JenkinsDaWyzView Answer on Stackoverflow
Solution 5 - JenkinsMorficView Answer on Stackoverflow
Solution 6 - JenkinsPradip sakhavalaView Answer on Stackoverflow
Solution 7 - JenkinsXavierHdzView Answer on Stackoverflow
Solution 8 - JenkinsksrView Answer on Stackoverflow