Skip to content

Instantly share code, notes, and snippets.

@benbenmushi
Last active May 28, 2019 08:21
Show Gist options
  • Save benbenmushi/6ba6b7f2ff82fd76e1219b57b8898909 to your computer and use it in GitHub Desktop.
Save benbenmushi/6ba6b7f2ff82fd76e1219b57b8898909 to your computer and use it in GitHub Desktop.
Jenkins: Automatically increment job's next build number
// This script role is to increment the next job's build number automatically if some parameter is set
//
import jenkins.model.Jenkins
import hudson.model.*
def build = Thread.currentThread().executable
def parameterList = build.getAction(ParametersAction.class).getAllParameters()
def size = parameterList.size()
def incrementBuildNumber = null;
for (i = 0; i < size; i++)
{
if (parameterList[i].name == "IncrementBuildNumber")
{
incrementBuildNumber = parameterList[i].value
}
}
if (incrementBuildNumber != null && incrementBuildNumber.trim() != "" && incrementBuildNumber.trim() == "Yes")
{
def job_data = Jenkins.instance.getItemByFullName(build.project.getName())
last_job_num = job_data.getLastBuild().getNumber()
println 'Set next build number to: ' + (last_job_num + 3)
job_data.updateNextBuildNumber(last_job_num + 3)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment