PullNotifier Logo
Published on

How to use Github Actions to send Slack notifications

Authors

How to use Github Actions to send Slack notifications

One of the easiest ways to get Github pull request notifications on your Slack Channel is by creating a Github Action. Whether you're looking to send a Slack message using Github Actions, post to Slack with a Github Action, or simply notify Slack about a pull request on Github, this guide has you covered.

Today we're going to show you how to do this.

Pro tip: You can skip all these steps and install a free app that will handle all of these for you ✨

Let's get started

First of all, let's list down the steps we need to do to make this work.

  1. Create an internal Slack app
  2. Add a Github Action to your codebase
  3. Add slack token to your Github repository secrets

Pretty straightforward! Let's dive right into it!

1. Create an Internal Slack app.

Slack makes it very easy for us to create "bots" or Slack apps that make our lives easier.

  • All you need to do is go to this link: https://api.slack.com/apps/ .You need to be logged in your Slack account in order to account is.
    • Click on Create New App → A popup should show up
    • On the popup
      • Add your app's name e.g. "PR Bot"
      • Choose your slack workspace
      • Click on Create App
    • Once the app is created, click on Oauth and Permissions on the Sidebar
    • On the Scopes section, specifically the Bot Token Scopes
      • click on Add an OAuth Scope
      • Select chat:write
    • Look for the Bot User OAuth Token field, and copy that token. Save it somewhere because it'll be used later on :)

And that's it for the first step! All you needed was the Bot User OAuth token, which we're going to use later on.

2. Add a Github action to your codebase

To send Slack notifications from Github Actions or post to Slack with Github Actions, follow these steps:

  • On the root of your codebase, create the file: .github/workflows/notify_slack.yml
  • The .github/workflows/ path allows Github to recognize that this is a Github action.
  • On the notify_slack.yml file, add the contents below:
  • Important notes:
    • Change the your-slack-channel-here below to the Slack channel that you're targeting.
    • Notice the secrets.SLACK_TOKEN reference below, we will get to that on the next step :)
  • Push these changes and merge it to your main branch.
name: PR Slack Notification

on:
  pull_request:
    types: [opened]

env:
  SLACK_TOKEN: ${{ secrets.SLACK_TOKEN }}

jobs:
  send-notification:
    runs-on: ubuntu-latest

    steps:
      - name: Notify Slack
        run: |
          curl -X POST -H 'Content-type: application/json' -H 'Authorization: Bearer ${{ secrets.SLACK_TOKEN}}' --data "{\"channel\":\"your-slack-channel-here\",\"text\":\"New PR created\\nDetails: *${{github.event.pull_request.title}}*\\nAuthor: *${{github.event.pull_request.user.login}}*\\n${{github.event.pull_request.html_url}}\"}" https://slack.com/api/chat.postMessage
  • That's it for now! This file is basically a Github Action that gets triggered every time a pull request has been made on your repository. It demonstrates how to use Github Actions to notify Slack.

3. Add slack token to your Github repository secrets

✅ So we've got the Github action on our repository. ✅ And we have a Slack internal app on our workspace. The final step is to add a slack token to your Github repository secrets.

Steps to do this:

  • Go to your repository on the Github site. The URL would look something like: github.com/{org_name}/{repo_name}
  • Go the the Settings page
  • On the security section, go to Secrets and variables and go to Actions
  • Click on New repository secret
  • Add the following details:
    • Name: SLACK_TOKEN
    • Value: Input the Bot User OAuth Token that we got from step 1 above
  • Click on *Add secret**

One more additional thing: Go to your Slack workspace - and on the slack channel you're targeting, make sure that you've invited the app to that channel. You can do so by typing /invite and you should be shown a list of apps.

✨ And that should do it!

Go ahead and create a new pull request on your repository to see Github pull request Slack notifications in action :)


Bonus section ⭐️:

You can skip all of the steps above and just install this free Slack app called PullNotifier.

All you need to do is:

  1. Install it in the Slack app marketplace on the Slack UI or just log in through their website directly.
  2. Connect Github & Slack
  3. And boom 💥! You now have Slack notifications for all pull requests on ALL of your repos.

The cool thing is you can also configure different Slack channels for each repository you have (very useful for large teams).

Here's a video on what PullNotifier does:

  That's it for today! Hope you found the content here useful ❤️