Skip to content

How to Integrate Slack into GitHub Actions Workflows

Jeremy Postlethwaite edited this page Dec 5, 2023 · 3 revisions

Continuous Integration: Slack and GitHub Actions

Slack provides multiple ways to send notifications to your Organization Workspace.

In this tutorial, we will be creating an app under my GammaMatrix Slack account.

Dev Note You will need a working configuration file to integrate Slack messaging

We will then be using Slack Send GitHub Action - Technique 2: Slack App to send messages to the app.

  • This GitHub Actions package is available from Slack's GitHub account.
  • This package supports sending to multiple channels in Slack; however, in our example, we will be using a dedicated channel called #ci, short for Continuous Integration. You can use whatever channel you want. A CI channel could be noisy, so you may want the ability for others to mute it. So probably best not send it to a main communication channel.
  • We will be sending a single message with the start time and an IN PROGRESS status. When our GitHub Workflow Action completes, we will update the same message with one of the finishing statuses: SUCCESS or FAILED

Read more on: GitHub Actions

A few notes about using this technique:

If you have multiple PHP repositories, you will be able to use, nearly the same, workflow file. Just edit the header messages in the Slack payload of the YAML file to denote different repositories.

This tutorial uses some simple math calculations, in BASH, on the runner using $GITHUB_OUTPUT which is similar to $GITHUB_ENV.

Workflow timer

We use a few environment variables to keep track of the time it takes to run a test. Be mindful of the names you use and do not overwrite other variables they may already exist in a shell environment.

NOTE: This step is optional.

  • If omitted, make sure to remove the date and duration from the Slack message if you only care about the build status or links to the build, commits and pull requests.

Environment variables for the timer

Variable type Description
DATE_END string Used in the Slack message for the end date.
DATE_START string Used in the Slack message for the start date.
TIMESTAMP_START integer Used to calculate the duration of the steps.
TIMESTAMP_END integer Used to calculate the duration of the steps.

Here is a snippet from the ci.yml configuration of the Slack message markdown that is using DATE_START and DATE_END which get saved in the steps: timer_start and and timer_end of $GITHUB_OUTPUT:

{
  "type": "section",
  "fields": [
    {
      "type": "mrkdwn",
      "text": "*Start:*\n${{ steps.timer_start.outputs.DATE_START }}"
    },
    {
      "type": "mrkdwn",
      "text": "*End:*\n${{ steps.timer_end.outputs.DATE_END }}"
    }
  ]
}

Preparing time

steps:
  - name: Preparing timer
    id: timer_start
    run: |
      echo "DATE_START=$(date +'%Y-%m-%d %H:%M:%S')" >> $GITHUB_OUTPUT
      echo "TIMESTAMP_START=$(date +'%s')" >> $GITHUB_OUTPUT

 - name: Stopping timer
  if: ${{ !cancelled() }}
  id: timer_end
  env:
    TIMESTAMP_START: ${{ steps.timer_start.outputs.TIMESTAMP_START }}
  run: |
    echo "DATE_END=$(date +'%Y-%m-%d %H:%M:%S')" >> $GITHUB_OUTPUT
    echo "DURATION_PHRASE=$(($(date +'%s')-$TIMESTAMP_START)) seconds" >> $GITHUB_OUTPUT

IMPORTANT: Use if: ${{ !cancelled() }} instead of if: ${{ always() }} for your steps.

  • If something goes wrong, your build could hang for up 6 hours before being terminated.

https://github.com/slackapi/slack-github-action

Create an app

Set the App Icon

For now, I will use the Continuous Integration icon from Web Skills Flat Vectors Collection created by Andreas Mehlsen - thank you!

SVG Repo provides an interface to create a PNG of the SVG. Slack uses a square image smaller than 512px.

Add the App to your Slack Workspace

Get your channel ID for the Slack message

Create a secret in your repository

We will place the secret at the end of the YAML configuration file.

      env:
        SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}

Go to Security > Secrets and variables > Actions

https://github.com/<organization/<repository>/settings/secrets/actions

There are two sections for secrets: Environment and Repository

  • We will be using a repository secret.

Click the New repository secret button and set the value of the OAuth token you created in the Slack App for Continuous Integration: SLACK_BOT_TOKEN

Use Slack's Block Kit Tool to construct your message

Slack provides a helpful set of Block-kit docs for constructing your messages.

- name: "Slack notification: IN PROGRESS"
      id: slack
      uses: slackapi/[email protected]
      with:
        channel-id: 'YOUR-CHANNEL-ID-FROM-YOUR-SLACK'
        payload: |
          {
            "text": CI Build Status for playground-matrix: IN PROGRESS\n${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}",
            "blocks": [
                {
                "type": "header",
                "text": {
                  "type": "plain_text",
                  "text": ":runner: CI Build Status for playground-matrix"
                }
              }



            ]
          }
      env:
        SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
  • The formatting is pretty simple. You can use icons that you use in slack with their shortcut string, for example: :runner: or :timer_clock: that is used here.
  • VS Code (a free and great code editor if you need one) will mark your secret if you have not set it yet:

Slack message from the workflow:

Pull Requests

Merges into develop

References and Notes

Packages used in this workflow

Links to Slack Documentation

Scratch Notes