Send A Slack Notification Automatically When A Release Is Published On Github

Table of Contents

Table of Contents

This article will show you a way how to send a notification automatically after you published a release on github.
The way includes Github Actions and Slack Incoming Webhook.

About Github Actions

Github Actions

About Slack Incoming webhooks

Incoming webhooks for slack

Purpose

Create github release.
โ†“ Publish the github release.
โ†“ Automatically send a message which contains tag_name, author, release body, github url of the release to slack.

Process

Prepare Slack Incoming Webhook Url

Follow this article Incoming webhooks for slack, and we will get a url that can be use in Github Actions to send messages.

Github

Here are two things has to do.

  1. set the slack incoming webhook url to the Actions secrets.
  2. Create a Github Actions workflow.

Actions secrets

Go to the github repository, click Settings tab -> Secrets -> New repository secret -> Name: SLACK_WEBHOOK_URL, Value: The slack incoming webhook url.

Workflow

1. Go to the github repository, click the actions tab to open the github actions page.

(The page link maybe https://github.com/{user_name}/{repository_name}/actions.
user_name, repository_name must be changed to yours.)

2. Create a New workflow.

I will create my workflow, so click set up a workflow yourself link to open a basic workflow.
Set the workflow file name and edit it.
Here is the contents of workflow file.

name: ReleaseNotice
on:
  release:
    types: [published]
  workflow_dispatch:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      # To check the github context
      - name: Dump Github context
        env:
          GITHUB_CONTEXT: ${{ toJSON(github) }}
        run: echo "$GITHUB_CONTEXT"
      - name: Slack Notification on SUCCESS
        if: success()
        uses: tokorom/action-slack-incoming-webhook@main
        env:
          INCOMING_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
        with:
          text: A release is published.
          blocks: |
            [
              {
                "type": "header",
                "text": {
                  "type": "plain_text",
                  "text": "${{ github.event.release.tag_name}} is published!"
                }
              },
              {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": "*Author:*\n${{ github.actor }}"
                }
              },
               {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": "*Information:*"
                }
              },
              {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": ${{ toJSON(github.event.release.body) }}
                }
              },
              {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": "${{ github.event.release.html_url }}"
                }
              }
            ]
Trigger Timing

According to Events that trigger workflows, let trigger the event after publishing the release.

on:
  release:
    types: [published]
Use Slack Incoming Webhook

Thanks to author who created and shared the great work Slack Incoming Webhook, it can help us to send slack messages easily.
In the with part, we can design the message contents.
For debug, we can use Block kit to check what will the message look like in advance.

steps:
      # To check the github context
      - name: Dump Github context
        env:
          GITHUB_CONTEXT: ${{ toJSON(github) }}
        run: echo "$GITHUB_CONTEXT"
      - name: Slack Notification on SUCCESS
        if: success()
        uses: tokorom/action-slack-incoming-webhook@main
        env:
          INCOMING_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
        with:
          text: A release is published.
          blocks: |
            [
              {
                "type": "header",
                "text": {
                  "type": "plain_text",
                  "text": "${{ github.event.release.tag_name}} is published!"
                }
              },
              {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": "*Author:*\n${{ github.actor }}"
                }
              },
               {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": "*Information:*"
                }
              },
              {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": ${{ toJSON(github.event.release.body) }}
                }
              },
              {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": "${{ github.event.release.html_url }}"
                }
              }
            ]
3. Start Commit

You can commit it to main branch or other branch to let the workflow be launch.

Test

The workflow will be launched when you publish a release from the branch that the workflow is committed.
Now, create a new release and published it.
You will see the message is send to your slack channel.
github-actions-slack-message

Thatโ€™s all. I hope this will help you.