iT邦幫忙

2024 iThome 鐵人賽

DAY 25
0
DevOps

菜逼八用Github Actions系列 第 25

Day 25 - 例子 - 完成發版後發送通知

  • 分享至 

  • xImage
  •  

目錄

摘要

在上一篇我們學到

這篇我們會來會來看一下如何用workflow觸發Slack發通知

關於slack-github-action

slack-github-action是Slack關發發布的action,使用這個action能夠將將資料發送到Slack

發送的方式有三種

  1. 將資料傳送到Slack的Workflow Builder(需要付費版才能用)
  2. 透過 Slack APP發送資料給workspace中的某個的channel、使用者
    除了傳送訊息功能,還可以更新Slack APP發出的訊息
  3. 透過 Slack incoming webhooks 發送資料
    除了純文字、圖片,也可以在訊息中加上互動元件

不過不管用哪個方法message payload中最多都只能有20個block

步驟

前置作業

  1. 準備一個有內容的repo
  2. 註冊Slack帳號,並建立一個workspace以及channel

https://ithelp.ithome.com.tw/upload/images/20240926/20135568DrfcS6sZbg.png

設置Slack APP

先來建立Slack APP

https://ithelp.ithome.com.tw/upload/images/20240926/20135568RrqTECUUHa.png
https://ithelp.ithome.com.tw/upload/images/20240926/20135568AdeLWdREhc.png
https://ithelp.ithome.com.tw/upload/images/20240926/20135568J58F23Gf0s.png

之後你會得到一堆App Credentials,這些credentials在打Slack incoming webhook時會用到,不過在這篇我們不會用到它,所以先不管

題外話,如果你想幫APP換頭貼可以到https://api.slack.com/apps選basic info做設定

然後到Your APPs點選剛剛建立的APP,然後選OAuth & Permissions幫APP做權限設定

https://ithelp.ithome.com.tw/upload/images/20240926/201355687I4eCaGpzl.png

完成後把APP裝到預先建好的workspace

https://ithelp.ithome.com.tw/upload/images/20240926/20135568Hp3dKOVlF0.png

創建好之後會得到Bot User OAuth Token,到你的repo把這個token設成名為SLACK_BOT_TOKEN的secret

最後用指令/invite @bot的名稱邀請APP進來頻道

撰寫workflow

name: Send a release message
# 以下例子是當deploy的workflow執行完後發通知
# 可以自行視情況修改觸發時機
on:
  workflow_run:
    workflows:
      - 'Deploy to github page'
    types:
      - completed

jobs:
  send-message:
    name: send message
    runs-on: ubuntu-latest
    steps:
      - name: Get the latest version
        # 取得master分支上最新的版號
        run: |
          echo "newVersion=$(git describe --tags --abbrev=0)" >> $GITHUB_ENV
      - name: Post to a Slack channel
        id: slack
        uses: slackapi/slack-github-action@v1.27.0
        with:
          # 使用瀏覽器打開Slack頻道就可以在網址上看到channel id(通常是C開頭)
          # 如果想要一次傳到多個頻道,就用,把channe id隔開
          channel-id: '你的channel id'
          # 想傳給人的話則使用user-id
          payload: |
            {
              "text": "New Version 🚀",
              "blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "New version $newVersion is deployed."
                  }
                },
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "For more details, please refer to release note."
                  }
                }
              ]
            }
        env:
          SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}

完成後只要新版本發布就會自動發送通知囉

https://ithelp.ithome.com.tw/upload/images/20240926/20135568PUDz7NAfEH.png


上一篇
Day 24 - 例子 - 發版後自動更新Jira release note狀態
下一篇
Day 26 - 例子 - Lighthouse整合到CI
系列文
菜逼八用Github Actions28
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言