0

I am new to Jenkins, and stuck at replacing branch name in Jenkinsfile from Jenkins.

I want to write a common Jenkinsfile, and in that I want to pick branch name from Jenkins. Below is the snippet from my Jenkinsfile

checkout([$class: 'GitSCM', branches: [[name: '*/master']],

Whatever the branch name I select from Jenkins, it should replace the same in Jenkinsfile and do the build.

I have gone through the answers parametrized build

Somewhat not able to do the same. Can anyone refer me to the doc/link which I can go through and implement the same?

My Changes:

checkout([$class: 'GitSCM', branches: [[name: 'params.Branch_Select']],

enter image description here

GIT PARAMETER

BRANCH SPECIFIER

Jenkinsfile

Error

Aman
  • 357
  • 2
  • 5
  • 17

2 Answers2

2

I have fixed the above question. UNCHECK the box.

Lightbox

Aman
  • 357
  • 2
  • 5
  • 17
0

In your Jenkinsfile, add a parameters section which will accept BRANCH_NAME as parameter

parameters {
        string(defaultValue: "master", description: 'enter the branch name to use', name: 'BRANCH_NAME')
    }

and use that variable in your checkout step as follows:

checkout([$class: 'GitSCM', branches: [[name: "*/${BRANCH_NAME}"]]])

Note: You have to run the build once to see the 'Build with parameters' option.

Sample pipeline with just a checkout stage:

pipeline{
    agent any
    parameters {
        string(defaultValue: "develop", description: 'enter the branch name to use', name: 'BRANCH_NAME')
    }
    stages{
        stage('Checkout'){
            steps{
                checkout([$class: 'GitSCM', branches: [[name: "*/${BRANCH_NAME}"]], gitTool: 'jgit', userRemoteConfigs: [[url: "https://github.com/githubtraining/hellogitworld.git"]]])
                //checkout([$class: 'GitSCM', branches: [[name: "*/${BRANCH_NAME}"]],url:'https://github.com/brianmyers/hello'])
            }
        }
    }
}
Sujji
  • 66
  • 6