Post to Slack on deployment

A step by step guide to using Humanitec webhooks to post a message to Slack when a deployment happens.

Overview

In this tutorial we will set up a webhook that will cause a message to be posted to a Slack channel whenever a deployment occurs in an Environment of a particular Application.

You will require:

  • An App set up in Humanitec

  • A "paid for" Slack organization

Create a Slack Workflow

The fastest way of getting a message into a channel in Slack is with Slack Workflows. We will build a workflow that is triggered by a webhook and then posts a message to a channel.

Open the Workflow Builder

You can access the workflow builder in Slack by clicking your Slack organization's name in the top left of the desktop version of Slack. Select Tools → Workflow Builder.

Create a new workflow

In the Workflow Builder, click the Create button in the top right.

When prompted, choose a name for your workflow.

Choose a "Webhook" trigger

Click the Select button next to "Webhook (advanced)" from the options to start the workflow.

Configure the webhook

The webhook request generated by Humanitec will contain a number of variables that we need to configure in Slack.

You should add the following variables:

Key

Data Type

app_id

text

env_id

text

triggered_by

text

comment

text

status

text

You can add these variables by clicking the Add Variable button.

Once the variables are added, your dialog should look like this:

Click Save to move to the next step.

Add a step to your workflow

Next, you will be prompted to add a step to the workflow. Click Add Step.

Then choose Send a message step by clicking the Add button next to "Send a message".

Choose the channel you want to send to and craft your message. You can insert the variables you added earlier into the message by clicking the Insert a variable button. You can also add formatting as you would in Slack. A preview of how your message will look is shown at the bottom of the window.

Once you are happy with your message, click Save.

Publish your Workflow

Click the Publish button in the top right of the Workflow Builder to publish your workflow.

Next you will be prompted with the URL to use for the webhook. Make sure you copy this somewhere safe. You will need to paste it into Humanitec later.

Create a Webhook in Humanitec

Navigate to the Webhooks section of the App Settings Screen for your Application in Humanitec.

  1. Enter an ID you will use to identify this webhook in future. It must be a valid Humanitec ID.

  2. Paste in the URL you copied in the last step of creating your Slack workflow.

  3. Select the Deployment → Finished trigger to make the webhook be fired once every deployment has completed.

  4. Click the Create button finish setting up the webhook.

Test your webhook

When you next deploy to an Environment in the App, you should get a slack message in the channel you setup in your workflow!