概要
- ソースコードの変更をトリガーにGithub ActionsでAWS Lambdaへの自動デプロイを行うフローを構築した。
- デプロイの結果をActionsの画面で見るのは面倒なので、ジョブの結果を通知するGithbu Actionsを構築したので記録として残しておく
- 今回の最終的な構成は下キャプチャのような構成となる
対象
- Github ActionsでSlack通知を実現したい方
- 依存関係のあるJobがあり、同Jobの結果をもとに通知の内容を変更したいと思っている方
手順
Slack通知用のIncoming webhookを取得する
-
通知する先のチャンネルを用意する
通知するためのGithub Actionsを記述する
- GitHub Actions Library にある
Slack Notify
を使用する - 本記事では、
Slack Notify
を使用したslack_notify.ymlというファイルを作成し、モジュールとして呼び出す形で使用している(デプロイだけでなく、他の用途でもSlack通知のニーズがありそうなので、モジュールとして切り出し汎用的に使用できるようにする意図) - ポイント
- needsコンテキストを使用して、Jobの依存関係を作る
-
[jobs.<job_id>.if]
で依存関係のJobの結果次第で後続の処理の実行を判定している- ifを使用できるコンテキストの種類:github, needs, vars, inputs
- 実行可能な関数:always, cancelled, success, failure
- https://docs.github.com/ja/actions/learn-github-actions/contexts#context-availability
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
の公式ページに詳しく載っています(キャプチャは公式ページから引用)
-
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 }}
実行
-
Slack 通知
まとめ
- ライブラリを使って簡単に作成できた
- ニーズに応じて色々拡張できそうだと思った
- 今回指定したライブラリのオプション以外にもさまざまなオプションを指定できるので、時間ができたら試そうと思った
- 今回作成したコード