1. Overview
Jenkins is one of the most popular open-source automation servers. It’s been widely adopted for building, testing, and deploying software automatically. Like any other automation server, Jenkins assigns a unique build number to each job. By default, the build number starts with one and increments with each subsequent job execution.
These unique build numbers are useful in identifying the particular job execution. However, in some cases, we might want to set the build number back to 1 or some other higher number. The good thing is we can do this effortlessly in Jenkins.
In this short tutorial, we’ll cover different ways to change the build number of the Jenkins job. So, let’s get started.
2. Resetting the Build Number by Overwriting the Job History
In this section, we’ll discuss methods that overwrite the history of the existing job. We must be very careful while performing these steps on the production server.
2.1. Using the Next Build Number Plug-in
One of the best things about Jenkins is that it provides plug-ins for commonly performed tasks, and resetting the build numbers isn’t an exception.
The Next Build Number plugin is handy for setting the next build number. Let’s see how to use this plug-in.
To begin, first, navigate to the Dashboard -> Manage Jenkins -> Plugins and install the Next Build Number plugin. The successful installation of this plug-in shows the option to set the build number in the left pane:
To reset the build number, click on the Set Next Build Number option, provide the next build number, and build the job.
As the name suggests, this plug-in sets the next build number. So, the provided number must be larger than the value for the most recent build. However, to go back to a previously used number we must first delete the older builds.
2.2. By Editing the nextBuildNumber File
The Jenkins plug-in is the easiest and recommended method for changing the build number. However, we can use a few other methods if this plugin isn’t installed. One such method is to edit the nextBuildNumber file.
The nextBuildNumber is a text file in the ${JENKINS_HOME}/jobs/
To change the build number, edit the nextBuildNumber file, reload the configuration by navigating to Dashboard -> Manage Jenkins -> Reload Configuration from Disk, and finally build the job.
It’s important to note that to use the previous build numbers, we must delete older builds.
2.3. Using the Script Console
Similarly, we can change the build number using the Jenkins script console. It allows us to run the Groovy script within the Jenkins controller at runtime. Let’s understand this with a simple example.
To change the build number, navigate to https://
jobName = "<job-name>"
item = Jenkins.instance.getItemByFullName(jobName)
item.updateNextBuildNumber(<number>)
In this example, we’re using the updateNextBuildNumber() method to programmatically update the next build number.
A key point to remember is that one must update the value of the jobName variable and pass the appropriate integer value to the updateNextBuildNumber() method.
The above-mentioned script doesn’t remove the older build; hence, the next build number value must be greater than the latest build. However, we can use the delete() method to remove the old build.
So, to remove all old builds and reset the build number back to 1, we can execute the following script:
jobName = "<job-name>"
item = Jenkins.instance.getItemByFullName(jobName)
// Remove all previous builds
item.builds.each() { build ->
build.delete()
}
item.updateNextBuildNumber(1)
2.4. Using the Jenkins CLI
In the previous example, we used the script console to reset the build number. However, that isn’t the most efficient method, as we have to execute the script manually.
We can overcome this limitation using the Jenkins CLI, which allows us to execute the same script automatically. So, let’s see this in action.
First, let’s create a reset_build_number.groovy file with the following contents:
import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
jobName = "<job-name>"
item = Jenkins.instance.getItemByFullName(jobName)
// Remove all previous builds
item.builds.each() { build ->
build.delete()
}
item.updateNextBuildNumber(1)
Now, let’s execute the Groovy script using the Jenkins CLI:
$ java -jar jenkins-cli.jar -s https://<jenkins-controller-ip>/ -auth <user>:<password> groovy = < reset_build_number.groovy
Beware that this script removes all older builds, so we must be very careful while running it on a production server.
3. Resetting the Build Number by Copying and Renaming the Job
So far, we have discussed methods that reset the build number by overwriting the job history. However, such methods aren’t always recommended.
It’s always better to keep a backup of the existing job and its history. In such a case, we first rename the existing job and then create a new job from the existing job.
The best thing about this approach is that it allows us to reset the build number of the job without overwriting the job history. However, one of the downsides of this approach is that it consumes some additional disk space.
4. Conclusion
In this article, we discussed how to reset the build number of the Jenkins job.
First, we discussed resetting the build number using the Next Build Number Plug-in.
Next, we updated the nextBuildNumber file to reset the build number.
Then, we discussed how to achieve the same using the Groovy script in an interactive and non-interactive way.
Finally, we discussed resetting the build number by copying and renaming the job.