Performing code checks with GitHub actions & sending results to slack

Khaliq
3 min readApr 29, 2021

What is a GitHub Action ?

A GitHub action is an element of your custom workflow that is triggered when a push or pull request is created and performs some action such as checking code, sending a message or deploying the application.

The most important concept of GitHub actions is their composability. You can assemble actions as building blocks and build a workflow that matches your needs.

In this article we would like to explore how we can put certain actions together and perform different steps based on outcome.

Lets create an action which performs linting checks on a new pull request.

on: pull_request

jobs:
lint-code:
runs-on: ubuntu-latest
name: Perform Checks
steps:
- name: Checkout
uses: actions/checkout@v2

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.29

Sending alerts

Next we want to get a notification on slack if these checks passed or failed.

We will use PingMe Action to send these alerts.

on: pull_request

# Get values from github secrets of slack token and target channels and set the variables.
# we can set these within the action block as well for pass/fail.
env:
SLACK_TOKEN: ${{ secrets.SLACK_TOKEN }}
SLACK_CHANNESL: ${{ secrets.SLACK_CHANNELS }}

jobs:
lint-code:
runs-on: ubunut-latest
name: Perform Checks
steps:
- name: Checkout
uses: actions/checkout@v2
# We want to perform checks and see if the code is properly linted.
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.29

- name: Alert when checks fail
uses: kha7iq/pingme-action@v1
# This action will only run if checks failed.
if: failure()
env:
SLACK_MSG_TITLE: '🟢 New Request: ${{ github.ref }}'
SLACK_MESSAGE: 'Event is triggerd by ${{ github.event_name }} Checks ❌ ${{ job.status }}'
with:
service: slack

- name: Alert when checks pass
uses: kha7iq/pingme-action@v1
# This action will only run if checks are successfull.
if: success()
# Message and Title are string values, you can create custome message or title.
env:
SLACK_MSG_TITLE: '🟢 New Request: ${{ github.ref }}'
SLACK_MESSAGE: 'Event is triggerd by ${{ github.event_name }} Checks ✅ ${{ job.status }}'
with:
service: slack
  • Passed
Screenshot passed checks
  • Failed
screenshot failed tests

You can see this workflow in action here.

kha7iq/pingme

https://github.com/kha7iq/pingme

--

--