Ccmmutty logo
Commutty IT
1
0 pv2 min read

【GitHub Actions】デプロイの成功・失敗時にSlack通知を分ける

https://cdn.magicode.io/media/notebox/blob_4lBbzJ1
GitHub Actionsでデプロイの成功・失敗時のSlack通知を分けてみたのでコードを共有します!
name: Deploy

on:
  push:
    branches:
      - develop
  workflow_dispatch:

jobs:
  start:
    runs-on: ubuntu-latest
    steps:
    - name: Slack notification start
      uses: rtCamp/action-slack-notify@v2
      env:
        SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}
        SLACK_USERNAME: Github Actions
        SLACK_ICON_EMOJI: ':octocat:'
        SLACK_COLOR: ${{ job.status }}
        SLACK_MESSAGE: 'STARTED DEPLOY'

  deploy:
    runs-on: ubuntu-latest
    needs: start
    steps:
      - name: Deploy
        run: |
          # デプロイ処理

  finish:
    if: always()
    needs: deploy
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2

    - run: |
          MESSAGE='SUCCESSED DEPLOY'
          if [ "${{ needs.deploy.result }}" == "failure" ]; then
            MESSAGE='FAILURED DEPLOY <!channel>'
          fi
          echo "::set-output name=MESSAGE::$MESSAGE"
      id: success_or_failure

    - name: Slack notification finish on Success
      uses: rtCamp/action-slack-notify@v2
      env:
        SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}
        SLACK_USERNAME: Github Actions
        SLACK_ICON_EMOJI: ':octocat:'
        SLACK_COLOR: ${{ needs.deploy.result }}
        SLACK_MESSAGE: ${{ steps.success_or_failure.outputs.MESSAGE }}

Discussion

コメントにはログインが必要です。