Feature branch BRANCH_NAME for PR builds (Github Organization)

63 views
Skip to first unread message

Logan Glickfield

unread,
Sep 30, 2016, 11:17:53 AM9/30/16
to Jenkins Users
Hi there!

Is there any way to get the original feature branch name within a pipeline that was triggered by a PR build rather than the "PR-#" style BRANCH_NAME?

I've tried things like:
scm checkout
def branch = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()

But that doesn't work because its actually on a detached head commit with no branch.


Our Github Organization is currently configured to use:
- Build origin branches
- Build origin PRs (unmerged head)

We also specify "Include branches" so that we only build the "master PR-*" branches.

It seems like I could use "Build origin branches also filed as PRs" and get the correct BRANCH_NAME for free. But if I understand correctly, "Include branches" would become useless and I would no longer be able to configure it to build only the "master" branch and pull request branches.

Anyone have any ideas how I might be able to make this work?

Thanks!

-Logan

Sean Flanigan

unread,
Oct 20, 2016, 10:47:15 PM10/20/16
to jenkins...@googlegroups.com
Sorry I can't help, but that's an excellent question. I was about to ask it myself (although I build _merged_ heads for PRs).

There don't seem to be any suitable environment variables, so the only option I can think of is to interrogate the Multibranch Plugin, which may or may not have the original branch name in it somewhere. I haven't worked which class to use yet.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/3135bfee-2501-4814-8555-af0c79551112%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sean Flanigan

unread,
Oct 21, 2016, 7:00:48 AM10/21/16
to jenkins...@googlegroups.com
I found a workaround for this, but it does require approving a few methods for the Groovy sandbox, and it costs an extra call to the GitHub API:

// TODO use GitHub credentials to avoid rate limiting
// See https://developer.github.com/v3/pulls/#get-a-single-pull-request
// use head.label for owner:branch
// use head.ref for the branch name only
def sourceBranchLabel = new groovy.json.JsonSlurper().parseText(prUrl.text).head.label


On 21 October 2016 at 12:46, Sean Flanigan <sean.f...@gmail.com> wrote:
Sorry I can't help, but that's an excellent question. I was about to ask it myself (although I build _merged_ heads for PRs).

There don't seem to be any suitable environment variables, so the only option I can think of is to interrogate the Multibranch Plugin, which may or may not have the original branch name in it somewhere. I haven't worked which class to use yet.
Reply all
Reply to author
Forward
0 new messages