Slack with GitHub

How to notify users via Slack when something happens on GitHub.

This public content is an excerpt from Flutter staff GitHub docs. It is published as a reference to show how GitHub is used for inner source at Flutter.

All divisions in Flutter use Slack. Integrating GitHub and Slack together for notifications can improve your development workflows. There are 2 recommended approaches:

  1. Slack GitHub App – for quick and easy notifications, but you can’t customise the message.
  2. GitHub Action triggers a Slack Workflow – for flexible notification logic and message format, but harder to setup.

Slack GitHub App

The Slack GitHub App allows you to receive notifications from GitHub in Slack. It’s simple to set up and recommended if you’re happy with the message format it creates.

  • The app is already installed in your divisional Slack workspace and a member of all public channels. For private channels, you need to invite the app by using the /invite @github Slack command.
  • To enable the app, you need to authenticate it with GitHub within your channel. Do this by running the Slack command /github signin.
  • Once you have authenticated the app, use the /github subscribe <github_organization>/<github_repository_name> command to subscribe to notifications from a repository. You can unsubscribe at any time using /github unsubscribe <github_organization>/<github_repository_name>.

You can configure which notifications the channel receives by subscribing to specific features like pull requests, workflows, or labels. The GitHub Slack Integration guide documents a variety of options.

GitHub Action Triggers a Slack Workflow

If you need to customise the message format or notification logic, you can use a GitHub Action to trigger a Slack Workflow that posts a custom message.

1. Create a Slack Workflow

Create a Slack Workflow that runs when a custom webhook sends a web request to Slack from another app or service.

To create a new Slack Workflow:

  1. Click your workspace name in the top left and then select Tools > Workflow Builder which opens a new window.
  2. Then click Create in the top right and choose your workflow name, click Next, and select Webhook as the way to start the workflow.
  3. Setup the “variables” – this is the data fields you intend to send to your workflow from GitHub. Then add the steps you want your workflow to do, and finally get the webhook URL. Refer to the Slack docs for more detail.

2. Create the GitHub Action Workflow

Now you can trigger the Slack workflow from GitHub. First, add the Slack webhook URL as a secret in your repo settings named SLACK_WEBHOOK_URL.

Now you need to simply add an extra step to a new or existing GitHub Action workflow that triggers the Slack workflow with the correct data. You can use the slack-github-action action to send data to Slack.

- name: Trigger Slack Workflow
  uses: slackapi/slack-github-action@d419a1666387fff2f2205a3aa17cb432f3613051
  with:
    # This data can be anything from a previous step in the workflow.
    # Use the toJSON() function and env vars to escape the data correctly.
    payload: |
      {
        "my_variable": ${{ toJSON(env.MY_VARIABLE) }},
        "foo": ${{ toJSON(env.BAR) }}
      }
  env:
    SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

See the action README for more usage examples.

Slack Bot?

If a Slack workflow isn’t flexible enough, you can create a Slack bot. You can call a Slack bot via webhook from GitHub Actions in the same way you would a Slack workflow. In most cases however, there is no need to create a Slack bot – a simpler Slack workflow is all you need.

Examples

See our blog for stories and examples of slack usage in Flutter-Global.