We use Jenkins pipeline & Github Multibranch.
I worked on a feature branch called feature/my1stfeature.
The Jenkins job returned the proper branch name:
println(env.BRANCH_NAME) returned feature/my1stfeature.
However, as soon as I created my first pull-request in Github
println(env.BRANCH_NAME) returns PR-01.
I would of course like to have the name of the feature branch.
How to solve this?
For multibranch pipeline, you can get the branch name from any of below env variable -
BRANCH_NAME
For a multibranch project, this will be set to the name of the branch being built,
for example in case you wish to deploy to production from master but not from feature branches; if corresponding to some kind of change request, the name is generally arbitrary (refer to CHANGE_ID and CHANGE_TARGET).
CHANGE_BRANCH
For a multibranch project corresponding to some kind of change request,
this will be set to the name of the actual head on the source control system which may or may not be different from BRANCH_NAME.
For example in GitHub or Bitbucket this would have the name of the origin branch whereas BRANCH_NAME would be something like PR-24.
CHANGE_TARGET
For a multibranch project corresponding to some kind of change request, this will be set to the target or base branch to which the change could be merged, if supported; else unset.
You can open ${YOUR_JENKINS_HOST}/env-vars.html page on your Jenkins master server to get a list of all environment variables listed on an HTML page.
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