Skip to content

Instantly share code, notes, and snippets.

@codingjoe
Created January 19, 2021 17:29
Show Gist options
  • Save codingjoe/e2ebb7d189468a82fe50d3fe00122a8d to your computer and use it in GitHub Desktop.
Save codingjoe/e2ebb7d189468a82fe50d3fe00122a8d to your computer and use it in GitHub Desktop.
Sentry Release on Heroku w/ commits, sourmaps & assets

Sentry Release on Heroku w/ commits, sourmaps & assets

Sentry is a great tool to monitor your application and Heroku is great to deploy applications.

To make the most of both services we created a routine that enables all Sentry features like

  • release tracking (with the correct Heroku release version),
  • commit trackings (so you know who broke what),
  • sourcemap or assset publishing (so you always have a pretty stack trace on Sentry).

Setup

The Heroku legacy integration isn't able to provide all that information to Sentry, but it can be setup via the Sentry CLI with a coupple simple steps:

Install Sentry CLI

You will need to add the Node.JS buildpack, if you don't use it already:

heroku buildpacks:add heroku/nodejs

Next, you need to add the Sentry CLI to your projects runtime dependencies:

npm i @sentry/cli

Finally you will need to enable Heroku Dyno Metadata. (This is a labs feature, but has been around for ages.)

heroku labs:enable runtime-dyno-metadata

You will also need some Sentry specific environment variables:

heroku config:set SENTRY_AUTH_TOKEN=xxxxx \
SENTRY_ORG=your_sentry_org \
SENTRY_PROJECT=your_sentry_project \
GITHUB_ORG=your_gh_org
GITHUB_REPO=your_gh_repo

Finally add or extend your Heroku release phase with the scripts below. You can of course skip any step, like uploading sourcemaps, if you don't have any.

PS: Don't forget to make your script files executeable chmod +x ;)

#!/usr/bin/env bash
# Add a little Heroku style syntax sugar.
indent() {
sed "s/^/ /"
}
puts-step() {
echo "-----> $*"
}
puts-warn() {
echo " ! $*"
}
release: bin/release
web: ...
worker: ...
#!/usr/bin/env bash
set -eo pipefail
BIN_DIR=$(cd "$(dirname "$0")"; pwd)
# shellcheck source=bin/heroku_utils
source "$BIN_DIR/heroku_utils"
if [[ -z "$SENTRY_AUTH_TOKEN" ]]; then
puts-warn "Skip Sentry release"
else
puts-step "Sentry release"
sentry-cli releases new "$HEROKU_RELEASE_VERSION" | indent
sentry-cli releases set-commits "$HEROKU_RELEASE_VERSION" --commit "$GITHUB_ORG/$GITHUB_REPO@$HEROKU_SLUG_COMMIT" | indent
sentry-cli releases files "$HEROKU_RELEASE_VERSION" upload-sourcemaps ./assets/js --url-prefix='~/static/js' | indent
sentry-cli releases finalize "$HEROKU_RELEASE_VERSION" | indent
fi
puts-step "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment