|
92 | 92 | environments: '["review","dev","preprod","prod"]' |
93 | 93 | commit_sha: ${{ github.sha }} |
94 | 94 | secrets: inherit |
| 95 | + |
| 96 | + notify-failure: |
| 97 | + name: 'Notify failure' |
| 98 | + runs-on: ubuntu-latest |
| 99 | + if: ${{ failure() }} |
| 100 | + needs: [commit-stage, test-stage, build-stage, deploy-stage] |
| 101 | + steps: |
| 102 | + - name: Determine failed stages |
| 103 | + id: failed_stages |
| 104 | + run: | |
| 105 | + failed_stages="" |
| 106 | +
|
| 107 | + if [[ "${{ needs.commit-stage.result }}" == "failure" ]]; then |
| 108 | + failed_stages="${failed_stages}• Commit stage\n" |
| 109 | + fi |
| 110 | + if [[ "${{ needs.test-stage.result }}" == "failure" ]]; then |
| 111 | + failed_stages="${failed_stages}• Test stage\n" |
| 112 | + fi |
| 113 | + if [[ "${{ needs.build-stage.result }}" == "failure" ]]; then |
| 114 | + failed_stages="${failed_stages}• Build stage\n" |
| 115 | + fi |
| 116 | + if [[ "${{ needs.deploy-stage.result }}" == "failure" ]]; then |
| 117 | + failed_stages="${failed_stages}• Deploy stage (check workflow for environment)\n" |
| 118 | + fi |
| 119 | +
|
| 120 | + # Format for Slack (remove trailing newline) |
| 121 | + failed_stages=$(echo -e "$failed_stages" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}') |
| 122 | +
|
| 123 | + # Use delimiter for multiline output |
| 124 | + echo "stages<<EOF" >> $GITHUB_OUTPUT |
| 125 | + echo "$failed_stages" >> $GITHUB_OUTPUT |
| 126 | + echo "EOF" >> $GITHUB_OUTPUT |
| 127 | +
|
| 128 | + - name: Notify Slack |
| 129 | + uses: slackapi/slack-github-action@v2.1.1 |
| 130 | + with: |
| 131 | + webhook: ${{ secrets.SLACK_WEBHOOK_DEV_NOTIFICATIONS_URL }} |
| 132 | + webhook-type: incoming-webhook |
| 133 | + payload: | |
| 134 | + blocks: |
| 135 | + - type: section |
| 136 | + text: |
| 137 | + type: mrkdwn |
| 138 | + text: ":warning: Main branch CI/CD workflow failed:" |
| 139 | + - type: section |
| 140 | + fields: |
| 141 | + - type: mrkdwn |
| 142 | + text: |- |
| 143 | + *Workflow:* |
| 144 | + <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|${{ github.workflow }}> |
| 145 | + - type: mrkdwn |
| 146 | + text: |- |
| 147 | + *Commit:* |
| 148 | + <${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }}|${{ github.sha }}> |
| 149 | + - type: mrkdwn |
| 150 | + text: |- |
| 151 | + *Repo:* |
| 152 | + ${{ github.repository }} |
| 153 | + - type: section |
| 154 | + text: |
| 155 | + type: mrkdwn |
| 156 | + text: |- |
| 157 | + *Failed Stages:* |
| 158 | + ${{ steps.failed_stages.outputs.stages }} |
0 commit comments