Open In App

How To Reset Build Number In Jenkins?

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Jenkins is an open-source automation server that is mainly used in building, testing, and deploying software projects. In some scenarios, you must have found yourself in a situation where you want to reset the build number in Jenkins. Whether it is because of organizational reasons, or you want to start new, or any other specific reason. You can reset the build number in Jenkins by following the mentioned steps in the article and also we will going to discuss it in detail.

What are Build Numbers in Jenkins?

A build number is a unique identifier assigned to every instance of software build for a continuous integration or continuous delivery (CI/CD) system like Jenkins. It acts as a sequential label for a particular build of a software project that helps in tracking, referencing, and organizing different versions of the codebase. The build number is normally an integer value that starts from 1 and gets incremented with each new build. This process helps in differentiating between different builds and also helps in understanding the order in which builds are executed.

In Jenkins, the build numbers that are associated with each build of a job can be used for various purposes like as identifying build-in logs, monitoring changes over time, and also referencing specific builds in the CI/CD pipeline.

Prerequisites

  1. Jenkins Installed: Before proceeding further our first step is to install Jenkins so make sure you already have installed Jenkins and it is running on your server. You can also download their latest version by visiting their website.
  2. Access to Jenkins Dashboard: Make sure you have all the necessary credentials and access rights to log into the Jenkins dashboard.
  3. Knowledge of Jenkins File System: You must have all the knowledge of the Jenkins file system, like the location of job directories and key files like “build.xml” and “nextBuildNumber.”
  4. Backup Strategy: Before making any changes, make sure you have a proper backup to ensure that you can recover it when it is required also it is very important to prevent data loss in case if system fails.

How to reset the build number in Jenkins?

To reset the build number in Jenkins you can follow this step-by-step guide. Here is the detailed description of every step to reset the build number in Jenkins:

Step 1: Log in to Jenkins Dashboard

You can simply log in to your Jenkins dashboard by using your user ID and password.

login-to-jenkin

Login Jenkin Dashboard

Step 2: Navigate to Manage Jenkins

After successful login you will be redirected to the Jenkins dashboard, on the left hand side you can see option of manage Jenkin, click on it.

click-on-manage-jenkins-(1)

Click on Manage Jenkins

Step 3: Script Console

Now click on the script console after that run this command:

Jenkins.instance.getItemByFullName("test-job").updateNextBuildNumber(42) 

click-on-script-console

Click on Script Console

command-1

Run the Command

Step 4: Verify Build Number

We can verify this build number by again going to dashboard, then click on the test job and then run the job to verify it.

click-on-test-job

Click on Test Job

After that, click on the Build now

click-on-build-now

click on build now

You can verify your build number

verify-your-build

Verify your build number

Step 5: Reset Build number in Jenkins

Now to reset the build number in Jenkins, again go the script console and then run this command:

item = Jenkins.instance.getItemByFullName("test-job")
Item.builds.each() { build -> build.delete() }
item.updateNextBuildNumber(1)
item.getNextBuildNumber()
command-2

Run the command

After running command you will see result 1 means you have reset build number successfully

result

Step 6: Verify Reset of Build Number

After running the command successfully, you can navigate to dashboard section in this you can clearly see that there no built which clearly indicates that you have successfully reset the build number in Jenkins.

reset-build-successfully

Reset Build Successfully

Conclusion

It must be concluded here that you can successfully reset build number in Jenkin by simply following the mentioned steps of the article. You must remember all the prerequisite before resetting the build number in Jenkins.

How to reset build number in jenkins? – FAQs

Why it is necessary to disable the job before resetting the build number?

when you’re making changes disabling the job prevents new builds from getting triggered. Therefore it is very important to disable them before resetting the build number.

Is it necessary to delete the whole build directory to reset build number?

No, you only have to delete the directory corresponding to the latest build number of the “builds” directory.

How can I set the next build number in the “nextBuildNumber” file?

Firstly, open the “nextBuildNumber” file with the text editor and set its content to the desired build number, make sure it should be higher than the deleted build number.

Can I reset build number even while Jenkins is running?

Yes, you can reset the build number even when Jenkins is running, but it is strongly recommended to disable the job temporarily to avoid any build conflicts.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads