Saturday, March 31, 2018

How to Clean or Reset build numbers in Jenkins job ?

When you were working with some POC or R&D, you may have to do things a number of times until you come to final conclusion. In my recent POC, I was working with Jenkins build jobs for Azure container registry with Azure Managed Container services aka AKS.

During this whole activity I have created 70 plus builds and during this process each build was creating one separate tag for docker image on ACR. When this whole POC gets completed, I thought that it will be really good idea to flush/clear out build history and start building from scratch.

Situation after completing POC-


Below are the step by step instructions for the same-

  • Login to Jenkins server, click on " Manage Jenkins" --> "Script Console". This will open below dialog box.
  • Once you click on "Script Console", then copy and paste below script there and run.
Note - Make sure to change the "Job Name"

def jobName = "Enter Job Name"
def job = Jenkins.instance.getItem(jobName)
job.getBuilds().each { it.delete() }
job.nextBuildNumber = 1
job.save()
  • Check again for build history. You'll notice that everything got flashed.

  • Try to build job again and notice the change in Build number.




  • One more thing would like to highlight. Let's support you have stable version at build number 90 and then you did some poc. During your poc you created some more builds which are just trash and you want to remove those build and would like to start new stable build with build_number 91. Please use below snippet for this -

def jobName = "JOB NAME"
def job = Jenkins.instance.getItem(jobName)
job.nextBuildNumber = 91
job.save()


Hope this will help !!

Integrate Jenkins with Azure Key Vault

Jenkins has been one of the most used CI/CD tools. For every tool which we are using in our daily life, it becomes really challenges when ...