Skip to content

Instantly share code, notes, and snippets.

@sampsonbryce
Last active August 22, 2023 14:14
Show Gist options
  • Save sampsonbryce/da5587e2464defb64bbddb063e2f2322 to your computer and use it in GitHub Desktop.
Save sampsonbryce/da5587e2464defb64bbddb063e2f2322 to your computer and use it in GitHub Desktop.
Heroku Sentry Release Script

Heroku Sentry Release

Trigger a release in sentry from heroku. A workaround for getsentry/sentry#15722

Note:

  1. The script is setup for a RAILS project, you may need to change how the script finds the sentry environment
  2. The script is setup for if you use commit hashes as the release versions in sentry. It does not use the heroku release version.
# Sentry's Heroku app only supports deploys to one environment @see https://github.com/getsentry/sentry/issues/15722
# so this script is a replacement for performing the deploys and can be used during the heroku release phase to create
# a release in sentry @see https://devcenter.heroku.com/articles/release-phase
#
# Challenges:
# 1. Heroku doesn't allow access to git commit history
# To solve this we use the heroku api to get the previous slug commit
# and manually set the sentry commits via `set-commits --commit` instead
# of `set-commits --auto`
# 2. Heroku doesn't allow you to install any software on one-off dynos (which is where release phase scripts are run)
# To solve this we install yarn which is able to be installed locally (in the home folder, note the installation of yarn
# fails due to not being able to update the .profile file but the binary is still downloaded and available). With yarn
# installed locally we can install sentry-cli. We also download the jq binary to process the json response from herokus api
#
# Required ENV vars
# * SENTRY_ORG
# * SENTRY_PROJECT
# * SENTRY_AUTH_TOKEN
# * HEROKU_API_KEY (can be generated by running `heroku authorizations:create` to get a long life token)
# * RAILS_ENV (change to another env var if not running this on a rails project. this should be `staging` or `production`)
# * REPO should be like '<username_or_organization_name>/<repo_name>'
#
# Sentry env vars are needed for creating the sentry release https://docs.sentry.io/workflow/releases/#create-release
# Install yarn
curl -o- -L https://yarnpkg.com/install.sh | bash
# Put this here since the yarn install technically fails due to it not being able to update a .profile or .bash_profile
# but the binary exists so we don't care about the yarn install script updating .profile or .bash_profile
set -ex
# Install jq
wget -O jq https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64
chmod +x ./jq
# Get last heroku version
CURRENT_VERSION="${HEROKU_RELEASE_VERSION:1}" # Turn v100 into 100
PREVIOUS_VERSION=$(expr $CURRENT_VERSION - 1) # Subtract 1
# Get slug id for last heroku version
PREVIOUS_SLUG_ID=$(curl https://api.heroku.com/apps/$HEROKU_APP_NAME/releases/$PREVIOUS_VERSION \
-H "Accept: application/vnd.heroku+json; version=3" -H "Authorization: Bearer $HEROKU_API_KEY" | ./jq -r '.slug.id')
# Get commit for last heroku slug id
PREVIOUS_SLUG_COMMIT=$(curl https://api.heroku.com/apps/$HEROKU_APP_NAME/slugs/$PREVIOUS_SLUG_ID \
-H "Accept: application/vnd.heroku+json; version=3" -H "Authorization: Bearer $HEROKU_API_KEY" | ./jq -r '.commit')
# If commits match, we didn't launch any new code so we can skip sentry release
if [[ "$PREVIOUS_SLUG_COMMIT" == "$HEROKU_SLUG_COMMIT" ]]; then
exit 0
fi
# Check if we can actually get a previous slug commit
if [[ "$PREVIOUS_SLUG_COMMIT" == "null" ]]; then
exit 1
fi
# Install packages
~/.yarn/bin/yarn
# Release
./node_modules/.bin/sentry-cli releases new $HEROKU_SLUG_COMMIT
./node_modules/.bin/sentry-cli releases set-commits --commit "$REPO@$PREVIOUS_SLUG_COMMIT..$HEROKU_SLUG_COMMIT" $HEROKU_SLUG_COMMIT
./node_modules/.bin/sentry-cli releases finalize $HEROKU_SLUG_COMMIT
./node_modules/.bin/sentry-cli releases deploys $HEROKU_SLUG_COMMIT new -e $RAILS_ENV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment