I've create a jenkins pipeline and it is pulling the pipeline script from scm.
I set the branch specifier to 'all
', so it builds on any change to any branch.
How do I access the branch name causing this build from the Jenkinsfile?
Everything I have tried echos out null except
sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()
which is always master
.
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...
If you want to be able to dynamically give a Git branch to use in a Jenkins build then you'll need to do a couple of things. Then, in your Pipeline configuration, under Branches to build, add your parameter name inside the Branch Specifier box, surrounded by ${} . Jenkins will expand your variable when the job runs.
In Git, branches are commonly used in order to have a development separated from your main workflow. In software engineering teams, it is quite common to have a specific workflow implemented. You may choose for example to have one branch per major release or to have a branch in order to quickfix an issue.
Use multibranch pipeline job type, not the plain pipeline job type. The multibranch pipeline jobs do posess the environment variable env.BRANCH_NAME
which describes the branch.
In my script..
stage('Build') { node { echo 'Pulling...' + env.BRANCH_NAME checkout scm } }
Yields...
Pulling...master
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With