Automate Build Checks and Slack Notification With Github-Actions

The purpose of this blog is to help those who want to set up CI/CD for their projects. There are many popular tools present in the market right now like CircleCI and TravisCI and Github Actions is one of them.

Before moving forward with GitHub Actions, it is necessary for us to understand what are software development workflows and why is the need for them to be automated.

In a software development project, there is a team that consists of several individuals who work on the project, and is it obvious that the project needs to be hosted on some Version control and source code management system like Github or Gitlab. There are several organizational tasks that need to be managed.

Let us consider a scenario where a team member has pushed some code and before merging it to the target branch, the maintainer needs to verify if it is passing all the test cases or not and also needs to verify if the project is building successfully or not.

Generally, if things had been manual, the maintainer had to run the tests first and test if the build is successful or not manually. Then after reviewing changes, the maintainer merges the source branch to the target branch.

Now considering a professional project which has multiple individuals working on it, the above tasks would have been a headache for the maintainer since there could have been multiple Pull Requests open at a time by several developers.

And above case is just one scenario, there can be multiple organizational tasks that needed to be performed like creating an artifact (Binary of your app, Container image, Website build, etc.), sending a message on slack after a PR is created, deploying code to certain environment testing/staging/production when code is merged on a particular branch.

Here comes CI/CD tools in picture which can help you out with all this tasks which doesn't need human involvement.

CD/CD stands for Continuous Integration and Continuous Deployment (or Delivery). These are practices in software development that allow teams to build projects together quickly, efficiently, and ideally with fewer errors.

Continuous Integration deals with managing tasks related to development workflow like build, test, etc branch before merging it with the main working branch. This helps to constantly make sure everyone’s code is working properly together and is well-tested.

Continuous Deployment deals with the deployment process which is in a continuous loop with CI.

So, in short, we can say that with the Continuous Integration process, you automate the building and the testing, whereas Continuous Deployment will automate deploying the project to an environment.

What are Github Actions?

Github Actions is a tool integrated into github which can be used to automate software development workflows.

GitHub Actions help you automate your software development workflows in the same place you store code and collaborate on pull requests and issues. You can write individual tasks, called actions, and combine them to create a custom workflow. Workflows are custom automated processes that you can set up in your repository to build, test, package, release, or deploy any code project on GitHub.

Whenever something happens TO or IN your github repository like PR created or issue created or PR merged etc, automatic actions can be executed in response using github actions.


This enables you to include CI/CD capabilities and many other features directly in your repository.

To learn more about the basics of Github Actions, click here.

Understanding Syntax of workflow files

name [optional]: is displayed on your repo’s action page

on [required]: name of Github event/events that triggers the workflow

jobs [required]: job/jobs segregated by job_id, to be executed when event gets triggered and consists of a sequence of tasks(steps)

Above are some of the most commonly used workflow syntaxes. To read more about workflow syntaxes, click here.

Now, let us move ahead and build a demo application where we can use github actions to automate workflow.

I will be using Golang for Writing the Code, and the commands in the upcoming parts will be golang specific but you can replace them with tools and commands specific to the programming language you choose.

Workflow to be automated

I will be using a code where the main function is just printing the current time on the console. In the below image, you can see the code in main.go file.

In the image below you can see the test cases for main.go in file main_test.go

So while raising a PR we will be checking for the following aspects :
1. Build should be successful
2. Test cases should be passing
3. go vet should not report any suspicious constructs
4. go fmt should check code formatting and pass
5. golint should pass without any error

and just for some fun, we will send a notification to Slack when a PR is created which fulfills all the above criteria.

In the end, we will also look at how to restrict Pull Request to get Merged if checks fail.

Note: Just for the demonstration purpose, I will be tweaking the code a little bit in different PRs so we can see the working of checks when code is pushed on any branch. I will be attaching the links for the failed checks for each step so that you can see the code the caused failure.

Github Workflow Implementation

Depending on the tech stack you are using, you can already find multiple github-action templates in github marketplace. Now, we will create a workflow for our project.

Step 1: Create .github/workflows in your project’s root directory. Here we will be storing our YAML files consisting of workflow configurations.

Step 2: Configure the YAML file according to the workflow you wish to implement. Below we will be looking at the workflow setup step-by-step.

Create a file named build-workflow.yml in .github/workflows directory.
You can find the build-workflow.yml file at the end of this blog or here.

a} Setup Workflow name and Configure trigger events.
An example of events is push and pull_request which we will be using.

b} Setup Jobs to run in a workflow.
The code in the below image specifies a job with job_id build. Jobs can be run sequentially or in parallel or chained. Each Job has some steps.

The first 2 steps which we performed are setup go environment using pre-existing action setup-go and check out our current repo in github workspace using action checkout so the workflow can access it.

Now we will be performing the checks that we need to set up in our workflow.

c} Perform step: build package
This step will check for the build failures in our code.

Check out Failing Build Action on github event, here.

d} Perform step: run test cases
This step will check if all test cases under the package are passing or not.

Check out Failing Test Cases Action on github event, here.

e} Perform step: go vet
This step will check if Go source code reports suspicious constructs.

Check out Failing go vet Action on github event, here.

f} Perform step: go fmt
This step will check for formatting (indentation and blanks etc.) of our code.

Check out Failing go fmt Action on github event, here.

g} Perform step: go lint
This step will run lint on your code and will report if it fails.
But before running the lint command, we will first install the package.

Check out Failing go lint Action on github event, here.

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

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

Click on Create New App.

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.

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

Step 3: Install our Bot to the Workspace

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

Click on Allow when redirected to the authorization page.

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.

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.

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

Step 5: Setup Job in Github Actions Workflow

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.

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.

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

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.

Success ( slack notification sent ) :

Alternate setup for slack notification, based on the details you want to be sent ( if you wish, you can skip this part since about part covered one way to do it) :

Configuration below is used to get 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.

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

Prevent Pull Request from getting Merged if Checks fail :

Step 1: Navigate to Settings on Github Repo. Select Branches and Click on Add Rule.

Step 2: Set Branch name pattern. I’ve set it to main since I want to prevent the main branch from unchecked merges, you can set it to * for protecting all branches.

Select from the options below based on your requirements, but just for protecting unchecked merge I have selected just one option “ Require status checks to pass before merging”.

After that, search for Status Checks (jobs) name on which you wish to depend on mergeability of branches and select them.

Click on Create at the bottom of the page to create a rule.

After the branch protection rule has been applied.

As you can see in the above image, after applying for branch protection we can see the message implying we can only merge if statuses pass.

Note: administrator of a repository can still merge the PR, this means this branch protection rule prevents non-administrators from merging.

Github action workflow file

Conclusion: In the end, I just hope this helped you with setting up Github Actions for your project. Though there are many things that got covered in this blog, there are still a lot more things that you can explore.

If you have reached this point, Thank You for reading this blog.

References :

Demo-Application-Repo :
Github-Actions :
Slack-Notification :
primary -
alternative -
slack-bot setup -
Blog :

Software Engineer @Josh Software Pvt. Ltd.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store