Provide env variable for branch name targeted by pull request
For many reasons, it is necessary to determine the branch target pull request that triggered the current workflow. This has been a frequently requested feature, and has cropped up on the discuss.circleci site in numinous requests and formats.
Conditioning the selected image tag used by the docker executor via base branch name from PR, given that this is otherwise intractable given the otherwise lack of propagating such conditional logic to a jobs executed prior to the job being able to even run.
Please see the reference here:
Currently, as of CircleCi 2.0, no environment variables provide a basic means to aquise the branch name targeted by a pull request, be it from a forked repository or internal from the local repo.
The closest contenders include the `CIRCLECI_BRANCH` that for pull requests, instead returns the name of branch intended to be merged from with the new commits, not the base branch for which the commits would like to be merged into. Additionally, `CIRCLE_PULL_REQUEST` only contains the URL of the associated pull request, which has no-or-limited utility for the case example described above, given the branch name would first need to be parsed from the URL before useable.
If we take a comparative look at Travis, provided is the `TRAVIS_BRANCH` env variable that returns the the branch targeted by the pull request if triggered by such a PR.
- for push builds, or builds not triggered by a pull request, this is the name of the branch.
- for builds triggered by a pull request this is the name of the branch targeted by the pull request.
for builds triggered by a tag, this is the same as the name of the tag (
Given the current pattern of available variables from CircleCI, including:
||Integer||The number of the associated GitHub or Bitbucket pull request. Only available on forked PRs.|
||String||The name of the GitHub or Bitbucket repository where the pull request was created. Only available on forked PRs.|
||String||The GitHub or Bitbucket username of the user who created the pull request. Only available on forked PRs.|
I'd suggest adding `CIRCLE_PR_BRANCH` that provides such a feature as follows:
||String||The target branch name in the GitHub or Bitbucket repository where the pull request was created. Only available on
Alternatively, it might be nice to follow the logical switching of `TRAVIS_BRANCH` to allow for in branch commits to use the same conditioning logic as described in the case example link without compilation then the job trigger does not arise from a PR.