Track Deployments in Appsignal with Kamal

Application Monitoring

In a previous article I shared a quick tip on how to track deployments with honeybadger.io. Had some likes, retweets and nice comments on twitter (X) after sharing it, like this from Mikael Henriksson that had replies from both Joshua Wood the co-founder of Honeybadger.io, and Roy Tomeij the co-founder of Appsignal!

Twitter(X)

Appsignal

OK! So, eager to please - and also grateful for the stroopwafel cart at Rails World from fellow Foundation members Roy and Thijs - here’s a quick write up of how to track deployments in Appsignal with Kamal post-deploy hooks. As an aside, I just remembered this pic of me and Roy and other Rails Foundation members at Rails World 2023 in Amsterdam, standing in front of a large “TOMORROW” sign for some reason.

Rails Foundation Members at Rails World 2023

Setup

So, back to the task at hand. You’re going to need add a couple of keys to .env as follows. Again, like in the last article I’m assuming you’re using dot-env as you should be with Kamal, so open up your .env file and add the following keys, replacing xyz123 of course with your data:

APPSIGNAL_PERSONAL_TOKEN=xyz123
APPSIGNAL_APP_ID=xyz123

You can find (and rotate if needed) your APPSIGNAL_PERSONAL_TOKEN at the foot of your account settings page here: https://appsignal.com/users/edit:

You can find your APPSIGNAL_APP_ID from the URL of your app when opened as described in the docs.

The app_id for an application can be found in the URL of the AppSignal.com when an application is opened. For legacy reasons the URL will mention /sites rather than /applications. The id that follows is your application id. eg. https://appsignal.com/demo-organization/sites/xyz123

Note, I got confused and thought APPSIGNAL_PUSH_API_KEY was the token needed for the deploy markers API, and took a while after troubleshooting why I was getting 401’s all the time!

Kamal Hooks

As mentioned in the last article, but repeating here for completeness, when you initialise Kamal with kamal init, it will create the .kamal/hooks/ directory that contains some sample scripts that are executed within the deployment lifecycle. You can read all about Kamal Hooks in the official documentation. In our case, we’re just going to talk about using the post-deploy hook to notify Appsignal about our deployment.

Post Deploy Hook

Inside the .kamal/hooks/ directory you should find several sample files. We’re interested in post-deploy.sample, so make a copy of that first, without the .sample suffix. cd into your project directory and then run:

cp .kamal/hooks/post-deploy.sample .kamal/hooks/post-deploy

Open up the new post-deploy file and replace the contents with the following:

#!/bin/sh

export APPSIGNAL_PERSONAL_TOKEN=$(grep -m 1 '^APPSIGNAL_PERSONAL_TOKEN=' .env | cut -d '=' -f2)
export APPSIGNAL_APP_ID=$(grep -m 1 '^APPSIGNAL_APP_ID=' .env | cut -d '=' -f2)

curl -H "Content-Type: application/json" -POST https://appsignal.com/api/$APPSIGNAL_APP_ID/markers.json?token=$APPSIGNAL_PERSONAL_TOKEN \
  -d "{ \"marker\":{ \"kind\": \"deploy\", \"revision\": \"$KAMAL_VERSION\", \"user\": \"$KAMAL_PERFORMER\"}}"

The first two lines parse your local .env file to grab the Appsignal personal token and app_id we added previously, and exports them for use in the shell session.

The third line uses curl to POST the “revision” and “user” for the specified app to Appsignal as a Marker. We use the $APPSIGNAL_APP_ID in the path to specify the app we want to mark, and $APPSIGNAL_PERSONAL_TOKEN to authenticate - both previously parsed from the .env file - and then leverage some convenience variables provided by Kamal: $KAMAL_PERFORMER being the person who ran the operation and $KAMAL_VERSION to get the git revision. Neat!

Confirm in Appsignal

Deploy your app with kamal deploy and then head over to Appsignal and click “Deploys” in the left menu when viewing your app to see the results. With a bit of luck, if all went to plan you should see your deployments listed like this:

Create User

As always, don’t hesitate to drop me a note via twitter or any other channels listed here if you have questions, improvements or corrections!