Context
I am using CircleCI to deploy review apps for a React app. Essentially, when a PR is opened, CircleCI does these steps:
- Install Dependencies (
npm install
) - Running tests (
npm t
) - Build & Deploy
During the “Build & Deploy” step, I have to do a number of things like:
- Create AWS S3 bucket
- Create AWS Cloudfront Distribution
- Configure Okta to allow logging into the app
All of the steps above use the PR number from Github in order to construct the URL. For instance, the URL will be project-name-<PR_NUMBER>
.
The Problem
I have a cron job set up to run every night that looks to see if the PR has been closed and it if has, I need to run my cleanup script which removes the S3 bucket, the Cloudfront Distro, and also the config in Okta. The problem is that I depend on the PR number in order to know which S3 bucket, Cloudfront Distro, and Okta URL to delete. However, the PR number is not available because the PR has be closed. The way that I am getting the PR number is by using the CircleCI environment variable CIRCLE_PULL_REQUEST
in a Python script. Here is the error I get when attempting to access that environment variable: KeyError: 'CIRCLE_PULL_REQUEST'
.
The Question
How can I persist the PR number so that my cleanup cron job has access to it? I looked at Artifacts but it looks like in order to get the Artifact, I need to know the build number and I am not sure how I would get that in the cleanup job since there could be multiple builds run.
Also, if there is a better way to trigger a cleanup (ideally it would be triggered when the PR is merged), then please let me know.
Here is my config.yml
(I removed the test step). :
version: 2.1
executors:
docker-executor:
docker:
- image: circleci/node:12
resource_class: xlarge
orbs:
aws-cli: circleci/aws-cli@0.1.17
commands:
install-python:
steps:
- run: sudo apt install python3-pip && sudo pip3 install -r ./scripts/requirements.txt
attach-workspace:
steps:
- attach_workspace:
at: ~/
persist-workspace:
steps:
- persist_to_workspace:
root: ~/
paths: ./
# CircleCI PR-only option must be enabled in the job settings
dev_only: &dev_only
filters:
branches:
ignore:
- main
- develop
- staging
staging_only: &staging_only
context: frontend
deploy_env: 'staging'
filters:
branches:
only: main
jobs:
install:
executor: docker-executor
steps:
- checkout
- restore_cache:
keys:
- npm-deps-{{ checksum "package-lock.json" }}
- run: npm i
- save_cache:
key: npm-deps-{{ checksum "package-lock.json" }}
paths:
- ~/.npm
- node_modules
- persist-workspace
build-deploy:
executor: docker-executor
steps:
- attach-workspace
- install-python
- aws-cli/setup:
aws-region: AWS_REGION
- add_ssh_keys:
fingerprints:
- "fingerprint here"
- run:
name: Clone/Install SG1 Admin
command: |
GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa'
git clone git@github.com:shipt/sg1-admin.git ~/sg1-admin
cd ~/sg1-admin
npm i
- run:
name: Setup Review App and Env Vars
command: |
./scripts/create_reviewapp.py
cat .env.txt >> $BASH_ENV
source $BASH_ENV
cat $BASH_ENV
- store_artifacts:
path: /tmp/bucket_name
destination: bucket_name
- run:
name: Build SG1 Admin
command: |
cd ~/sg1-admin
cat .env.development
REACT_APP_ENV=development npm run build
- run:
name: Build and Deploy
command: |
cat .env.txt >> $BASH_ENV
source $BASH_ENV
cat $BASH_ENV
REACT_APP_ENV=development npm run build
./scripts/deploy_app.sh
- run:
name: Comment on PR
command: |
cat .env.txt >> $BASH_ENV
source $BASH_ENV
cat $BASH_ENV
./scripts/comment_on_pr.py $REVIEW_APP_URL
cleanup:
executor: docker-executor
steps:
- install-python
- run: ./scripts/cleanup_reviewapp.py
workflows:
install_test_deploy:
jobs:
- install:
name: "Install Dependencies"
- build-deploy:
<<: *dev_only
name: "Deploy Review App"
requires:
- "Install Dependencies"
cleanup:
triggers:
- schedule:
cron: "0 0 * * *"
<<: *dev_only
jobs:
- cleanup:
name: "Cleanup"