Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 241
swati jain Avatar asked Oct 31 '16 21:10

swati jain


People also ask

How do I select a branch in Jenkins pipeline?

Head over to your Jenkins instance and create a new item. Enter a name for the job, and select the “Multibranch Pipeline” option at the end of the screen. Then, click on the OK button. In the next screen, go to the “Branch sources” tab, click on the “Add source” button, and choose “Git” from the dropdown menu.

How do you add parameters to the Multibranch pipeline?

If you want to add new parameters you have to do it using properties in Jenkinsfile - goto <JENKINS URL>/pipeline-syntax and generate code for the properties step.

Which one enables you to implement different Jenkinsfiles for different branches of the same?

The Multibranch Pipeline project type enables you to implement different Jenkinsfiles for different branches of the same project. In a Multibranch Pipeline project, Jenkins automatically discovers, manages and executes Pipelines for branches which contain a Jenkinsfile in source control.


1 Answers

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.

like image 76
Dan Fego Avatar answered Oct 25 '22 03:10

Dan Fego