How to set next build number in Multibranch pipeline for a particular branch
Asked Answered
B

3

7

I am trying to set the next build number for our release branch programmatically but I'm facing an issue.

Below are the two ways I tried it:

def job = Jenkins.instance.getItem("master")
job.nextBuildNumber = env.BUILD_NUMBER + 1
job.saveNextBuildNumber()

I tried using CLI command also:

java -jar ${env.HOME} jenkins-cli.jar -s XX-JENKINS-SERVER" set-next-build-number 'Pipeline/master' 44

But no luck.

Please guide me how to set up next build number for multibranch pipeline.

Botanize answered 31/10, 2016 at 21:13 Comment(0)
L
9

You absolutely can do this, and I just did to test it. If you go into the actual page for the specific branch of your pipeline job you want to change, note the "Full project name:" near the top, below the title.

Then, follow the instructions from this other StackOverflow thread:

Jenkins.instance.getItemByFullName("My-Build-Pipeline/feature%2FABC-9-improve-build").updateNextBuildNumber(47)

In the above example, my "full project name" was:

My-Build-Pipeline/feature%2FABC-9-improve-build

I tried to use the "Next Build Number" plugin also mentioned in that thread, but it didn't work for my multibranch pipeline job.

Lunate answered 10/11, 2016 at 22:13 Comment(0)
L
3

For the multibranch job you need to get to the base job for the branch you want to modify because each are separate.

def jobName = "job-name"
def nextnumber = 32


def wfjob = Jenkins.instance.getItemByFullName(jobName)
def job = wfjob.getItem("master")
print "Was: " + job.nextBuildNumber + "\n"
job.nextBuildNumber = nextnumber
print "Now: " + job.nextBuildNumber
job.save()
wfjob.save()
Laudable answered 21/6, 2019 at 17:53 Comment(0)
B
-6

You cannot set the next build number. It is an internal use only thing, used to identify the differences between runs of the job. Every time you run the job the build number increments by one. There is no need (and I don't encourage trying to) set the next build number.

Consider changing the description instead using job.setDisplayName to reflect what's going on with the release candidate, or perhaps baking it into the branch itself release-candidate-branch-34.

Bedspring answered 1/11, 2016 at 4:11 Comment(1)
Consider a Jenkins migration that was using the build number as an artifact suffix. Your new Jenkins environment would need to have this set so that you don't (fail to) overwrite existing, released artifacts.Cristobalcristobalite

© 2022 - 2024 — McMap. All rights reserved.