LoginSignup
2
2

More than 3 years have passed since last update.

Github Actionsを使ってSlackに通知してみた話

Posted at

はじめに

前回の続きです
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形式で送ります
ペイロードはこちらを参考にしました
colorやらtitleやらを結構カスタムできるのでいいですね!
私の場合はtitleにPRのタイトル、title_linkはPRのリンク、author_nameはPRを出した人にしました。
今回はレビューがトリガーなのでGithubのWebhookのペイロードはこんな感じ

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に通知と来ました

その前にデプロイの方法も紹介してるので、次は自動でマージする方法です!
これでレビュー以外の全てのフローが自動化されますね!

それでは次の記事をお楽しみに

Github ActionsでDeployGateにデプロイした話
Github Actionsで自動テストした話

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2