DEV Community

loading...

Build Notifications @ Slack using GitHub Notifications

Revanth Reddy
Carpe Diem
ใƒป2 min read

If you have a team and if there are many people in the team pushing to GitHub then it might be hard sometimes to check the build status of an active project. The slack notification GitHub action will notify the team regarding the build status.

Alt Text

My Workflow

To enable the slack notifications, I searched for the marketplace for one such action and found this:

GitHub logo act10ns / slack

Slack messages for GitHub Action workflows, jobs and steps

build-test

Slack messages for GitHub Action workflows, jobs and steps

A simple and flexible Slack integration with GitHub Actions.

Configuration

Environment Variables (env)

SLACK_WEBHOOK_URL (required)

Create a Slack Webhook URL using either the Incoming Webhooks App (preferred) or by attaching an incoming webhook to an existing Slack App (beware, channel override not possible when using a Slack App):

env
  SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

Input Parameters (with)

status (required)

The status must be defined. It can either be the current job status using:

  with
    status: ${{ job.status }}

or a hardcoded custom status such as "starting" or "in progress":

  with
    status: in progress

steps (optional)

The individual status of job steps can be included in the Slack message using:

  with: 
    status: ${{ job.status }}
    steps: ${{ toJson(steps) }}

Note: Only steps that have a "step id" will be reported on. See example below.

channel (optional)

To override theโ€ฆ

I used this action in my django project by adding some lines of code in .github/workflows/django.yml.

name: Django CI
on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: [3.6]
    env:
      SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
    steps:
    - uses: act10ns/slack@v1
      with:
        status: starting
        channel: '#build-notifications'
      if: always()
    - name: Checkout
      uses: actions/checkout@v2
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install Dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Run Tests
      run: |
        python manage.py test shop -v 2
    - name: Slack Notification
      uses: act10ns/slack@v1
      with:
        status: ${{ job.status }}
        steps: ${{ toJson(steps) }}
        channel: '#build-notifications'
      if: always()
Enter fullscreen mode Exit fullscreen mode

This is my .yml workflow file. You can add these lines to your workflow file and get the job done.

Observe that I have used act10ns/slack@v1 which is the github action responsible for all this.

Here secret is similar to an environment variable that can be added by going to settings > secrets >. Add key as SLACK_WEBHOOK_URL and pair as a webhook URL that can be obtained from Incoming Webhooks App to generate slack messages

Alt Text

Submission Category:

DIY Deployments

Yaml File

Link to Yaml File

Link to Code

GitHub logo revanth-reddy / Raitubazaar

Platform between fruits&vegetable vendors(farmers) and end-users

Additional Resources

Slack GitHub Actions
Slack GitHub Actions Repo

PS: I have implemented the Slack Notification GitHub Action in one of my existing projects. You can add it to your existing projects too.

Keep Exploring !!!

Discussion (0)

Forem Open with the Forem app