Notify #357
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Notify | |
| # Centralized failure notifier. Rather than sprinkling `if: failure()` steps | |
| # across every job in every workflow, this watches the other workflows finish | |
| # and pings Discord whenever one of them concludes in failure. It therefore | |
| # also covers any jobs/workflows added later, with no further wiring. | |
| # | |
| # NOTE: `workflow_run` triggers are evaluated from the workflow file on the | |
| # repository's default branch, so this only takes effect once merged to main. | |
| on: | |
| workflow_run: | |
| workflows: [ "CI", "Release", "Docs", "Performance" ] | |
| types: [ completed ] | |
| permissions: | |
| contents: read | |
| jobs: | |
| discord: | |
| runs-on: ubuntu-latest | |
| # Only alert on genuine failures of non-PR runs. Pull-request failures are | |
| # already visible as checks on the PR, so excluding them keeps the channel | |
| # signal-to-noise high. `timed_out` is treated as a failure; `cancelled` | |
| # is usually intentional and is ignored. | |
| if: > | |
| (github.event.workflow_run.conclusion == 'failure' || | |
| github.event.workflow_run.conclusion == 'timed_out') && | |
| github.event.workflow_run.event != 'pull_request' | |
| steps: | |
| - name: Post failure to Discord | |
| env: | |
| DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} | |
| WF_NAME: ${{ github.event.workflow_run.name }} | |
| WF_CONCLUSION: ${{ github.event.workflow_run.conclusion }} | |
| WF_EVENT: ${{ github.event.workflow_run.event }} | |
| WF_BRANCH: ${{ github.event.workflow_run.head_branch }} | |
| WF_SHA: ${{ github.event.workflow_run.head_sha }} | |
| WF_ACTOR: ${{ github.event.workflow_run.actor.login }} | |
| WF_URL: ${{ github.event.workflow_run.html_url }} | |
| REPO: ${{ github.repository }} | |
| run: | | |
| if [ -z "$DISCORD_WEBHOOK_URL" ]; then | |
| echo "::warning::DISCORD_WEBHOOK_URL secret is not set; skipping notification." | |
| exit 0 | |
| fi | |
| SHORT_SHA="${WF_SHA:0:7}" | |
| CONTENT=$(printf '🔴 **%s** %s on `%s`\nRepo: `%s` · Trigger: `%s` · By: `%s` · Commit: `%s`\n%s' \ | |
| "$WF_NAME" "$WF_CONCLUSION" "$WF_BRANCH" \ | |
| "$REPO" "$WF_EVENT" "$WF_ACTOR" "$SHORT_SHA" "$WF_URL") | |
| # Build the JSON body with jq so branch/actor/commit values can never | |
| # break the payload, then POST it to the Discord webhook. | |
| jq -n --arg content "$CONTENT" '{content: $content}' \ | |
| | curl -fsS -H "Content-Type: application/json" -d @- "$DISCORD_WEBHOOK_URL" |