21

I'm using Multibranch Pipeline Job in Jenkins.

How do I find the branch name from which the Pull Request is raised in GitHub?

I'm using /github-webhook/ & also tried with /ghprbhook/ and tried with the following environment variables: GIT_LOCAL_BRANCH, GIT_BRANCH, ghprbSourceBranch, but I didn't get any result.

If there are any suggestions, I would love to try them.

StephenKing
  • 36,187
  • 11
  • 83
  • 112
sunbogaa
  • 405
  • 2
  • 6
  • 12

5 Answers5

32

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

CHANGE_TARGET gives the target name of the PR merge

Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171
jayas
  • 421
  • 4
  • 4
  • 1
    Interesting, while this is not mentioned in the [multibranch pipeline documentation](https://jenkins.io/doc/book/pipeline/multibranch/#additional-environment-variables), the code in [branch-api](https://github.com/jenkinsci/branch-api-plugin/blob/c4d394415cf25b6890855a08360119313f1330d2/src/main/resources/jenkins/branch/BranchNameContributor/buildEnv.jelly) plugin mentions those. – StephenKing Mar 16 '19 at 12:29
  • What about non-PR builds? I'd like to have a single Jenkinsfile for my builds. Do I then have to detect if CHANGE_BRANCH exists, and then assume it isn't a PR build? – trusktr Mar 28 '19 at 00:11
  • 1
    @trusktr Yes, if CHANGE_BRANCH exists, it means it's a PR build. Otherwise CHANGE_BRANCH doesn't exist. – Jonathan Rioux Jun 18 '21 at 15:22
4

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 scm
def 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.

mkobit
  • 43,979
  • 12
  • 156
  • 150
3

The environment variable CHANGE_BRANCH should give you the source branch name and CHANGE_TARGET the target branch name. For PR from forks, the CHANGE_FORK gives you the repository name of the fork. For PR from origin, CHANGE_FORK is not set.

To see the description of the environment variables, navigate to a branch job then Pipeline Syntax > Global Variable Reference > env. It should display the description of those variables.

0

According to the documentation, the pull request ID is exposed as CHANGE_ID variable.

Btw. environment variables can be accessed in pipeline's groovy code via env, such as env.CHANGE_ID.

StephenKing
  • 36,187
  • 11
  • 83
  • 112
0

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 scm
    def branchName = scmVars.GIT_BRANCH
    
  2. Print ENV variables

    sh("printenv")
    
bp2010
  • 2,342
  • 17
  • 34
  • 8
    Thanks for your reply. If I raise a PR and use this command, we are getting PR number as the branch name. – sunbogaa Dec 13 '17 at 10:56
  • 1
    that is strange. Maybe you can try printing all env vars and git vars to see if the branch name is stored in another variable? – bp2010 Dec 13 '17 at 11:45
  • 3
    This doesn't work. Jenkins make a branch like `PR-XXX` where `XXX` is the PR number, and that is what shows up in `env.BRANCH_NAME`. Wish it put the PR's original branch name in there! – trusktr Mar 27 '19 at 23:24