Story

GitHub ❤️ Slack

by: Nuno Crisóstomo

Our Slack integration docs describe 2 ways to integrate GitHub and Slack:

  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.

But how do engineers use these integrations? This post explores a few examples.

Show me the PRs!

A typical use case is to notify a Slack channel when opening, updating, or merging pull requests in a repository. This is the most common use of the Slack GitHub App using the /github command:

/github subscribe Flutter-Global/my-repo-name pulls

This is a quick and easy way to get started, but this default message format is long (I’ve just captured the top bit above!). With lots of pull requests, the channel gets hard to read.

Concise PR Notifications

For busy channels, a more concise format is better. For example, using a GitHub Action and Slack Workflow we post a concise link to an RFC pull request when it’s ready for review:

name: RFC PR Slack Message
on:
  pull_request:
    types: [ ready_for_review ]
    paths:
      - 'rfcs/**'

permissions: {}
jobs:
  rfc_notification:
    runs-on: ubuntu-latest
    steps:
      - uses: slackapi/slack-github-action@d419a1666387fff2f2205a3aa17cb432f3613051
        with:
          payload: |
            {
              "title": ${{ toJSON(env.TITLE) }},
              "actor": ${{ toJSON(github.actor) }},
              "url": ${{ toJSON(env.HTML_URL) }}
            }
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.RFC_SLACK_WEBHOOK_URL }}
          TITLE: ${{ github.event.pull_request.title }}
          HTML_URL: ${{ github.event.pull_request.html_url }}

The actions/stale GitHub Action is often used to manage stale pull requests – this adds a Stale label to the PR before closing it. Some users use this label to trigger a Slack notification:

name: Notify Stale PRs to Slack

on:
  pull_request:
    types: [ labeled ]

jobs:
  notify-stale-prs:
    if: ${{ github.event.label.name == 'Stale' }}
    runs-on: ubuntu-latest
    steps:
      - uses: slackapi/slack-github-action
    # ...

CI/CD Result Notifications

When using GitHub Actions for CI, it helps to know in Slack when a build passes or fails. You can simply add a messaging step to an existing GitHub Workflow job, or define separate pass and fail notification jobs that depend on the main CI jobs:

jobs:
    build-and-push-apps:
       # ...

    success-slack-message:
        if: success()
        needs: [build-and-push-apps]
        steps:
            # ...
            - uses: slackapi/slack-github-action@d419a1666387fff2f2205a3aa17cb432f3613051
              with:
                  payload: |
                      {
                      	"message": ":white_check_mark: Build of ${{ github.head_ref }}  successful!",
                      }
    
    failure-slack-message:
        if: failure()
        needs: [build-and-push-apps]
        steps:
            # ...
            - uses: slackapi/slack-github-action@d419a1666387fff2f2205a3aa17cb432f3613051
              with:
                  payload: |
                      {
                      	"message": ":x: Build of ${{ github.head_ref }} failed!",
                      }

by: Nuno Crisóstomo
in:
tags: Pull Requests Slack
category: Story