Slack bot for reminding about Heroku deployments
Using a reminder in Slack works, but can't show dynamic content about current deployments. Also, for some reason, the Heroku dashboard doesn't show a diff between prod and staging when there was a rollback.
- diff between staging and production
- current commit on production
- when code was last deployed to production
- warning when last deploy was a rollback
- promotion button from staging to prod
- links to staging and production envs
- deployment info updates on deploy
-
Navigate to https://api.slack.com/apps
-
Hit "Create New App" giving the app a name and selecting the workspace
-
Under "Add features and functionality" select "Permissions"
-
Add
chat:write:bot
as a permission -
At the top of the page, still under "OAuth & Permissions" select "Install App to Workspace"
-
Copy the "OAuth Access Token" and save it for later. We'll need for deploying the app. (
TTD_SLACK_API_TOKEN
) -
Navigate back to the "Basic Information" tab under "Settings" and configure the color, image, and description for the bot under "Display Information". Don't forget to hit "Save"!
-
Navigate to https://github.com/settings/apps
-
Hit "New GitHub App" and provide a name and home page url (can be anything, like https://github.com/sbdchd/time-to-deploy)
-
Scroll to the "Webhook" section and uncheck "Active"
-
Scroll to "Repository permissions" and select "read-only" for "Contents"
-
Scroll to bottom and click "Create GitHub App"
-
Record your "App ID" (
TTD_GITHUB_APP_ID
) shown under the "About" section -
Scroll to "Private keys" and click, "Generate a private key"
-
Convert the downloaded private key to base64. For example,
base64 my-app-name.2020-01-01.private-key.pem
. Use this encoded value forTTD_GITHUB_APP_PRIVATE_KEY_BASE_64
. -
On the left hand side click "Install App". Install the app.
-
Note the ID in your URL. For example, from the URL
https://github.com/settings/installations/15330603
, the installation ID would be "15330603" (TTD_GITHUB_INSTALL_ID
).
-
Log into the AWS console and navigate to https://console.aws.amazon.com/lambda/#/functions
-
Press the "Create function" button in the upper right hand corner.
-
Leave the "Author from scratch" section selected & fill out the function name with
time-to-deploy
. The default nodejs version is fine. Press "Create Function" -
At your lambda function's homepage, click "Copy ARN" and save the value for later.
-
Under "Configuration", edit the "General Configuration" to provide 512MB of RAM and 30 second timeout.
-
At the function homepage, press the "+ Add trigger" button and select "CloudWatch Events".
-
create a new rule giving it a name, this is going to be the cron that runs the deploy reminder.
-
in the schedule expression input
cron(30 14 ? * MON-FRI *)
which will run the job every weekday at 14:00 UTC. see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html for more info on the cron format. Hit the add trigger button. -
Create a Dynamodb table with the table name
time-to-deploy
and the partition key aspk
. -
Create a new IAM policy to give read and write access to the dynamodb table. Select
DynamoDB
as the Service. Under Actions, enableGetItem
under the Read section andPutItem
in the Write section. Under Resources, add your lambda's ARN you copied earlier. -
Find the IAM Role corresponding to the lambda function. It should start with
time-to-deploy-role
. Click "Attach Policies", then select your newly created IAM policy. -
Back at the function detail page scroll down to the env and input the env vars according to the
.env-example
file located in this repo. Use the previous OAuth Acess Token that starts withxoxp-
as theTTD_SLACK_API_TOKEN
. For theTTD_SLACK_CHANNEL_ID
you'll want to get the channel ID from the Slack URL. Don't forget to save your changes. -
Now we need to update our function with the actual code. Run
s/build
ands/deploy
. If you didn't name your lambda functiontime-to-deploy
, be sure to update thes/deploy
script before running it. -
Setup an API Gateway so external HTTP requests can trigger the lambda. Click "+ trigger" on the function homepage and create an
HTTP API
withopen security
. Navigate back to your lambda function homepage and click the new "API Gateway" trigger and copy the API endpoint URL. Append?auth_token=your_http_auth_token_here
to the URL and configure it as a Heroku deploy hook for all of your Heroku apps.
- "+ trigger", create api, http api, open security. go back to the lambda function homepage, click the new "API Gateway" trigger, copy the API endpoint URL and append "?auth_token=your_auth_token_here" to the URL to use as a Heroku deploy hook. The URL should look like:
https://e478006295.execute-api.us-east-1.amazonaws.com/default/time-to-deploy?auth_token=your-http-secret-here
-
Hookup Heroku post deploy hooks for each env of the apps to the API Gateway.
-
Optionally set a SENTRY_DSN in your environment variables to get Sentry error reports.
Run the function and ensure the deploy message appears in your Slack channel.
s/run
yarn install
s/lint
s/test
s/build
s/deploy
# run the lambda, usually for testing
s/run