Access real Branch name for Pull Request/Fork Build


#1

For Pull Request builds, how to get real branch name of user’s fork?

I was sure that $CIRCLE_BRANCH from https://circleci.com/docs/environment-variables/ should work, but for PR build it shows string, unrelated to real branch name.

# expected real branch name like: `testing-circleci`, not `pull/1234`.
ubuntu@box407:~/st2$ echo $CIRCLE_BRANCH
pull/2259

In my example I need to pass this data in webhook for further processing.

If $CIRCLE_BRANCH has different logic, then $CIRCLE_PR_BRANCH makes sense.

Thanks!


#2

Found “nasty hack” workaround for this (GitHub case):

When build is part of PR, there is $CI_PULL_REQUEST variable which looks something like: https://github.com/username/repo/pull/220

To find all needle info for Pull Request/Fork inside CircleCI build:
1.) Parse GitHub username, repo, number from: $CI_PULL_REQUEST
2.) Fetch GitHub API https://api.github.com/repos/${PR_ORG}/${PR_REPO}/pulls/${PR_NUMBER}

GitHub API returns just everything they know.

It’s pretty sad case that CIrcleCI don’t list more variables that are really important during the build and I need to additionally fetch GitHub API.


Need a way to get dest branch on pull request buulds
#3

Here is a one liner:

CIRCLE_PR_BRANCH = `curl -s https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/pulls/${CIRCLE_PR_NUMBER} | jq -r '.head.ref'`

Notice for private repos would require a GitHub token! With that it becomes quite complicated…

It would be best (like other CIs - Travis, Shippable) to simply provide CIRCLE_PR_BRANCH environment variable.


#4