194

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.

9 Answers 9

379

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

Jenkins.instance.getItemByFullName("YourJobName").updateNextBuildNumber(45)
11
  • 8
    I can verify this works perfectly to solve the problem. I manually copied build history between two jenkins servers, and on the new server nextBuildNumber was wrong. Running this allowed me to set a correct new nextBuildNumber, no jenkins config reload required. Commented May 24, 2016 at 16:59
  • 11
    If you are using the GitHub plugin, "YourJobName" is "organization/repository/branchname".
    – Ruud
    Commented Sep 20, 2016 at 15:50
  • 35
    Works. The value can be verified without building using: Jenkins.instance.getItemByFullName("YourJobName").getNextBuildNumber() Commented Mar 15, 2017 at 14:51
  • 14
    I can confirm this is also working for MultiBranch-Pipelines. The "JobName" is listed on the top of the branch page: Full project name: PipelineName/BranchName so it would be Jenkins.instance.getItemByFullName("YourPipelineName/YourBranchName").updateNextBuildNumber(45)
    – Markus
    Commented Aug 1, 2017 at 14:54
  • 1
    @Quantic meta.stackoverflow.com/questions/356435/… Commented Sep 12, 2017 at 13:04
40

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.

1
  • 3
    No need to use plugin. Commented Oct 7, 2014 at 14:19
34

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.

4
  • 12
    Did you reload the configuration from disk (Manage Jenkins menu) after changing the nextBuildNumber file?
    – vezenkov
    Commented Sep 17, 2015 at 18:59
  • Nope! I thought it happened automatically.
    – hfossli
    Commented Sep 18, 2015 at 8:13
  • 1
    This worked perfectly for me. You have to reboot the server before it takes affect.
    – Hawkeye64
    Commented Nov 20, 2018 at 17:46
  • 1
    Having to reboot the server is a pain if you have many users running pipeline. Better avoid this kind of solution
    – PixelPint
    Commented Feb 25, 2020 at 12:14
10

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.

5

Perhaps a combination of these plugins may come in handy:

1
  • Thanks a lot - I was able to use one of the plugins.
    – erman8
    Commented Nov 1, 2013 at 21:55
5

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

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

1
  • 3
    Note that changing the build number in the nextBuildNumber file on its own is not enough, executing the "Reload Configuration from Disk" in "Manage Jenkins" menu is necessary as well.
    – falstaff
    Commented May 19, 2020 at 20:40
4

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())
    }
}
3

Follow the steps: Jenkins Console > Manage Jenkins > Script Console, then write the script as:

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

1
-6

By using environmental variables:

$BUILD_NUMBER =4
1
  • 2
    Please, don't shout. Thank you.
    – OliCoder
    Commented Apr 9, 2019 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.