187

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.

Frederic Close
  • 9,389
  • 6
  • 56
  • 67
erman8
  • 2,141
  • 3
  • 19
  • 17

9 Answers9

367

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

Jenkins.instance.getItemByFullName("YourJobName").updateNextBuildNumber(45)
l8nite
  • 5,042
  • 1
  • 20
  • 23
  • 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. – Phil Hollenback May 24 '16 at 16:59
  • 9
    If you are using the GitHub plugin, `"YourJobName"` is `"organization/repository/branchname"`. – Ruud Sep 20 '16 at 15:50
  • 34
    Works. The value can be verified without building using: `Jenkins.instance.getItemByFullName("YourJobName").getNextBuildNumber()` – Daniel Alder Mar 15 '17 at 14:51
  • 10
    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 Aug 01 '17 at 14:54
  • @DanielAlder's comment has hidden characters in the statement and Jenkins cannot parse it correctly. Firefox only shows one of the characters that you can delete but there are more hidden ones that cause the statement not to work. Here is a copyable string: `Jenkins.instance.getItemByFullName("JobName").getNextBuildNumber()` – Quantic Sep 11 '17 at 21:19
  • @Quantic I'm surprised, you are right. But your string contains the same characters. Output of hexdump: "B u i � 200 214 � 200 213 l d N u". I cannot explain it but the characters seem to be inserted by stackoverflow! – Daniel Alder Sep 12 '17 at 12:49
  • 1
    @Quantic https://meta.stackoverflow.com/questions/356435/hidden-characters-in-comment – Daniel Alder Sep 12 '17 at 13:04
  • I tried this, I tried next build number plugin, I tried changing `nextBuildNumber` file content - nothing works. The latter just got overwritten by the next number, and others just have no effect. `getNnextBuildNumber()` returns not what I set, but the next incremental value. Not sure where it gets this from. – Evgen Feb 28 '20 at 00:37
  • This is working. You have to enter the full name of the project – Vedha Peri Sep 21 '20 at 21:43
  • And if your branch name has a forward slash (e.g. feature/123), you will need to replace the Forward Slash with its Unicode character %2F (e.g. feature%2F123). – James Selvakumar Nov 08 '21 at 08:35
  • On a multibranch pipeline you must include the branch name at the end, so `pipeline/job/branch`. – vesperto May 23 '23 at 17:02
39

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.

mighq
  • 1,004
  • 1
  • 13
  • 14
33

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.

hfossli
  • 22,616
  • 10
  • 116
  • 130
vezenkov
  • 4,009
  • 1
  • 26
  • 27
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.

DaWyz
  • 178
  • 2
  • 5
5

Perhaps a combination of these plugins may come in handy:

Morfic
  • 15,178
  • 3
  • 51
  • 61
4

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

tijko
  • 7,599
  • 11
  • 44
  • 64
  • 2
    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 May 19 '20 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())
    }
}
XavierHdz
  • 71
  • 3
2

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

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

  • Does this add any value to the top voted answer https://stackoverflow.com/a/36901733/1796579 ? – Henry Aug 04 '23 at 04:28
-5

By using environmental variables:

$BUILD_NUMBER =4
David Balažic
  • 1,319
  • 1
  • 23
  • 50
ksr
  • 601
  • 5
  • 4