-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·186 lines (142 loc) · 5.28 KB
/
run.sh
File metadata and controls
executable file
·186 lines (142 loc) · 5.28 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env bash
set -euo pipefail
run_id="${INPUT_RUN_ID}"
run_attempt="${INPUT_RUN_ATTEMPT}"
workflow_name="${INPUT_WORKFLOW_NAME}"
max_run_attempts="${INPUT_MAX_RUN_ATTEMPTS:-2}"
if [ -z "${GH_TOKEN:-}" ]; then
echo "GH_TOKEN is required." >&2
exit 1
fi
failed_jobs_csv=""
matched_jobs_csv=""
uninspectable_jobs_csv=""
csv_append() {
local current="$1"
local value="$2"
if [ -z "${current}" ]; then
printf '%s' "${value}"
return
fi
printf '%s,%s' "${current}" "${value}"
}
csv_to_summary_list() {
local csv="$1"
local rendered=()
local item=""
if [ -z "${csv}" ]; then
return
fi
IFS=',' read -r -a rendered <<< "${csv}"
for item in "${rendered[@]}"; do
printf '`%s`' "${item}"
if [ "${item}" != "${rendered[${#rendered[@]}-1]}" ]; then
printf ', '
fi
done
}
build_summary() {
local status="$1"
local lines=(
"## Transient Failure Retry Summary"
""
"- Workflow: \`${workflow_name}\`"
"- Run ID: \`${run_id}\`"
"- Run attempt: \`${run_attempt}\`"
"- Retry status: \`${status}\`"
)
if [ -n "${failed_jobs_csv}" ]; then
lines+=("- Failed jobs inspected: $(csv_to_summary_list "${failed_jobs_csv}")")
fi
if [ -n "${matched_jobs_csv}" ]; then
lines+=("- Jobs with transient GitHub failure signatures: $(csv_to_summary_list "${matched_jobs_csv}")")
fi
if [ -n "${uninspectable_jobs_csv}" ]; then
lines+=("- Failed jobs with unreadable logs: $(csv_to_summary_list "${uninspectable_jobs_csv}")")
fi
case "${status}" in
rerun-requested)
lines+=("- Action: Requested a rerun of failed jobs because every inspectable failed job matched transient GitHub-side error signatures.")
;;
skipped-run-attempt-limit)
lines+=("- Action: Skipped rerun because the workflow already reached the configured retry limit.")
;;
skipped-no-failed-jobs)
lines+=("- Action: Skipped rerun because the workflow reported failure without failed jobs to inspect.")
;;
skipped-no-transient-match)
lines+=("- Action: Skipped rerun because at least one failed job did not match the transient GitHub-side signatures.")
;;
skipped-uninspectable-logs)
lines+=("- Action: Skipped rerun because at least one failed job log could not be downloaded through the GitHub Actions API.")
;;
esac
printf '%s\n' "${lines[@]}"
}
write_summary_output() {
local summary="$1"
local delimiter="SUMMARY_$(date +%s%N)"
{
printf 'summary<<%s\n' "${delimiter}"
printf '%s\n' "${summary}"
printf '%s\n' "${delimiter}"
} >> "${GITHUB_OUTPUT}"
}
write_status_and_summary() {
local status="$1"
local summary
summary="$(build_summary "${status}")"
printf 'status=%s\n' "${status}" >> "${GITHUB_OUTPUT}"
write_summary_output "${summary}"
}
log_matches_transient_signature() {
local log_file="$1"
grep -Eiq \
"RPC failed; HTTP 5[0-9][0-9]|expected flush after ref listing|expected 'packfile'|remote:[[:space:]]+Internal Server Error|requested URL returned error:[[:space:]]*5[0-9][0-9]|fatal:[[:space:]]+unable to access 'https://github\\.com/.*': The requested URL returned error:[[:space:]]*5[0-9][0-9]" \
"${log_file}"
}
download_job_logs() {
local job_id="$1"
local output_file="$2"
curl \
-sS -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GH_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-o "${output_file}" \
-w '%{http_code}' \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/jobs/${job_id}/logs"
}
if [ "${run_attempt}" -ge "${max_run_attempts}" ]; then
write_status_and_summary "skipped-run-attempt-limit"
exit 0
fi
jobs_json="$(gh api "repos/${GITHUB_REPOSITORY}/actions/runs/${run_id}/jobs?per_page=100")"
failed_jobs_json="$(jq -c '.jobs[] | select(.conclusion == "failure")' <<< "${jobs_json}")"
if [ -z "${failed_jobs_json}" ]; then
write_status_and_summary "skipped-no-failed-jobs"
exit 0
fi
while IFS= read -r failed_job; do
[ -n "${failed_job}" ] || continue
job_id="$(jq -r '.id' <<< "${failed_job}")"
job_name="$(jq -r '.name' <<< "${failed_job}")"
failed_jobs_csv="$(csv_append "${failed_jobs_csv}" "${job_name}")"
temporary_log_file="$(mktemp)"
log_status_code="$(download_job_logs "${job_id}" "${temporary_log_file}")"
if [ "${log_status_code}" != "200" ]; then
uninspectable_jobs_csv="$(csv_append "${uninspectable_jobs_csv}" "${job_name} (${log_status_code})")"
rm -f "${temporary_log_file}"
write_status_and_summary "skipped-uninspectable-logs"
exit 0
fi
if ! log_matches_transient_signature "${temporary_log_file}"; then
rm -f "${temporary_log_file}"
write_status_and_summary "skipped-no-transient-match"
exit 0
fi
matched_jobs_csv="$(csv_append "${matched_jobs_csv}" "${job_name}")"
rm -f "${temporary_log_file}"
done <<< "${failed_jobs_json}"
gh api -X POST "repos/${GITHUB_REPOSITORY}/actions/runs/${run_id}/rerun-failed-jobs" >/dev/null
write_status_and_summary "rerun-requested"