-
Notifications
You must be signed in to change notification settings - Fork 4
95 lines (86 loc) · 3.05 KB
/
ci-failure-email.yml
File metadata and controls
95 lines (86 loc) · 3.05 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
name: CI Failure Issue
on:
workflow_run:
workflows:
- ci
- Publish Package to npmjs
types: [completed]
jobs:
notify:
if: contains(fromJSON('["failure","timed_out","action_required"]'), github.event.workflow_run.conclusion)
permissions:
issues: write
runs-on: ubuntu-latest
steps:
- name: Create failure issue
env:
GH_TOKEN: ${{ github.token }}
REPOSITORY: ${{ github.repository }}
WORKFLOW_NAME: ${{ github.event.workflow_run.name }}
CONCLUSION: ${{ github.event.workflow_run.conclusion }}
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
ACTOR: ${{ github.event.workflow_run.actor.login }}
RUN_URL: ${{ github.event.workflow_run.html_url }}
run: |
reported_at="$(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S %z')"
issue_title_prefix="[CI failed] $REPOSITORY / $WORKFLOW_NAME at "
body_file="$(mktemp)"
cat > "$body_file" <<EOF
@sunnylqm CI run failed.
Repository: $REPOSITORY
Workflow: $WORKFLOW_NAME
Conclusion: $CONCLUSION
Branch: $HEAD_BRANCH
Commit: $HEAD_SHA
Actor: $ACTOR
Reported at: $reported_at
Run: $RUN_URL
EOF
gh issue create \
--repo "$REPOSITORY" \
--title "$issue_title_prefix$reported_at" \
--body-file "$body_file"
close:
if: github.event.workflow_run.conclusion == 'success'
permissions:
issues: write
runs-on: ubuntu-latest
steps:
- name: Close recovered failure issues
env:
GH_TOKEN: ${{ github.token }}
REPOSITORY: ${{ github.repository }}
WORKFLOW_NAME: ${{ github.event.workflow_run.name }}
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
RUN_URL: ${{ github.event.workflow_run.html_url }}
run: |
issue_title_prefix="[CI failed] $REPOSITORY / $WORKFLOW_NAME at "
fixed_at="$(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S %z')"
issue_numbers="$(
gh issue list \
--repo "$REPOSITORY" \
--state open \
--limit 1000 \
--json number,title |
jq -r --arg prefix "$issue_title_prefix" '.[] | select(.title | startswith($prefix)) | .number'
)"
if [ -z "$issue_numbers" ]; then
exit 0
fi
comment_body="$(cat <<EOF
CI recovered at $fixed_at.
Repository: $REPOSITORY
Workflow: $WORKFLOW_NAME
Branch: $HEAD_BRANCH
Commit: $HEAD_SHA
Run: $RUN_URL
EOF
)"
while IFS= read -r issue_number; do
[ -n "$issue_number" ] || continue
gh issue close "$issue_number" \
--repo "$REPOSITORY" \
--comment "$comment_body"
done <<< "$issue_numbers"