19

I am migrating jenkins-workflow job to new template based workflow job. Because the build number is used as part of the version of build artifacts the workflow produces I have to start build number of the new workflow with a number greater than the old workflow. Unfortunately 'Next Build Number' plugin does not work with workflow pipeline.

Anybody knows a good way do this?

Vlad
  • 9,180
  • 5
  • 48
  • 67

6 Answers6

19

Or, you could add a snippet like this to your Pipeline/Workflow job DSL script (aka Jenkinsfile):

offset = 5
currentBuild.displayName = "#" + (currentBuild.number + offset)
MarkHu
  • 1,694
  • 16
  • 29
15

Unfortunately the methods listed above didn't work for me when using folders. I resorted to the following in the Jenkins script console:

job = Jenkins.getInstance().getItemByFullName("BSKAzureConversion-Jobs/master", Job.class)
job.nextBuildNumber = 92
job.save()
Ed Meagher
  • 151
  • 1
  • 3
  • 1
    If using `job.saveNextBuildNumber()` for line 3, this worked for my multi-branch pipeline. – David V Jan 03 '17 at 18:02
  • 1
    To find your job name from the multi-branch pipeline, run this: `def jobs = Jenkins.getInstance().getAllItems(); println(jobs)` And then, as @DavidV described above: `def job = Jenkins.instance.getItemByFullName("YourTeam/your_project/features%2Fbranchname"); job.nextBuildNumber = 301; job.saveNextBuildNumber();` – kivagant Mar 15 '17 at 13:29
  • this alternative worked for me. thanks I added a println to check after a change def job = Jenkins.getInstance().getItemByFullName("package/build-package", Job.class) println(job.nextBuildNumber) job.nextBuildNumber = 254 println(job.nextBuildNumber) job.saveNextBuildNumber() – byOnti Oct 10 '19 at 19:42
13

Try running below script in Jenkins Script Console.. Change "workFlow" to your Jobname

def job = Jenkins.instance.getItem("workFlow")
job.nextBuildNumber = 10
job.saveNextBuildNumber()
Jayan
  • 18,003
  • 15
  • 89
  • 143
  • 3
    This code snippet does not write the new build number to the `nextBuildNumber` file. Due to this, if you (gracefully) restart Jenkins and then trigger a build, it ends up using whatever build number was present in the `nextBuildNumber` file (and not the one specified in the above snippet). Using `job.saveNextBuildNumber()` instead of `job.save()` addresses this issue. – Amit Jun 27 '16 at 12:59
2

A few notes:

  • Jenkins Jobs and Folders are not serializable. Need to use @NonCPS
  • Disable Groovy Sandbox, as globals like Jenkins.instance are not accessible otherwise
  • Jenkins.instance.getItem() won't work well with folders. Use Jenkins.instance.getItemByFullName against the env.JOB_NAME
  • Setting job.nextBuildNumber will take effect subsequent build, so go ahead and kick off the next one
  • Make sure you return out of your pipeline so the initiating build doesn't continue with bad build number

Code:

@NonCPS
def updateBuildNumber(build_number) {
  def job = Jenkins.instance.getItemByFullName(env.JOB_NAME, Job.class)
  job.nextBuildNumber = build_number
  job.saveNextBuildNumber()
  build env.JOB_NAME
  return true
}
deepelement
  • 2,457
  • 1
  • 25
  • 25
1

I used @Jayan's answer and modified it to support getting a branch from that workflow because getItem("workflow/job/mybranch") did not work

def job = Jenkins.instance.getItem("workFlow").getItemByBranchName("mybranch")
job.nextBuildNumber = 10
job.saveNextBuildNumber()
0

I also found another way of doing this by using 'jenkins-cli'. It works for locked down installations where access to script console is not available (Cloudbees Enterprise Jenkins)

java -jar jenkins-cli.jar   -s https://instanceURL/ \
          set-next-build-number workflow-name  33
Vlad
  • 9,180
  • 5
  • 48
  • 67