Performing code checks with GitHub actions & sending results to slack

kha7iq

Abdul Khaliq

Posted on April 29, 2021

Performing code checks with GitHub 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.

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

Enter fullscreen mode Exit fullscreen mode

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_CHANNELS: ${{ 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

Enter fullscreen mode Exit fullscreen mode
  • Passed
    Alt Text

  • Failed
    Alt Text

You can see this workflow in action here.

GitHub logo kha7iq / pingme

PingMe is a CLI which provides the ability to send messages or alerts to multiple messaging platforms & email.


PingMe CLI

Release Go Report Card GitHub issues License Awesome Go Dev Reference

DocumentationSupported ServicesInstallGithub ActionConfigurationContributing


About

PingMe is a personal project to satisfy my needs of having alerts, most major platforms have integration to send alerts but its not always useful, either you are stuck with one particular platform, or you have to do alot of integrations. I needed a small utility which i can just call from my backup scripts, cron jobs, CI/CD pipelines or from anywhere to send a message with particular information. And i can ship it everywhere with ease. Hence, the birth of PingMe.

Everything is configurable via environment variables, and you can simply export the logs or messages to a variable which will be sent as message, and most of all this serves as a swiss army knife sort of tool which supports multiple platforms.

Supported services

  • Discord
  • Email
  • Gotify
  • Line
  • Mastodon
  • Mattermost
  • Microsoft
💖 💪 🙅 🚩
kha7iq
Abdul Khaliq

Posted on April 29, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related