はじめに
前回の続きです
→GithubActionsを使って自動テストしてみた
自動テストが終わった後にグリーンでもレッドでもslackに通知を送れるようにしました
準備
今回はSlackのincoming webhooksを使っていきます
旧方式と新方式があるっぽいんですが、今回は旧方式でやってみました。
通知を送りたいチャンネルを設定してWebhook URLをメモっといてください
そしていつもの如く、GithubのSecretsに追加します。
Secretsの追加の方法は下記の記事に書いてあるのでそちらを参照してください
→Github Actionsを使ってDeploygateにデプロイした話
これで準備完了!!
やり方
Github Actionsには直前のジョブのステータスをチェックしてくれる関数があります。
これを使ってテストが成功なのか失敗なのかを判断できます。
まずは全体をどうぞ
name: DevUnitTest
on:
pull_request_review:
types: [ submitted ]
branches: [ develop ]
jobs:
test:
if: github.event.review.state == 'approved'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: UnitTest
run: ./gradlew testDevDebugUnitTest
- name: Unit Test Success Notification
if: ${{ success() }}
run: |
curl \
-X POST \
-H "Content-Type: application/json" \
-d '{"attachments": [{"color": "#26A745", "title": "${{ github.event.pull_request.title }}", "title_link": "${{ github.event.pull_request.html_url }}", "text": "Success Unit Test", "author_name": "${{ github.event.pull_request.user.login }}"}]}' \
${{ secrets.SLACK_WEBHOOK}}
- name: Unit Test Failure Notification
if: ${{ failure() }}
run: |
curl \
-X POST \
-H "Content-Type: application/json" \
-d '{"attachments": [{"color": "#D73A49", "title": "${{ github.event.pull_request.title }}", "title_link": "${{ github.event.pull_request.html_url }}", "text": "Failure Unit Test", "author_name": "${{ github.event.pull_request.user.login }}"}]}' \
${{ secrets.SLACK_WEBHOOK}} && exit 1
テストまでは前回と一緒なので、UnitTestから下を見てもらえると助かります。
細かい解説
成功した場合の処理
- name: Unit Test Success Notification
if: ${{ success() }}
run: |
curl \
-X POST \
-H "Content-Type: application/json" \
-d '{"attachments": [{"color": "#26A745", "title": "${{ github.event.pull_request.title }}", "title_link": "${{ github.event.pull_request.html_url }}", "text": "Success Unit Test", "author_name": "${{ github.event.pull_request.user.login }}"}]}' \
${{ secrets.SLACK_WEBHOOK}}
こちらが直前の処理が成功した場合の分岐です
if: ${{ success() }}
curlコマンドでincoming webhooksのAPIを叩いていきます。 HTTPメソッドはPOST、json形式で送ります [ペイロードはこちら](https://api.slack.com/messaging/composing/layouts#attachments)を参考にしました colorやらtitleやらを結構カスタムできるのでいいですね! 私の場合はtitleにPRのタイトル、title_linkはPRのリンク、author_nameはPRを出した人にしました。 今回はレビューがトリガーなのでGithubのWebhookの[ペイロードはこんな感じ](https://docs.github.com/ja/free-pro-team@latest/developers/webhooks-and-events/webhook-events-and-payloads#pull_request_review)
run: |
curl \
-X POST \
-H "Content-Type: application/json" \
-d '{"attachments": [{"color": "#26A745", "title": "${{ github.event.pull_request.title }}", "title_link": "${{ github.event.pull_request.html_url }}", "text": "Success Unit Test", "author_name": "${{ github.event.pull_request.user.login }}"}]}' \
${{ secrets.SLACK_WEBHOOK}}
失敗した場合の処理
- name: Unit Test Failure Notification
if: ${{ failure() }}
run: |
curl \
-X POST \
-H "Content-Type: application/json" \
-d '{"attachments": [{"color": "#D73A49", "title": "${{ github.event.pull_request.title }}", "title_link": "${{ github.event.pull_request.html_url }}", "text": "Failure Unit Test", "author_name": "${{ github.event.pull_request.user.login }}"}]}' \
${{ secrets.SLACK_WEBHOOK}} && exit 1
こちらが直前の処理が失敗した時の分岐
if: ${{ failure() }}
テストが成功したときは テスト成功→成功処理に行ってくれるのでいいですが、
失敗したときは テスト失敗→successをスキップ→失敗処理としてくれるか心配だったのですが、ただの杞憂でした 問題なかったです。(そりゃそう)
まとめ
今回はテスト後にslackに通知を送る方法を紹介しました。
前回の記事を見てもらえるとわかると思いますが、レビュー→テスト開始→slackに通知と来ました
その前にデプロイの方法も紹介してるので、次は自動でマージする方法です!
これでレビュー以外の全てのフローが自動化されますね!
それでは次の記事をお楽しみに