This repository was archived by the owner on Jan 29, 2026. It is now read-only.
forked from Laomaoi/Action-KernelSU-Next
-
Notifications
You must be signed in to change notification settings - Fork 205
191 lines (169 loc) · 6.79 KB
/
delete.yml
File metadata and controls
191 lines (169 loc) · 6.79 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
187
188
189
190
191
name: 清理工作流运行记录和缓存
on:
workflow_dispatch:
inputs:
workflow_name:
type: choice
description: '清理的工作流名称'
required: true
default: 'Build KernelSU Next SUSFS All'
options:
- Build KernelSU Next SUSFS All
- Build TEST
- Build TEST2
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: false
reverse_order:
description: '从旧到新开始清理?'
required: false
type: boolean
default: false
clear_cache:
description: '是否删除所有ccache缓存?'
required: false
type: boolean
default: false
cancel_all:
description: '是否一键取消指定工作流的所有运行?'
required: false
type: boolean
default: false
permissions:
actions: write
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: 安装环境
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 }}
CANCEL_ALL: ${{ inputs.cancel_all }}
WORKFLOW_NAME: ${{ inputs.workflow_name }}
CURRENT_RUN_ID: ${{ github.run_id }}
run: |
set -e
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
ALL_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
ALL_RUNS=$(jq -s 'add' <(echo "$ALL_RUNS") <(echo "$RUNS"))
PAGE=$((PAGE + 1))
done
echo "共获取到 $(echo "$ALL_RUNS" | jq 'length') 条运行记录。"
if [[ "$CANCEL_ALL" == "true" ]]; then
echo "开始取消进行中或排队中的运行..."
CANCEL_COUNT=0
echo "$ALL_RUNS" | jq -c '.[] | select(.status == "in_progress" or .status == "queued")' | while read run; do
ID=$(echo "$run" | jq -r '.id')
if [[ "$ID" == "$CURRENT_RUN_ID" ]]; then continue; fi
echo "取消运行 ID: $ID"
gh api -X POST "repos/$REPO/actions/runs/$ID/cancel" && CANCEL_COUNT=$((CANCEL_COUNT + 1))
done
echo "已取消 $CANCEL_COUNT 条运行。"
fi
echo "开始处理删除逻辑..."
if [[ "$REVERSE_ORDER" == "true" ]]; then
SORTED_RUNS=$(echo "$ALL_RUNS" | jq 'sort_by(.run_started_at)')
else
SORTED_RUNS=$(echo "$ALL_RUNS" | jq 'sort_by(.run_started_at) | reverse')
fi
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 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"
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 continue; fi
echo "删除本工作流运行记录 ID: $ID"
gh api -X DELETE "repos/$REPO/actions/runs/$ID" || echo "删除失败"
done
fi
clean-caches:
runs-on: ubuntu-latest
needs: cleanup
if: ${{ inputs.clear_cache == true || inputs.clear_cache == 'true' }}
permissions:
actions: write
steps:
- name: 删除并重置缓存
uses: actions/github-script@v6
with:
script: |
const { owner, repo } = context.repo;
let totalDeleted = 0;
let page = 1;
while (true) {
const res = await github.rest.actions.getActionsCacheList({
owner,
repo,
per_page: 100,
page: page
});
const caches = res.data.actions_caches;
if (!caches || caches.length === 0) break;
for (const cache of caches) {
if (cache.key.startsWith('ccache-')) {
console.log(`删除缓存: ${cache.key}`);
await github.rest.actions.deleteActionsCacheById({
owner,
repo,
cache_id: cache.id
});
totalDeleted++;
}
}
if (caches.length < 100) break;
page++;
}
console.log(`共删除缓存数量: ${totalDeleted}`);