@@ -10,13 +10,61 @@ inputs:
1010 default : CG5MJHMB2 # docs-alerts
1111 required : false
1212 message :
13- description : The message to send to Slack
14- default : The last '${{ github.workflow }}' run failed. See ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
13+ description : >-
14+ Optional message override. When set, it is sent verbatim. When empty (the
15+ default), a standard multi-line failure message is built from the run
16+ context, plus a link to the failure issue if issue_url is provided.
17+ default : ' '
18+ required : false
19+ issue_url :
20+ description : >-
21+ Optional link to the tracking failure issue (e.g. the output of the
22+ create-workflow-failure-issue action). Appended to the default message.
23+ Ignored when a custom message is provided.
24+ default : ' '
1525 required : false
1626
1727runs :
1828 using : composite
1929 steps :
30+ # Build the Slack text here so the default message can be multi-line (real
31+ # newlines) and conditionally include the issue link. A caller-supplied
32+ # message is passed through verbatim for backward compatibility.
33+ - name : Build Slack message
34+ id : build
35+ shell : bash
36+ env :
37+ MESSAGE : ${{ inputs.message }}
38+ ISSUE_URL : ${{ inputs.issue_url }}
39+ SOURCE_REPO : ${{ github.repository }}
40+ WORKFLOW_NAME : ${{ github.workflow }}
41+ RUN_URL : ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
42+ EVENT_NAME : ${{ github.event_name }}
43+ GIT_REF : ${{ github.ref }}
44+ ACTOR : ${{ github.actor }}
45+ run : |
46+ # Escape Slack mrkdwn control chars in interpolated context fields so a
47+ # crafted branch/ref (e.g. containing <!channel>) can't inject mentions.
48+ esc() { printf '%s' "$1" | sed -e 's/&/\&/g' -e 's/</\</g' -e 's/>/\>/g'; }
49+ # Unique heredoc delimiter so a custom message can't collide with it.
50+ delim="SLACK_EOF_${RANDOM}${RANDOM}"
51+ {
52+ printf 'text<<%s\n' "$delim"
53+ if [ -n "$MESSAGE" ]; then
54+ printf '%s\n' "$MESSAGE"
55+ else
56+ printf ':actions: *Workflow failure* in %s: %s\n' "$(esc "$SOURCE_REPO")" "$(esc "$WORKFLOW_NAME")"
57+ printf 'on %s · %s · by %s\n' "$(esc "$EVENT_NAME")" "$(esc "$GIT_REF")" "$(esc "$ACTOR")"
58+ printf 'Run: %s\n' "$RUN_URL"
59+ if [ -n "$ISSUE_URL" ]; then
60+ printf 'Issue: %s\n' "$ISSUE_URL"
61+ else
62+ printf ':warning: No issue created\n'
63+ fi
64+ fi
65+ printf '%s\n' "$delim"
66+ } >> "$GITHUB_OUTPUT"
67+
2068 - name: Send Slack notification if workflow fails
2169 uses: slackapi/slack-github-action@45a88b9581bfab2566dc881e2cd66d334e621e2c # v3.0.3
2270 with:
2573 errors: true
2674 payload: |
2775 channel: ${{ toJSON(inputs.slack_channel_id) }}
28- text: ${{ toJSON(inputs.message ) }}
76+ text: ${{ toJSON(steps.build.outputs.text ) }}
0 commit comments