Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass project parameter as branch name to build in Jenkins

I am wondering is there any way we can pass the value of project parameter as the Git branch to build from.

Below is what I am trying to do:

  1. Create a Jenkins pipeline project with a build parameter:

enter image description here

  1. Then I tried to using the project parameter to pass it on (Branches to build)

enter image description here

However the branch_name variable is not resolved from the project parameter.

I could do manually change branch name for every build, but it is far from ideal.

Appreciate for any idea?

like image 632
Joey Trang Avatar asked Oct 12 '17 04:10

Joey Trang


People also ask

How do you set a branch to build in Jenkins?

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 I pass a git branch as parameter in Jenkinsfile?

To use the branch in a build I typically name the parameter "branch" and then under "Source Code Management" (with Git selected) use "${branch}" (without the quotes) for the "Branches to build" "Branch Specifier (blank for 'any')" field. Hope that helps.

How do you define parameters for a build in Jenkins?

Defining Build Parameters Any Jenkins job or pipeline can be parameterized. All we need to do is check the box on the General settings tab, “This project is parameterized”: Then we click the Add Parameter button.


2 Answers

This issue has been reported several times. This works if you disable the "Lightweight Checkout". Apparently the code path is very different if you are using the lightweight checkout, and that has not been resolved, apparently.

See JENKINS-28447

like image 108
Rob Hales Avatar answered Oct 04 '22 13:10

Rob Hales


You can use git parameter plugin ...

then in paramerterized select Git Paramerter ..Give any Name e.g. BRANCH select parameter type as "Branch" and put default value for branch. Then in Branches to build inthat put ${BRANCH}. Build job with parameter as branch name...

like image 31
Varsha Prakash Patil Avatar answered Oct 04 '22 14:10

Varsha Prakash Patil