-
Notifications
You must be signed in to change notification settings - Fork 1.2k
141 lines (122 loc) · 4.92 KB
/
Clean_workflows.yml
File metadata and controls
141 lines (122 loc) · 4.92 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
name: Clean Workflows
on:
workflow_dispatch:
inputs:
workflow_name:
type: choice
description: '清理的工作流名称'
required: true
default: 'Build SukiSU Ultra'
options:
- Build SukiSU Ultra
- Build KernelSU Next
- Build Kernel Only
- Build KernelSU Official
- Build SukiSU Ultra (40129)
- Build ReSukiSU
count:
description: '最多清理的运行次数'
required: false
default: '20'
delete_failed:
description: '是否删除失败的运行记录'
required: false
type: boolean
default: true
delete_success:
description: '是否删除成功的运行记录'
required: false
type: boolean
default: false
delete_cancelled:
description: '是否删除已取消的运行记录'
required: false
type: boolean
default: true
reverse_order:
description: '是否从旧到新开始清理'
required: false
type: boolean
default: true
permissions:
actions: write
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: 安装 GitHub CLI 和 jq
run: |
sudo apt-get update
sudo apt-get install -y gh jq
- name: 认证 GitHub CLI
run: echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token
- name: 清理指定工作流及本工作流运行记录
env:
REPO: ${{ github.repository }}
COUNT: ${{ inputs.count }}
DELETE_FAILED: ${{ inputs.delete_failed }}
DELETE_SUCCESS: ${{ inputs.delete_success }}
DELETE_CANCELLED: ${{ inputs.delete_cancelled }}
REVERSE_ORDER: ${{ inputs.reverse_order }}
WORKFLOW_NAME: ${{ inputs.workflow_name }}
CURRENT_RUN_ID: ${{ github.run_id }}
run: |
set -e
echo "当前仓库:$REPO"
echo "查找工作流 \"$WORKFLOW_NAME\" 的 ID..."
WORKFLOW_ID=$(gh api repos/$REPO/actions/workflows | jq -r ".workflows[] | select(.name == \"$WORKFLOW_NAME\") | .id")
if [ -z "$WORKFLOW_ID" ]; then
echo "未找到工作流 \"$WORKFLOW_NAME\",退出。"
exit 1
fi
echo "开始分页获取所有运行记录..."
PER_PAGE=100
PAGE=1
MATCHED_RUNS="[]"
while true; do
RESP=$(gh api "repos/$REPO/actions/workflows/$WORKFLOW_ID/runs?per_page=$PER_PAGE&page=$PAGE")
RUNS=$(echo "$RESP" | jq '.workflow_runs')
COUNT_THIS_PAGE=$(echo "$RUNS" | jq 'length')
if [ "$COUNT_THIS_PAGE" -eq 0 ]; then
break
fi
MATCHED_RUNS=$(jq -s 'add' <(echo "$MATCHED_RUNS") <(echo "$RUNS"))
PAGE=$((PAGE + 1))
done
echo "共获取到 $(echo "$MATCHED_RUNS" | jq 'length') 条运行记录。"
# 排序顺序:旧到新 or 新到旧
if [[ "$REVERSE_ORDER" == "true" ]]; then
SORTED_RUNS=$(echo "$MATCHED_RUNS" | jq 'sort_by(.run_started_at)')
else
SORTED_RUNS=$(echo "$MATCHED_RUNS" | jq 'sort_by(.run_started_at) | reverse')
fi
# 仅保留前 N 条
TO_DELETE_RUNS=$(echo "$SORTED_RUNS" | jq ".[0:${COUNT}]")
echo "$TO_DELETE_RUNS" | jq -c '.[]' | while read run; do
ID=$(echo "$run" | jq -r '.id')
STATUS=$(echo "$run" | jq -r '.conclusion')
STATE=$(echo "$run" | jq -r '.status')
if [[ "$STATE" == "in_progress" || "$STATE" == "queued" ]]; then
echo "跳过运行 ID: $ID(状态为 $STATE)"
continue
fi
if [[ "$STATUS" == "failure" && "$DELETE_FAILED" != "true" ]]; then continue; fi
if [[ "$STATUS" == "success" && "$DELETE_SUCCESS" != "true" ]]; then continue; fi
if [[ "$STATUS" == "cancelled" && "$DELETE_CANCELLED" != "true" ]]; then continue; fi
echo "删除运行记录 ID: $ID (状态: $STATUS)"
gh api -X DELETE "repos/$REPO/actions/runs/$ID" || echo "删除失败"
done
echo "清理本工作流运行记录..."
SELF_WORKFLOW_ID=$(gh api repos/$REPO/actions/workflows | jq -r '.workflows[] | select(.name == "清理工作流运行记录") | .id')
if [ -n "$SELF_WORKFLOW_ID" ]; then
SELF_RUNS=$(gh api "repos/$REPO/actions/workflows/$SELF_WORKFLOW_ID/runs?per_page=50" | jq -c '.workflow_runs[]')
echo "$SELF_RUNS" | while read run; do
ID=$(echo "$run" | jq -r '.id')
if [[ "$ID" == "$CURRENT_RUN_ID" ]]; then
echo "跳过当前运行 ID: $ID"
continue
fi
echo "删除本工作流运行记录 ID: $ID"
gh api -X DELETE "repos/$REPO/actions/runs/$ID" || echo "删除失败"
done
fi