GitHub Branch name from which the Pull Request is raised GitHub Branch name from which the Pull Request is raised jenkins jenkins

GitHub Branch name from which the Pull Request is raised


CHANGE_BRANCH gives the correct name of the source branch of the PR.

CHANGE_TARGET gives the target name of the PR merge


There are a few different parameters and it can be difficult to find the correct one depending on the context.

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).

This can either be the branch name (like in branch builds) or some other identifier (like the pull request Id). The documentation is clear that it can be either, but it is surprising behavior if you haven't read the full documentation.

The other answer by MZ2010 shows a way that will probably work as the checkout. It may depend on plugin versions and is probably affected by the same issue as above.

def scmVars = checkout scmdef branchName = scmVars.GIT_BRANCH

The way that that you can get it is if you use the GitHub Branch Source Plugin which supports the CHANGE_BRANCH environment variable. This was added in JENKINS-43418 and you should be able to reference it if you are using env.CHANGE_BRANCH. It may not be available in Multibranch jobs, though.


For multibranch pipelines, use:

env.BRANCH_NAME

for accessing the branch name.

Edit: If you need to debug env variables or git variables you can try:

  1. access GIT variables

    def scmVars = checkout scmdef branchName = scmVars.GIT_BRANCH
  2. Print ENV variables

    sh("printenv")