-
Notifications
You must be signed in to change notification settings - Fork 3.2k
319 lines (292 loc) · 11.3 KB
/
delete.yml
File metadata and controls
319 lines (292 loc) · 11.3 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
name: Workflow and Cache Cleanup
on:
workflow_dispatch:
inputs:
workflow_name:
type: choice
description: '清理的工作流名称'
required: true
default: 'Build All OnePlus Kernels'
options:
- Build All OnePlus Kernels
- CAll Build Start UP
- Build TEST
- Build TEST2
cache_mode:
type: choice
description: '缓存清理类型'
required: false
default: 'none'
options:
- none
- all
- ccache
- apt-cache
count:
description: '最多访问的运行次数'
required: false
default: '20'
delete_failed:
description: '删除指定工作流失败的运行记录?'
required: false
type: boolean
default: false
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
cancel_all:
description: '是否一键取消指定工作流的所有运行?'
required: false
type: boolean
default: false
target_run_numbers:
description: '按运行编号删除'
required: false
default: ''
target_run_ids:
description: '按Run-ID删除'
required: false
default: ''
permissions:
actions: write
jobs:
cleanup-workflow:
runs-on: ubuntu-latest
if: >-
${{ inputs.cancel_all
|| inputs.delete_failed
|| inputs.delete_success
|| inputs.delete_cancelled
|| inputs.target_run_numbers != ''
|| inputs.target_run_ids != '' }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
steps:
- name: Delete by Run Number
if: ${{ inputs.target_run_numbers != '' }}
env:
WORKFLOW_NAME: ${{ inputs.workflow_name }}
TARGET_RUN_NUMBERS: ${{ inputs.target_run_numbers }}
run: |
set -e
echo "精准模式 - 按运行编号删除: $TARGET_RUN_NUMBERS"
IFS=',' read -ra NUM_LIST <<< "$TARGET_RUN_NUMBERS"
for RAW_NUM in "${NUM_LIST[@]}"; do
NUM=$(echo "$RAW_NUM" | tr -d ' #')
[ -z "$NUM" ] && continue
echo "查找运行编号 #${NUM}..."
LINE=$(gh run list \
--repo "$REPO" \
--workflow "$WORKFLOW_NAME" \
--limit 300 \
--json databaseId,number,status,conclusion \
--jq ".[] | select(.number == $NUM) | [.status, .conclusion, (.databaseId | tostring)] | join(\" \")")
if [ -z "$LINE" ]; then
echo "⚠️ 未找到运行编号 #${NUM},跳过。"
continue
fi
STATE=$(echo "$LINE" | awk '{print $1}')
CONCLUSION=$(echo "$LINE" | awk '{print $2}')
RUN_ID=$(echo "$LINE" | awk '{print $3}')
if [[ "$STATE" == "in_progress" || "$STATE" == "queued" || "$STATE" == "waiting" ]]; then
echo "#${NUM}(ID: $RUN_ID)仍在运行中($STATE),跳过。"
else
echo "删除 #${NUM}(ID: $RUN_ID,conclusion: $CONCLUSION)..."
gh run delete "$RUN_ID" --repo "$REPO" \
&& echo "✅ 已删除 #${NUM}" \
|| echo "❌ 删除失败 #${NUM}"
fi
done
- name: Delete by Run ID
if: ${{ inputs.target_run_ids != '' }}
env:
TARGET_RUN_IDS: ${{ inputs.target_run_ids }}
run: |
set -e
echo "精准模式 - 按 Run ID 删除: $TARGET_RUN_IDS"
IFS=',' read -ra ID_LIST <<< "$TARGET_RUN_IDS"
for RAW_ID in "${ID_LIST[@]}"; do
RID=$(echo "$RAW_ID" | tr -d ' ')
[ -z "$RID" ] && continue
echo "查询 Run ID $RID..."
LINE=$(gh run view "$RID" \
--repo "$REPO" \
--json status,conclusion,number \
--jq '[.status, .conclusion, (.number | tostring)] | join(" ")' \
2>/dev/null || true)
if [ -z "$LINE" ]; then
echo "⚠️ Run ID $RID 不存在或无权访问,跳过。"
continue
fi
STATE=$(echo "$LINE" | awk '{print $1}')
CONCLUSION=$(echo "$LINE" | awk '{print $2}')
RUN_NUMBER=$(echo "$LINE" | awk '{print $3}')
if [[ "$STATE" == "in_progress" || "$STATE" == "queued" || "$STATE" == "waiting" ]]; then
echo "ID $RID(#${RUN_NUMBER})仍在运行中($STATE),跳过。"
else
echo "删除 ID $RID(#${RUN_NUMBER},conclusion: $CONCLUSION)..."
gh run delete "$RID" --repo "$REPO" \
&& echo "✅ 已删除 ID $RID" \
|| echo "❌ 删除失败 ID $RID"
fi
done
- name: Batch Cancel and Delete
if: >-
${{ inputs.target_run_numbers == ''
&& inputs.target_run_ids == ''
&& (inputs.cancel_all
|| inputs.delete_failed
|| inputs.delete_success
|| inputs.delete_cancelled) }}
env:
WORKFLOW_NAME: ${{ inputs.workflow_name }}
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 }}
CURRENT_RUN_ID: ${{ github.run_id }}
run: |
set -e
if [[ "$CANCEL_ALL" == "true" ]]; then
echo "批量模式 - 取消所有进行中/排队中的运行..."
CANCEL_COUNT=0
for STATUS_FILTER in in_progress queued waiting; do
while IFS= read -r RUN_ID; do
[ -z "$RUN_ID" ] && continue
[[ "$RUN_ID" == "$CURRENT_RUN_ID" ]] && continue
echo "取消 ID: $RUN_ID"
gh run cancel "$RUN_ID" --repo "$REPO" \
&& CANCEL_COUNT=$((CANCEL_COUNT + 1)) \
|| echo "取消失败: $RUN_ID"
done < <(gh run list \
--repo "$REPO" \
--workflow "$WORKFLOW_NAME" \
--status "$STATUS_FILTER" \
--limit 200 \
--json databaseId \
--jq '.[].databaseId | tostring')
done
echo "已取消 $CANCEL_COUNT 条运行。"
fi
[[ "$REVERSE_ORDER" == "true" ]] && SORT_OPT="" || SORT_OPT="-r"
for CONCLUSION in failure success cancelled; do
case "$CONCLUSION" in
failure) [[ "$DELETE_FAILED" != "true" ]] && continue ;;
success) [[ "$DELETE_SUCCESS" != "true" ]] && continue ;;
cancelled) [[ "$DELETE_CANCELLED" != "true" ]] && continue ;;
esac
echo "批量模式 - 处理结论类型: $CONCLUSION(最多 $COUNT 条)..."
DELETED=0
while IFS=' ' read -r RUN_ID _STARTED; do
[ -z "$RUN_ID" ] && continue
[[ "$RUN_ID" == "$CURRENT_RUN_ID" ]] && continue
[ "$DELETED" -ge "$COUNT" ] && break
echo "删除 ID: $RUN_ID"
gh run delete "$RUN_ID" --repo "$REPO" \
&& DELETED=$((DELETED + 1)) \
|| echo "❌ 删除失败: $RUN_ID"
done < <(gh run list \
--repo "$REPO" \
--workflow "$WORKFLOW_NAME" \
--status "$CONCLUSION" \
--limit "$COUNT" \
--json databaseId,startedAt \
--jq '.[] | [(.databaseId | tostring), .startedAt] | join(" ")' \
| sort $SORT_OPT -k2)
echo "已删除 $DELETED 条 $CONCLUSION 记录。"
done
cleanup-caches:
runs-on: ubuntu-latest
if: ${{ inputs.cache_mode != 'none' }}
permissions:
actions: write
steps:
- name: Clean caches by mode
uses: actions/github-script@v6
with:
script: |
const { owner, repo } = context.repo;
const mode = '${{ inputs.cache_mode }}';
console.log(`🔹 当前缓存清理模式: ${mode}`);
let totalDeleted = 0;
let totalFound = 0;
let page = 1;
while (true) {
const res = await github.rest.actions.getActionsCacheList({
owner, repo, per_page: 100, page
});
const caches = res.data.actions_caches;
if (!caches || caches.length === 0) break;
for (const cache of caches) {
const key = cache.key;
let matched = false;
switch (mode) {
case 'all': matched = true; break;
case 'ccache': matched = key.startsWith('ccache-'); break;
case 'apt-cache': matched = key.includes('apt-cache') || key.includes('apt'); break;
default: matched = false;
}
if (matched) {
totalFound++;
console.log(`删除缓存: ${key}`);
try {
await github.rest.actions.deleteActionsCacheById({
owner, repo, cache_id: cache.id
});
totalDeleted++;
} catch (err) {
console.log(`删除缓存失败: ${key}`);
}
}
}
if (caches.length < 100) break;
page++;
}
if (totalFound === 0) {
console.log("没有找到符合条件的缓存。");
} else {
console.log(`共找到 ${totalFound} 个缓存,成功删除 ${totalDeleted} 个。`);
}
cleanup-self-workflow:
runs-on: ubuntu-latest
if: always()
env:
CURRENT_RUN_ID: ${{ github.run_id }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
steps:
- name: Clean the workflow own Run History
run: |
set -e
echo "清理本工作流(Workflow and Cache Cleanup)历史记录..."
DELETED=0
while IFS=' ' read -r RUN_ID STATE; do
[ -z "$RUN_ID" ] && continue
[[ "$RUN_ID" == "$CURRENT_RUN_ID" ]] && continue
[[ "$STATE" == "in_progress" || "$STATE" == "queued" || "$STATE" == "waiting" ]] && continue
echo "删除自身记录 ID: $RUN_ID"
gh run delete "$RUN_ID" --repo "$REPO" \
&& DELETED=$((DELETED + 1)) \
|| echo "❌ 删除失败: $RUN_ID"
done < <(gh run list \
--repo "$REPO" \
--workflow "Workflow and Cache Cleanup" \
--limit 200 \
--json databaseId,status \
--jq '.[] | [(.databaseId | tostring), .status] | join(" ")')
echo "✅ 自身记录清理完成,共删除 $DELETED 条。"