Wednesday 20 October 2010

Setting the Hudson/Jenkins next build number programatically

Programatically setting the next build number in Hudson/Jenkins is a useful thing to do if you have a build pipeline. After a bit of research it seems there are 3 main approaches that could be useful in different situations:

  1. Set the nextBuildNumber.txt in your Hudson job's directory
  2. Use environment variables for build number passthrough (see @ruby_gem's post here)
  3. Use the nextBuildNumber Hudson plugin and HTTP POST the build number to it

In this post I'll be looking at option 3 - using the nextBuildNumber Hudson plugin.

Using the nextBuildNumber Hudson plugin

Firstly install the nextBuildNumber plugin. You can now set the next build number for a job manually from the plugin link that appear on the left.
To use this programatically you simulate the manual for submission with a web client. I tested it with FireFox ReST client.
To set the next build number to 999 simply formulate an HTTP POST to your Hudson URL:

e.g. http://myhudsonserver:8080/job/myhudsonjobname/nextbuildnumber/submit
With the form data
nextBuildNumber=999
And the HTTP Content-Type request header
Content-Type: application/x-www-form-urlencoded

If your Hudson is secured you'll need to authenticate your web client too.

That's it! Finally it's worth noting that build numbers in Hudson can only go up!

2 comments :

Gilles said...

Great post, exactly what I needed!
The following can save a few minutes to anyone wanting to use a shell command to bump the build # of another jenkins job:

bn=`expr $BUILD_NUMBER + 1`
curl --user "user:pass" --data "nextBuildNumber=$bn" --header "Content-Type: application/x-www-form-urlencoded" http://myhudsonserver:8080/job/myhudsonjobname/nextbuildnumber/submit

Anonymous said...

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