Automate Slack Notification With Github-Actions

Sagar Sonwane
6 min readJun 26, 2021

--

This particular blog is a continuation of a previously written blog that covered the basics of github actions and a working demo of setting up github actions for running build checks when some code is pushed or PR is raised in your github project.

Automate Build Checks With Github-Actions : https://sagarsonwane230797.medium.com/automate-build-checks-and-slack-notification-with-github-actions-3b7eb702dae6

In this blog, we will be covering how to send a slack notification after a PR is raised which passes all the build checks.

Setting Up Slack Notification after All check are passed on Pull Request :

slack channel with a message from bot

The configuration below is used to get the slack notification in the above format.

There are several steps you need to perform to set up slack-bot in your slack workspace to get notifications after PR is raised which passes all checks.

Step 1: Create New App for our slack workspace

create new app on slack page

Click on Create New App.

setting up name of the app and selecting workspace to integrate with

Set an App Name for your app to which I’ve set Github-Actions and Select the workspace (mine is integrations) you need to integrate your app with.
Then click on Create App.

Step 2: Assign Permissions to Slack Bot.

Navigate to App Home on the left sidebar. Click on Review Scopes to Add to set permissions for our bot.

adding permissions to slack bot

Click on Add an OAuth Scope and select appropriate permissions.
Since our bot just needs to write on the slack channel, so I have provided only 2 permission channels:join and chat:write

selecting permissions for bot

Step 3: Install our Bot to the Workspace

Now, navigate to OAuth & Permissions and Click on Install to Workspace

install slack app on workspace

Click on Allow when redirected to the authorization page.

bot asking permission to access slack workspace

Now, our slack bot is ready to operate on the workspace we have integrated it with. You will be redirected to the OAuth & Permissions page again where you can see now a Bot User OAuth Token.

generated bot user OAuth token

Copy this Token and Save this in your github repository secrets. Go to Settings -> Secrets & save the token under the name SLACK_BOT_TOKEN.
We will be using this in gihtub workflow file.

Step 4: Invite Bot to Channel

We will now invite Slack Bot to the channel we need to get the notifications.
I have created a separate channel “demo-project_pull-requests” and send this message on channel /invite @bot-name.

inviting bot to channel where we want to get notifications

With this, we have finished setting up a slack bot for getting the notifications.

Step 5: Setup Job in Github Actions Workflow

setting up job for sending slack notification

The above job uses a pre-existing Github Action i.e Post Slack Messages.
I have embedded the build-workflow.yml file at the end for you to fully see the contents.

Here, notify job is linked to the build job i.e if the build job successfully runs then only notify job will be trigerred.

Note: needs tag identifies any jobs that must complete successfully before this job will run. It can be a string or an array of strings. If a job fails, all jobs that need it are skipped unless the jobs use a conditional expression that causes the job to continue.

Two important fields to focus on in image above:
1. env.SLACK_BOT_TOKEN: It will be used to communicate with slack.
2. with.args: It is used to configure payload to the slack API. It includes 2 main things.

a] channel holds the value of your slack Channel ID.
In the image below in the URL bar, you can see the last value(underlined) that is the Channel ID.

example image of received slack message from bot

We will go ahead and store the Channel ID in github secrets in our project repository under the name SLACK_PR_CHANNEL_ID.

github page with repository secrets

In the above image you can see we have set 2 secrets which are :
1. SLACK_BOT_TOKEN: bot token used to communicate with slack.
2. SLACK_PR_CHANNEL_ID: Channel ID to our slack channel where notifications will be sent.

b] blocks is our actual message to the slack channel. In with.args, the value of blocks is stringified and escaped.

Generally, the payload looks like in the image below.

{
"channel":"${{ secrets.SLACK_PR_CHANNEL_ID }}",
"blocks":[
{
"type":"section",
"text":{
"type":"mrkdwn",
"text":"*Pull Request:* ${{ github.event.pull_request.title }}"
}
},
{
"type":"section",
"text":{
"type":"mrkdwn",
"text":"*Contributor :* ${{ github.event.pull_request.user.login }} *Request State:* ${{ github.event.pull_request.state }}"
}
},
{
"type":"section",
"text":{
"type":"mrkdwn",
"text":"<${{ github.event.pull_request.html_url }}|View Pull Request>"
}
}
]
}

Success ( slack notification sent ) : https://github.com/sagar23sj/github-workflow-demo/actions/runs/921996014

Alternate setup for slack notification:

Note: Choose the configuration based on the format you choose to send the slack message. If you think the format which we just saw above is good enough for you, then you can skip this part.

An alternative format of the slack message

The configuration below is used to get a slack notification in the above format.

For this you need to do 2 things :

1. Create Incoming Webhook URL on slack. Incoming webhooks are a simple way to post messages from external sources into Slack. You will get the below page at the end where you will find the Webhook URL.

slack page with created webhook url

2. Save the Slack Webhook URL in your github secrets. Settings->Secrets under the name SLACK_WEBHOOK_URL.

3. Setup Job in Github Actions Workflow.

setting up a job for slack notification in an alternative format

The above job uses a pre-existing Github Action i.e action-slack.
I have embedded the build-workflow.yml file below for you to fully see the contents. You can replace the existing configuration for notify job with the above configuration to get a slack message in the alternative format.

Github action workflow file

This workflow file contains a build checks job(build) following the slack notification job(notify).

You can find Part-1 of this blog which covers Automate Build Checks With Github-Actions, at the link below.
https://sagarsonwane230797.medium.com/automate-build-checks-and-slack-notification-with-github-actions-3b7eb702dae6

Conclusion: In the end, I just hope this helped you with setting up Github Actions and automate slack notification for your project.

Thank You for reading this blog.

References :

Demo-Application-Repo : https://github.com/sagar23sj/github-workflow-demo
Github-Actions :
https://docs.github.com/en/actions
Slack-Notification :
primary
https://github.com/marketplace/actions/post-slack-message
alternative — https://github.com/marketplace/actions/action-slack
slack-bot setuphttps://spacejelly.dev/posts/how-to-use-github-actions-to-automate-tests-and-slack-notifications/

--

--