Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`env.BRANCH_NAME` becomes `PR-1`

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?

like image 761
codesmith Avatar asked Jan 29 '26 07:01

codesmith


1 Answers

For multibranch pipeline, you can get the branch name from any of below env variable -

  1. If multibranch job is run for any branch -
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).
  1. If multibranch job is run for any PR -
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.

like image 168
Parvez Kazi Avatar answered Feb 01 '26 03:02

Parvez Kazi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!