I'm working on a Jenkins multibranch project and
BRANCH_NAME
and GIT_BRANCH
is set to the name of the branch (master
) and CHANGE_BRANCH
does not existBRANCH_NAME=master
GIT_BRANCH=master
CHANGE_BRANCH not exist
CHANGE_BRANCH
is set to the name of the branch (feature
) and BRANCH_NAME
and GIT_BRANCH
is set to the change request name (PR-01
).BRANCH_NAME=PR-01
GIT_BRANCH=PR-01
CHANGE_BRANCH=feature
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).
GIT_BRANCH The remote branch name, if any.
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.
In my declarative pipeline i always need the branch name regardless if a change request or not is built.
Currently i solve this with parameter expansion in bash:
sh 'echo branch name: ${CHANGE_BRANCH:-${BRANCH_NAME}}'
Is there a more Jenkins-oriented approach to solving this?
When building pull requests, a variable named CHANGE_ID
is set (to the PR number). You can use that variable to select between the two other variables:
def always_branch = env.CHANGE_ID ? env.CHANGE_BRANCH : env.BRANCH_NAME