21

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?

Joey Trang
  • 1,105
  • 2
  • 23
  • 44

3 Answers3

28

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

Rob Hales
  • 5,123
  • 1
  • 21
  • 33
  • 3
    I disabled the Lighweitht Checkout (LC), But apparently I am not sure about consequence of unchecking this option. – Joey Trang Oct 12 '17 at 06:04
  • I tried to name parameter as "Branch Name" and passing it to "Branches to build" as ${'Branch Name'}. this time it is not working. Seemingly the parameter name follows the Java Naming convention. However, In the Jenkins file we can refer to this parameterized project by using following syntax: `${env.'Branch Name'}` @Rob Hales – Joey Trang Oct 12 '17 at 06:15
  • The lightweight checkout tries to go to the SCM to get your Jenkinsfile without doing a full checkout first. When you do a pipeline without the LWCO, you will notice 2 checkouts in the console output. The first is to a different workspace, and just to get the Jenkinsfile. So, assuming you can afford a couple extra seconds and a little more disk space, this shouldn't affect you. – Rob Hales Oct 12 '17 at 23:47
  • Is there a way to do this if you set the variables as parameters in the Jenkinsfile? e.g. `parameters {string(name: 'BRANCH_SPECIFIER', defaultValue: 'develop', description: 'This is the branch to build')}` – Mark Han Dec 18 '19 at 17:23
  • In this case, which jenkinsfile will bee used for pipeline? the one existing in first checkout or 2nd ? – Manoj K Sardana Oct 28 '21 at 05:58
1

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...

0

Disable the "Lightweight Checkout" is the best option!

Here is another: Use a Pipeline Script to load the Jenkinsfile:

node('Jenkins node') {
    
    properties([
        parameters([
            string(defaultValue: 'develop', description: 'Branch des ***-Repositorys', name: 'BRANCH')
        ])
    ])
    //gitlabBranch Parameter is set if gitlab send a webhook, so you can start the job manually or by webhook
    env.BRANCH = env.gitlabBranch ?: env.BRANCH
    
    git url: '<url>', credentialsId: '***', branch: env.BRANCH
    load 'Jenkinsfile'
}
dreSte
  • 1
  • 2