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
def prUrl = new URL("
https://api.github.com/repos/my-organisation/my-repository/pulls/${env.CHANGE_ID}
")
// 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 <[email protected]> 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.
>
> On 1 October 2016 at 01:17, 'Logan Glickfield' via Jenkins Users <
> [email protected]> wrote:
>
>> 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
>>
>> --
>> 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 [email protected].
>> To view this discussion on the web visit https://groups.google.com/d/ms
>> gid/jenkinsci-users/3135bfee-2501-4814-8555-af0c79551112%
>> 40googlegroups.com
>> <https://groups.google.com/d/msgid/jenkinsci-users/3135bfee-2501-4814-8555-af0c79551112%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/CAGi586PX-SnMxUz0eAfUaLeqF4tiowHZHEE4W_jkXNc8w0qVLA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to