1
1

GithubActionsでワークフローの結果をSlack通知する方法

Last updated at Posted at 2024-05-31

概要

  • ソースコードの変更をトリガーにGithub ActionsでAWS Lambdaへの自動デプロイを行うフローを構築した。
  • デプロイの結果をActionsの画面で見るのは面倒なので、ジョブの結果を通知するGithbu Actionsを構築したので記録として残しておく
  • 今回の最終的な構成は下キャプチャのような構成となる
    image.png

対象

  • Github ActionsでSlack通知を実現したい方
  • 依存関係のあるJobがあり、同Jobの結果をもとに通知の内容を変更したいと思っている方

手順

Slack通知用のIncoming webhookを取得する

  • 通知する先のチャンネルを用意する

  • 用意したらチャンネルの右上(キャプチャ赤枠)のアイコンを押下する
    image.png

  • 「インテグレーション」のタブを押して「アプリを追加する」を押下する
    image.png

  • 「Incoming WebHooks」と入力して、キャプチャの赤枠をクリックする
    image.png

  • 「Slackに追加する」を押下する
    image.png

  • 該当のチャンネルを選択(クリックしても反応しないので、↓矢印を押すとチャンネルが出てくる)
    image.png

  • 追加するをクリックする
    image.png

  • Webhook URLを控えておく
    image.png

  • チャンネルに追加されたことを確認する
    image.png

通知するためのGithub Actionsを記述する

  • GitHub Actions Library にある Slack Notify を使用する
  • 本記事では、Slack Notifyを使用したslack_notify.ymlというファイルを作成し、モジュールとして呼び出す形で使用している(デプロイだけでなく、他の用途でもSlack通知のニーズがありそうなので、モジュールとして切り出し汎用的に使用できるようにする意図)
  • ポイント
yaml depoy_to_development.yml
name: Deploy to Development

on:
  push:
    branches:
      - main
    paths:
      - ".github/workflows/**"
      - "app/**"

permissions:
  id-token: write
  contents: read

jobs:
  # 何かしらの依存関係のあるjob
  deploy_api:
    uses: ./.github/workflows/xxxx.yml
    with:
      xxxx:xxxx
      xxxx:xxxx
    secrets: inherit

  # Slack通知処理を呼び出す
  slack_notification:
    # deploy_apiの後に実行されるようにする
    needs: deploy_api
    # deploy_apiが成功していても失敗していても実行されるようにする
    if: ${{ always() }}
    uses: ./.github/workflows/slack_notify.yml
    with:
      slack_channel: info-gha
      # needs.<job_id>.resultで前のジョブの結果を参照できる(success、failure、cancelled、および skipped)
      slack_color: ${{ needs.deploy_api.result == 'success' && 'good' || 'danger' }}
      slack_username: GitHub Actions Results
      slack_icon: https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png
      slack_title: Deployment to Development dev-qrcode-generator-lambda
      slack_message: ${{ needs.deploy_api.result == 'success' && ':white_check_mark:Deployment successful' || ':x:Deployment failed' }}
    secrets: inherit

  • ポイント
    • SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}で取得したSlackのIncoming WebHook URLを指定している
    • URLはGithub リポジトリのsecretsに格納している
    • secretsについてわからない方はこちらの方の記事にわかりやすく記載されているため、参照をお願いします。
    • 各変数の設定は、Slack Notifyの公式ページに詳しく載っています(キャプチャは公式ページから引用)
       image.png
yml slack_notify.yml
name: Slack Notification

on:
  workflow_call:
    inputs:
      slack_channel:
        description: "The Slack channel to send the notification to"
        required: true
        type: string
      slack_color:
        description: "The color to display in Slack"
        required: true
        type: string
      slack_username:
        description: "The username to display in Slack"
        required: true
        type: string
      slack_icon:
        description: "The icon to display in Slack"
        required: true
        type: string
      slack_title:
        description: "The title to display in Slack"
        required: true
        type: string
      slack_message:
        description: "The message to send to Slack"
        required: true
        type: string

jobs:
  slackNotification:
    name: Slack Notification
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Slack Notification
        uses: rtCamp/action-slack-notify@v2
        env:
          SLACK_CHANNEL: ${{ inputs.slack_channel }}
          SLACK_COLOR: ${{ inputs.slack_color }}
          SLACK_USERNAME: ${{ inputs.slack_username }}
          SLACK_ICON: ${{ inputs.slack_icon }}
          SLACK_TITLE: ${{ inputs.slack_title }}
          SLACK_MESSAGE: ${{ inputs.slack_message }}
          SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}

実行

  • needsを設定すると、Job間で線が結ばれ、順次実行される形となる
    image.png

  • Slack 通知

    • 実行者のGithubアカウントや実行されたワークフローのURL、トリガー対象のCommitのURLを自動で転記してくれるので便利

    • 依存関係のJob成功パターン
      image.png

    • 依存関係のJob失敗パターン
      image.png

まとめ

1
1
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
1
1