Send A Slack Notification Automatically When A Release Is Published On Github
Tags: github, devops, slack, message
Published: 2021-12-07 03:12:05

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

## Original Source
[cyublog | Send A Slack Notification Automatically When A Release Is Published On Github](https://cyublog.com/articles/dev-en/github-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](https://slack.com/help/articles/115005265063-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.  
Enter the workflow file name and edit the contents.  
Here is the contents of workflow file.  

```yml
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 }}"
                }
              }
            ]
```

##### 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](https://github.com/scobin/colab-tutorial/blob/main/asserts/image/github-actions-message.png?raw=true)
That’s all. I hope this will help you.