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.

Rafael Marques
  • 1,501
  • 15
  • 23
swati jain
  • 79
  • 1
  • 2

3 Answers3

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.

Community
  • 1
  • 1
Dan Fego
  • 13,644
  • 6
  • 48
  • 59
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()
-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.

Everspace
  • 382
  • 1
  • 3
  • 13
  • 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. – mike Oct 08 '20 at 20:02