Skip to content

Commit 1ddef56

Browse files
Add Slack notification for CI failures (#816)
### What Add a notification job to the CI workflow that sends Slack messages when builds fail on the main branch. Implement caching to prevent duplicate notifications for the same image configuration. ### Why Teams need immediate visibility into CI failures on the main branch to respond quickly to broken builds without being spammed by repeated notifications.
1 parent 80c5bcd commit 1ddef56

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

.github/workflows/ci.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,62 @@ jobs:
138138
uses: ./.github/workflows/internal-action-test.yml
139139
with:
140140
tag: ${{ needs.setup.outputs.tag-prefix }}${{ fromJSON(needs.setup.outputs.tags)[0] }}
141+
142+
notify:
143+
name: 7 slack notify on failure
144+
if: always() && github.ref == 'refs/heads/main' && (github.event_name == 'push' || github.event_name == 'schedule') && contains(needs.*.result, 'failure')
145+
needs: [setup, build, test, push]
146+
runs-on: ubuntu-latest
147+
continue-on-error: true
148+
steps:
149+
- name: Generate cache key from images config
150+
id: cache_key
151+
env:
152+
images: ${{ needs.setup.outputs.images }}
153+
run: |
154+
cache_key=$(<<< $images jq -cS '.' | sha256sum | awk '{print $1}')
155+
echo "key=notify-$cache_key" >> $GITHUB_OUTPUT
156+
- name: Restore notification cache
157+
id: cache_restore
158+
uses: actions/cache/restore@v4
159+
with:
160+
path: .notify_cache
161+
key: ${{ steps.cache_key.outputs.key }}
162+
- name: Send Slack notification
163+
if: steps.cache_restore.outputs.cache-hit != 'true'
164+
env:
165+
slack_webhook_url: ${{ secrets.SLACK_WEBHOOK_URL }}
166+
event: ${{ github.event_name }}
167+
sha: ${{ github.sha }}
168+
build_emoji: ${{ needs.build.result == 'success' && '✅' || needs.build.result == 'failure' && '❌' || '❓' }}
169+
test_emoji: ${{ needs.test.result == 'success' && '✅' || needs.test.result == 'failure' && '❌' || '❓' }}
170+
push_emoji: ${{ needs.push.result == 'success' && '✅' || needs.push.result == 'failure' && '❌' || '❓' }}
171+
commit_url: https://github.com/${{ github.repository }}/commit/${{ github.sha }}
172+
run_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
173+
run: |
174+
short_sha="${sha:0:7}"
175+
176+
payload="$(jq -n \
177+
--arg build "$build_emoji" \
178+
--arg test "$test_emoji" \
179+
--arg push "$push_emoji" \
180+
--arg event "$event" \
181+
--arg short_sha "$short_sha" \
182+
--arg commit_url "$commit_url" \
183+
--arg run_url "$run_url" \
184+
'{text: ":red_circle: • build \($build) • test \($test) • push \($push) • <\($commit_url)|\($short_sha)> • <\($run_url)|run> • \($event)"}')"
185+
186+
curl -X POST "$slack_webhook_url" \
187+
-H 'Content-Type: application/json' \
188+
-d "$payload"
189+
- name: Create cache file
190+
if: steps.cache_restore.outputs.cache-hit != 'true'
191+
run: |
192+
mkdir -p .notify_cache
193+
echo "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" > .notify_cache/timestamp
194+
- name: Save notification cache
195+
if: steps.cache_restore.outputs.cache-hit != 'true'
196+
uses: actions/cache/save@v4
197+
with:
198+
path: .notify_cache
199+
key: ${{ steps.cache_key.outputs.key }}

0 commit comments

Comments
 (0)