Skip to content

Instantly share code, notes, and snippets.

@dianjuar
Created January 30, 2019 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dianjuar/8df3873cd9db49b7fcd86e74b57dd848 to your computer and use it in GitHub Desktop.
Save dianjuar/8df3873cd9db49b7fcd86e74b57dd848 to your computer and use it in GitHub Desktop.
A set of environment variables related to pull request to use in CircleCI. These are for GitHub provider

How to use it?

  1. Put the .sh file anywhere in the repo
  2. Inside the job that you want use the variables, in the steps section, add another step like
steps:
  # Create extra env Variables
  - run: cat ./.circleci/scripts/extra-variables.sh >> $BASH_ENV
  - run: echo "base branch -> $BC_PR_BASE_BRANCH"

With that you are able to use it in any following step

# Pull Request ID
export BC_PR_ID="${BC_PR_ID:-${CI_PULL_REQUEST##*/}}";
REPO=$CI_PULL_REQUEST;
REPO=${REPO##https://github.com/};
REPO=${REPO%%/pull/$BC_PR_ID};
# Repo Slug
export BC_REPO_SLUG=$REPO;
# Install dependencies
apk update && apk add jq;
REPOS_VALUES=($(curl -H "Authorization: token $GITHUB_TOKEN" -sSL https://api.github.com/repos/$BC_REPO_SLUG/pulls/$BC_PR_ID | jq -r -c ".head.repo.full_name, .head.repo.owner.login, .base.ref, .head.ref"));
# Pull request base branch (usually master)
export BC_PR_BASE_BRANCH=${REPOS_VALUES[2]};
# Pull request branch (branch to be merged into the base branch)
export BC_PR_BRANCH=${REPOS_VALUES[3]};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment