Reputation: 1595
I am using Jenkins and Gradle to build my java project.
Every time I build my project, I get a new build number on the Jenkins screen.
The following is my Jenkins build info:
Success > Console Output #96 03-Jan-2014 15:35:08
Success > Console Output #95 03-Jan-2014 15:27:29
Failed > Console Output #94 03-Jan-2014 15:26:16
Failed > Console Output #93 03-Jan-2014 15:25:01
Failed > Console Output #92 03-Jan-2014 15:23:50
Success > Console Output #91 03-Jan-2014 12:42:32
Success > Console Output #90 03-Jan-2014 12:02:45
I want to reset the Jenkins build number like:
Success > Console Output #1 03-Jan-2014 12:02:45
How can I reset the build number in Jenkins?
Upvotes: 102
Views: 112630
Reputation: 141
Jenkins script(Test on Jenkins 2.387.3
):
def jobName = "<YOUR_PIPELINE_NAME>"
def job = Jenkins.instance.getItem(jobName)
job.getBuilds().each { it.delete() }
job.updateNextBuildNumber(1)
Delete all build history and reset build number.
Upvotes: 0
Reputation: 21
Here is variation of @antweiss answer for multi-branch pipeline
items = Jenkins.instance.getItemByFullName("your-job-name-here").getItems()
//THIS WILL REMOVE ALL BUILD HISTORY
items.collectMany { it.builds }.each { build ->
build.delete()
}
items.each {
it.updateNextBuildNumber(1)
}
Upvotes: 0
Reputation: 4284
So I tried the above solution and getting the following error.,
groovy.lang.MissingPropertyException: No such property: builds for class: org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject.
So I tried this,
item = Jenkins.get().getItem("Job Name")
jobs = item.getAllJobs()
jobs.each() { item ->
builds = item.getBuilds()
builds.each() { b ->
b.delete()
}
item.updateNextBuildNumber(1)
}
and it worked!!
Upvotes: 1
Reputation: 447
Use Purge Job History plugin (Jenkins >= 2.176.1)
https://plugins.jenkins.io/purge-job-history/
Upvotes: 2
Reputation: 429
I found an easy way to do this.
Jenkins use previous build to determine the next build number, if build number you input is lower than previous build number, Jenkins will automatically increase your build number to higher than previous build. So here we just
Upvotes: -1
Reputation: 2088
If you want set the next build number, there is plugin "NextBuildNumber" for that. But this will not work in your case because the build number you need, which is 1, is lesser than your current build number.
Here need to wipe out all the previous builds first. You can do this by running this simple script Go to -> Manage Jenkins -> Script console
// change this variable to match the name of the job whose builds you want to delete
def jobName = "Your Job Name"
def job = Jenkins.instance.getItem(jobName)
job.getBuilds().each { it.delete() }
Now you can set next build number to 1 and run the build. It will start with 1. :) Its that simple.
Update - Jenkins now has a Purge Job History plugin to get this done in easiest way. Checkout the page for more details - https://wiki.jenkins.io/display/JENKINS/Purge+Job+History+Plugin
Upvotes: 24
Reputation: 1
To reset build numbers of all jobs:
Jenkins.instance.getAllItems(AbstractProject.class).each {
item = Jenkins.instance.getItemByFullName(it.fullName)
//THIS WILL REMOVE ALL BUILD HISTORY
item.builds.each() { build ->
build.delete()
}
item.updateNextBuildNumber(1)
}
Upvotes: -1
Reputation: 8547
As an extention of @antweiss's excellent answer, we can actually go further ...
There's no need to delete the full Build History, if you don't want to, you can simply roll back time, to a prior point:
resetNumberTarget = 14
item = Jenkins.instance.getItemByFullName("Project Name [from project Dashboard]")
//println(item)
item.builds.each() { build ->
//println(build)
//println(build.number)
if(build.number >= resetNumberTarget)
{
//println("About to Delete '" + build + "'")
build.delete()
}
}
item.updateNextBuildNumber(resetNumberTarget)
If you want a dummy run, to check what it's going to do, without actually doing it, simply comment out the build.delete()
and item.updateNextBuildNumber(resetNumberTarget)
lines and uncomment the various print commands.
item
is a FreeStyleProject (or possibly, just any type of Abstract Project?)build
appears to be a Run, thus exposing a number property and inheritting a delete() method from JobUpvotes: 6
Reputation: 10500
Expanding on the accepted answer, here's how to do it for all projects at once:
Jenkins.instance.allItems.each() {
item -> item.builds.each() {
build -> build.delete()
}
item.updateNextBuildNumber(1)
}
Upvotes: 6
Reputation: 2026
You can use either nexBuildNumber
plug-in or simply modify nexBuildNumber
file to reset build number.
Following are the steps that you need to perform:
.jenkins/Jobs/<YourJobName>/build/
, take backup of this folder(if you need for future use) and delete build folder.Note: Once you clean up old builds, you lose build histories and they are no longer available on the Jenkins dashboard.
nextBuildNumber
file in yourjob directory.Upvotes: 1
Reputation: 820
To more generally reset your build number to N
(where N
is not necessarily 1):
buildNumber >= N
.Program Files (x86)/Jenkins/jobs/yourjob/nextBuildNumber
. Set the number it contains to N
.Manage Jenkins -> Reload Configuration from Disk
.Upvotes: 6
Reputation: 2879
Can be easier done from groovy script console . Go to http://your-jenkins-server/script In script window enter:
item = Jenkins.instance.getItemByFullName("your-job-name-here")
//THIS WILL REMOVE ALL BUILD HISTORY
item.builds.each() { build ->
build.delete()
}
item.updateNextBuildNumber(1)
Upvotes: 184
Reputation: 893
Upvotes: 38