Integrate GitHub Actions with Slack, Say Goodbye to Email Notifications

For the last couple of weeks, we’ve been looking at GitHub Actions. If you missed those, check them out here:

Continuing with the theme of GitHub Actions, today, we’ll look at how we can get your GitHub Actions workflow to post messages in Slack when it carries out an action. Slack is ubiquitous in the workplace and as an industry, we’ve largely moved away from email notifications to more persistent, collaborative communication mediums such as Slack and Microsoft Teams. Let’s see how we can integrate these two invaluable tools together.

Create a Slack App

There are a few different ways you can create some automation around Slack – you can use setup an incoming webhook and have other apps or scripts post messages to it. If you have a paid Slack subscription, you can use the Workflow Builder, a no-code way of creating automations around Slack. Another way is to create a Slack app and use it to post messages into a channel. It is this last approach that we’ll explore today.

To create a Slack app, navigate to https://api.slack.com/apps. Click on the “Create New App” button.

Slack API landing page with the Create New App button

You are given the option to create an app “from scratch” or to “create a manifest”.  The former option provides a GUI to create an app and specify scopes and other settings while the latter allows you to specify the settings in YAML or JSON format.  I’ve selected the “from scratch” option for this tutorial.

Slack's create app modal asking for an app name and a workspace to develop your app in.

After your app is provisioned, it will appear in your app listing.  Click on its name to manage its various settings.  Navigate to the “Permissions” section within the settings.

The Basic Information blade for the Slack app that you just created showing various options including a Permissions button.

Navigate to the “Scopes” section.  Here, you’ll need to add the chat.write scope to its list of permissions so that you can post a message to Slack from a GitHub Action via this app.

Scopes selection screen with the chat: write scope selected.

Next, install your new app to your Slack workspace.

Install to Workspace button in the Slack app basic info screen.

You’ll need to provide the necessary approval for this app to be installed within your workspace.

Slack workspace prompting whether to allow this new Slack app to be installed

Next, head back to the OAuth & Permissions blade of your app and copy the “Bot User OAuth Token”.

OAuth & Permissions blade of Slack app showing the Bot user OAuth Token for the app.

Integrate Your Slack App with Your GitHub Actions Workflow

Login to your GitHub account.  Navigate to the Repository with the GitHub Action workflow that you want to integrate with Slack. Navigate to its Settings screen, then “Secrets and variables” and create a new secret by clicking on the “New repository secret” button.  Save the Bot User OAuth Token that you obtained from Slack, earlier as the secret’s value.   

GitHub repository's Secrets and variables section where you can create a new secret

In Slack, invite the bot to join the channel where you would like to receive automated messages from GitHub.  You can do so by typing /invite @your-bots-name.

Slack prompt to invite the Slack app (bot) to join a particular channel.

Update your GitHub Action workflow file, adding one or more steps to post messages to Slack from within the workflow. If you haven’t created a GitHub Actions workflow, please refer to the previous two posts for more information. Links are provided at the top of this article. Below, you’ll find a snippet that shows how to configure a step that posts a message to one or more slack channels.

- name: Post to a Slack channel
	id: slack
  uses: slackapi/slack-github-action@v1.23.0
  with:
    # Slack channel id, channel name, or user id to post message.
    # See also: https://api.slack.com/methods/chat.postMessage#channels
    # You can pass in multiple channels to post to by providing a comma-delimited list of channel IDs.
    channel-id: 'C04L9MJ39KN'
    # For posting a simple plain text message
    slack-message: "GitHub build result: ${{ job.status }}\n${{ github.event.pull_request.html_url || github.event.head_commit.url }}"
  env:
    SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} 

In the step above, you’ll need to reference the Slack channel(s) where you’d like GitHub to post messages.  You must specify the ID of the channel (and not its name).  You can find out the channel id by clicking on the Channel name in the Channel header and navigating to the bottom of the Channel details modal popup that appears.

Slack channel settings modal showing the Channel ID at the bottom

Save your changes and trigger one or more workflow changes.  You should see automated GitHub messages appearing in Slack.

The GitHub bot Slack app auto-posting messages in a Slack channel

Closing Remarks

GitHub Actions and Slack are two powerful tools that can help streamline your development workflow. While GitHub Actions allows you to automate tasks and processes, Slack keeps your team informed and organized. Integrating these two tools together can bring a whole new level of efficiency to your development process.

You can checkout my companion repository to this post, on my GitHub, here:

https://github.com/tvaidyan/github-actions-demo

Leave a Comment

Your email address will not be published. Required fields are marked *