Access real Branch name for Pull Request/Fork Build


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

I was sure that $CIRCLE_BRANCH from 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

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.



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

When build is part of PR, there is $CI_PULL_REQUEST variable which looks something like:

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${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

Here is a one liner:


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.