-
Notifications
You must be signed in to change notification settings - Fork 800
163 lines (156 loc) · 6.45 KB
/
slack-workflow-failed.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
name: CI Results
run-name: ${{ github.event.workflow.name }} on ${{ github.event.workflow_run.head_branch }}
on:
workflow_run:
types: [ 'completed' ]
workflows:
- Build
- Build Docker
- Tests
- Gardening
- Monorepo Auto-tagger
- Post-Build
- PR is up-to-date
- Update Jetpack Staging Test Sites
- Update Phan stubs
- k6 Tests for Jetpack Staging Test Sites
branches: [ 'trunk', 'prerelease', '*/branch-*' ]
jobs:
notify:
name: Notify failure
runs-on: ubuntu-latest
if: ${{ github.repository == 'Automattic/jetpack' && github.event.workflow_run.conclusion == 'failure' && github.event.workflow_run.event != 'issues' && github.event.workflow_run.event != 'issue_comment' }}
steps:
- uses: actions/checkout@v4
- name: Generate message
id: message
run: |
source .github/files/gh-funcs.sh
# If this is triggered off of another "workflow_run" workflow, it'll always see the head_branch as "trunk".
# In that case, we assume it's setting display_title to be like "NAME on BRANCH" and extract the branch from there.
if jq -e '.workflow_run.event == "workflow_run"' "$GITHUB_EVENT_PATH" &>/dev/null; then
TITLE=$(jq -r '.workflow_run.display_title' "$GITHUB_EVENT_PATH")
BRANCH=${TITLE##* on }
echo "branch=$BRANCH" >> "$GITHUB_OUTPUT"
DATA=$( jq -nc --slurpfile event "$GITHUB_EVENT_PATH" --arg branch "$BRANCH" '$event[0] as $e | $e.workflow_run as $run |
{
workflow_name: $e.workflow.name,
branch: $branch,
run_id: $run.id,
run_url: $run.html_url,
duration: ( ( $run.updated_at | fromdateiso8601 ) - ( $run.run_started_at | fromdateiso8601 ) | strftime( "%H:%M:%S" ) ),
}
' )
else
BRANCH=$(jq -r '.workflow_run.head_branch' "$GITHUB_EVENT_PATH")
echo "branch=$BRANCH" >> "$GITHUB_OUTPUT"
DATA=$( jq -nc --slurpfile event "$GITHUB_EVENT_PATH" '$event[0] as $e | $e.workflow_run as $run |
( $run.head_commit.message | sub( "\n.*"; ""; "m" ) | match( " \\(#([0-9]+)\\)$" ).captures[0].string // null ) as $pr |
{
workflow_name: $e.workflow.name,
branch: $run.head_branch,
run_id: $run.id,
run_url: $run.html_url,
duration: ( ( $run.updated_at | fromdateiso8601 ) - ( $run.run_started_at | fromdateiso8601 ) | strftime( "%H:%M:%S" ) ),
commit: "\( $run.head_commit.id[0:8] ) \( $run.head_commit.message | sub( "\n.*"; ""; "m" ) )",
author: ( $run.head_commit.author.name ),
commit_url: "\( $run.head_repository.html_url )/commit/\( $run.head_commit.id )",
pr: $pr,
pr_url: ( if $pr then "\( $run.head_repository.html_url )/pull/\( $pr )" else null end )
}
' )
fi
jq -r '[
"### \( .workflow_name ) failed on \( .branch )",
"",
if .commit then "Commit: \( .commit )" else empty end,
if .author then "Author: \( .author )" else empty end,
"Duration: \( .duration )",
"",
(
[
"[Run #\( .run_id )](\( .run_url ))",
if .commit_url then "[Commit](\( .commit_url ))" else empty end,
if .pr then "[PR #\( .pr )](\( .pr_url ))" else empty end
] | join( " | " )
)
] | join( "\n" )' <<<"$DATA" >> $GITHUB_STEP_SUMMARY
gh_set_output message "$( jq -c '
{
icon_emoji: ":github-rejected:",
text: "*\( .workflow_name ) failed on \( .branch )*\n<\( .run_url )|Run #\( .run_id )>",
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: "*\( .workflow_name ) failed on \( .branch )*"
}
},
{
type: "context",
elements: [
if .commit then {
type: "plain_text",
text: "Commit: \( .commit )",
emoji: false
} else empty end,
if .author then {
type: "plain_text",
text: "Author: \( .author )",
emoji: false
} else empty end,
{
type: "plain_text",
text: "Duration: \( .duration )"
}
]
},
{
type: "actions",
elements: [
{
type: "button",
text: {
type: "plain_text",
text: "Run #\( .run_id )"
},
url: .run_url
},
if .commit_url then {
type: "button",
text: {
type: "plain_text",
text: "Commit"
},
url: .commit_url
} else empty end,
if .pr then {
type: "button",
text: {
type: "plain_text",
text: "PR #\( .pr )"
},
url: .pr_url,
} else empty end
]
}
]
}
' <<<"$DATA" )"
- name: Send message to alerts channel
uses: slackapi/slack-github-action@v1.25.0
if: steps.message.outputs.branch == 'trunk'
with:
channel-id: ${{ secrets.SLACK_JP_ALERTS_CHANNEL }}
payload: ${{ steps.message.outputs.message }}
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_TOKEN }}
- name: Send message to releases channel
uses: slackapi/slack-github-action@v1.25.0
if: contains( steps.message.outputs.branch, '/branch-' ) || steps.message.outputs.branch == 'prerelease'
with:
channel-id: ${{ secrets.SLACK_RELEASES_CHANNEL }}
payload: ${{ steps.message.outputs.message }}
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_TOKEN }}