-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathclean_failed_cancelled_runs.yml
More file actions
61 lines (56 loc) · 2.43 KB
/
clean_failed_cancelled_runs.yml
File metadata and controls
61 lines (56 loc) · 2.43 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
name: Clean Failed/Cancelled Workflow Runs
on:
workflow_dispatch:
permissions:
contents: read
jobs:
clean-failed-cancelled:
if: github.repository == 'Aethersailor/Custom_OpenClash_Rules'
runs-on: ubuntu-latest
steps:
- name: Delete all failed and cancelled workflow runs
env:
GH_PAT: ${{ secrets.GH_PAT }}
REPO: ${{ github.repository }}
WORKFLOW_NAME: ${{ github.workflow }}
run: |
echo "Fetching failed and cancelled workflow runs for $REPO"
page=1
while :; do
response=$(curl -s -H "Authorization: token $GH_PAT" \
"https://api.github.com/repos/$REPO/actions/runs?per_page=100&page=$page")
run_count=$(echo "$response" | jq '.workflow_runs | length')
if [ "$run_count" -eq 0 ]; then
break
fi
runs=$(echo "$response" | jq '.workflow_runs[] | select(.conclusion=="failure" or .conclusion=="cancelled") | .id')
for run_id in $runs; do
echo "Deleting run $run_id"
curl -s -X DELETE -H "Authorization: token $GH_PAT" \
"https://api.github.com/repos/$REPO/actions/runs/$run_id"
done
page=$((page+1))
done
# 额外:删除本 workflow 的所有 run(包括成功的)
echo "Deleting all runs of this workflow itself (including successful runs)..."
workflow_id=$(curl -s -H "Authorization: token $GH_PAT" \
"https://api.github.com/repos/$REPO/actions/workflows" | jq ".workflows[] | select(.name == \"$WORKFLOW_NAME\") | .id")
if [ -n "$workflow_id" ]; then
page=1
while :; do
response=$(curl -s -H "Authorization: token $GH_PAT" \
"https://api.github.com/repos/$REPO/actions/workflows/$workflow_id/runs?per_page=100&page=$page")
run_count=$(echo "$response" | jq '.workflow_runs | length')
if [ "$run_count" -eq 0 ]; then
break
fi
runs=$(echo "$response" | jq '.workflow_runs[] | .id')
for run_id in $runs; do
echo "Deleting self run $run_id"
curl -s -X DELETE -H "Authorization: token $GH_PAT" \
"https://api.github.com/repos/$REPO/actions/runs/$run_id"
done
page=$((page+1))
done
fi
echo "All done."