本文將介紹你一種方法,當你在Github上發佈release後自動地發送相關訊息到slack上。
這個方法將使用Github Actions跟Slack Incoming Webhook。
Github Actions
Github Actions是一個Github的服務,讓你可以偵測Github中的事件然後做出相對應的處理。
以這次來說,將對要去偵測Github Release的發佈(Published)事件,一旦發佈事件完成,就去執行發送Slack訊息的處理。
Incoming webhooks for slack
Slack Incoming webhooks是一種網路API,讓你可以不用透過Slack軟體,也可以向目標頻道(Channel)發送訊息。
參考這篇文章 Incoming webhooks for slack, 我們可以取得一個slack Incoming Webhook url,並用它讓Github Actions發送訊息到slack。
這裡有兩件事要做。
到github repository, 點擊 Settings tab -> Secrets -> New repository secret -> Name: SLACK_WEBHOOK_URL, Value: The slack incoming webhook url.
到 github repository, 點擊 actions tab 開啟Github Actions 頁面。
這個頁面的網址可能是這樣:https://github.com/user_name/repository_name/actions
。
建立一個 New workflow。
這裡將建立一個自己的workflow,所以直接點擊 set up a workflow yourself 連結取開啟一個基本格式的workflow。
設定一個你喜歡的workflow檔名然後編輯內容。
以下就是這次的workflow內容。
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 }}"
}
}
]
根據Events that trigger workflows, 可以去設定觸發時機為當Release被發佈後的時候。
on:
release:
types: [published]
感謝這位作者製作並分享了他的作品 Slack Incoming Webhook, 透過它讓我們可以很簡單地去發送slack訊息。
在with
部分, 我們可以去設計訊息的內容。
為了確認跟除錯,可以使用Block kit事先地查看設計後訊息的樣貌。
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 }}"
}
}
]
內容編輯好了之後,可以將修改推上(commit)你的分支(branch),基本上是main分支,或者你也可以創立一個新的分支並將修改推到新分支上。
在你建立一個Release時,會要選擇有本次修改的分支,也就是這個分支上有設定好workflow。
如此事件才會被觸發,訊息才能被發送。
現在,創建一個新的Release並發佈,然後到Slack上看看訊息是否正確。
如果出現錯誤,可以到Github Actions上看看錯誤訊息並改正問題。
好了,以上就是這次要分享的內容,希望對你有所幫助。