Skip to content

Commit c0bbbbf

Browse files
Merge pull request #800 from NHSDigital/DTOSS-11196-slack-alert-integration
[DTOSS-11196] - feat(ci): Add Slack notifications for main branch CI/CD workflow failures
2 parents 458f8b8 + 5c1a3e2 commit c0bbbbf

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

.github/workflows/cicd-2-main-branch.yaml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,67 @@ jobs:
9292
environments: '["review","dev","preprod","prod"]'
9393
commit_sha: ${{ github.sha }}
9494
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

Comments
 (0)