Performing code checks with gihtub actions & sending results to slack

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.

 1on: pull_request
 2
 3jobs:
 4  lint-code:
 5    runs-on: ubuntu-latest
 6    name: Perform Checks
 7    steps:
 8      - name: Checkout
 9        uses: actions/checkout@v2
10
11      - name: golangci-lint
12        uses: golangci/golangci-lint-action@v2
13        with:
14          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.

 1on: pull_request
 2
 3# Get values from github secrets of slack token and target  channels and set the variables.
 4# we can set these within the action block as well for pass/fail.
 5env:
 6  SLACK_TOKEN: ${{ secrets.SLACK_TOKEN }}
 7  SLACK_CHANNESL: ${{ secrets.SLACK_CHANNELS }}
 8
 9jobs:
10  lint-code:
11    runs-on: ubunut-latest
12    name: Perform Checks
13    steps:
14      - name: Checkout
15        uses: actions/checkout@v2
16      # We want to perform checks and see if the code is properly linted.
17      - name: golangci-lint
18        uses: golangci/golangci-lint-action@v2
19        with:
20          version: v1.29
21
22      - name: Alert when checks fail
23        uses: kha7iq/pingme-action@v1
24        # This action will only run if checks failed.
25        if: failure()
26        env:
27          SLACK_MSG_TITLE: '🟢 New Request: ${{ github.ref }}'
28          SLACK_MESSAGE: 'Event is triggerd by ${{ github.event_name }} Checks  ❌  ${{ job.status }}'
29        with:
30          service: slack
31
32      - name: Alert when checks pass
33        uses: kha7iq/pingme-action@v1
34        # This action will only run if checks are successfull.
35        if: success()
36        # Message and Title are string values, you can create custome message or title.
37        env:
38          SLACK_MSG_TITLE: '🟢 New Request: ${{ github.ref }}'
39          SLACK_MESSAGE: 'Event is triggerd by ${{ github.event_name }} Checks ✅  ${{ job.status }}'
40        with:
41          service: slack
42        

Passed

Failed