Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-43418

Cannot get branch name for Github Organizational Multibranch PR builds

      When using the GitHub Organizational Folder plugin, we have the option to Build origin PRs (unmerged head). I would love to use this feature, however in the Pipeline job that gets created and run for our PR's, I'm unable to find any way to get the branch name, which is absolutely necessary for various parts of our build process. 

      The ideal methods...

      env.BRANCH_NAME // returns PR-1234
      scm.branches[0] // returns PR-1234

      The often suggested hack, post-checkout...

      sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD') // returns HEAD
      

      ...returns "HEAD" and I'm not entirely sure why.

      Am I completely missing something here? This just seems like it would be painfully necessary for some people. Using `env.BRANCH_NAME` does work for the ordinary branch/non PR jobs. I found several older JIRA issues related to that, but cannot find anything related to the PR builds specifically. 

          [JENKINS-43418] Cannot get branch name for Github Organizational Multibranch PR builds

          Jason Riddle added a comment -

          Any update on this? Just ran up against this problem as well.

          Jason Riddle added a comment - Any update on this? Just ran up against this problem as well.

          andrew morton added a comment -

          Worth noting that scm.branches[0] isn't available in the sandbox. 

          git rev-parse isn't going to work because checkout scm checks out the commit rather than the branch. You could try something like git branch --contains which gives back output that would need a little parsing:

          * (HEAD detached at 65fc5ca3bb)
            jenkins-test

          andrew morton added a comment - Worth noting that scm.branches [0]  isn't available in the sandbox.  git rev-parse isn't going to work because checkout scm  checks out the commit rather than the branch. You could try something like git branch --contains which gives back output that would need a little parsing: * (HEAD detached at 65fc5ca3bb)   jenkins-test

          Stephen Connolly added a comment - https://github.com/jenkinsci/branch-api-plugin/blame/baeae5db935b06f6e202ab345236450b388725b9/src/main/java/jenkins/branch/BranchNameContributor.java#L69  exposes it in CHANGE_BRANCH will be released with JENKINS-43507

          Fixed, but depending on other plugin releases before fix can be released

          Stephen Connolly added a comment - Fixed, but depending on other plugin releases before fix can be released

          stephenconnolly you, sir, are a gentleman and a scholar. Also, a saint. 

          James Hardwick added a comment - stephenconnolly  you, sir, are a gentleman and a scholar. Also, a saint. 

            stephenconnolly Stephen Connolly
            hardwickj James Hardwick
            Votes:
            2 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: