0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GitHub Actionsを使用してJOBSのステータスをSlackに通知する

Last updated at Posted at 2024-01-23

開発プロジェクトでは、継続的インテグレーション(CI)や継続的デリバリー(CD)のプロセスが重要です。GitHub Actionsはこれらのプロセスを自動化するのに役立ちます。この記事では、GitHub Actionsを使って、ワークフローの実行結果をSlackに通知する方法を説明します。

必要なもの

  1. GitHubリポジトリ
  2. Slackワークスペース
  3. SlackでのIncoming Webhook URL

1: SlackでIncoming Webhookを設定する

  1. SlackのアプリディレクトリでIncoming Webhooksを検索し、インストールします。
  2. 新しいWebhookを作成し、通知を受け取りたいチャンネルを選択します。
  3. Webhook URLをコピーして安全な場所に保存します。

2: GitHubリポジトリにSecretを追加する

  1. GitHubリポジトリにアクセスします。
  2. 「Settings」→「Secrets」→「New repository secret」の順に進みます。
  3. 名前にSLACK_WEBHOOK_URLと入力し、値にSlackのWebhook URLを貼り付けます。
  4. 「Add secret」をクリックして保存します。

3: GitHub Actionsワークフローを作成する

.github/workflows ディレクトリに新しいYAMLファイルを作成します。

JOB実行ステータス種類
  1. success,
  2. failure,
  3. cancelled,
  4. skipped

以下は基本的なワークフローの例です。JOB 失敗成功 だけをSlackに通知する

....
jobs:
  job-one:
    runs-on: ubuntu-latest
    .....
  job-two:
    runs-on: ubuntu-latest
    .....
  job-three:
    runs-on: ubuntu-latest
    ..... 
  slack-notification:
    runs-on: ubuntu-latest
    needs:
      - job-one
      - job-two
      - job-three
    # success, failure, cancelled, or skipped
    if: always() && (failure() || success())
    steps:
      - name: "Send Notification to Slack"
        uses: rtCamp/action-slack-notify@v2
        env:
          SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}
          SLACK_CHANNEL: "your channel name"
          SLACK_USERNAME: "GitHub Actions"
          SLACK_COLOR: ${{ (needs.job-one.result == 'success' && needs.job-two.result == 'success' && needs.job-three.result == 'success') && 'good' || 'danger' }}
          SLACK_MESSAGE: ${{ (needs.job-one.result == 'success' && needs.job-two.result == 'success' && needs.job-three.result == 'success') && '実行成功。' || '<!channel> 実行失敗. ご確認ください。' }}
          SLACK_TITLE: "Slack Notification"
          SLACK_FOOTER:
            "Action URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}\n
            Branch URL: ${{ github.server_url }}/${{ github.repository }}/tree/${{ github.ref_name }}"
          SLACK_ICON: "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?