-
-
Notifications
You must be signed in to change notification settings - Fork 14
256 lines (248 loc) · 10 KB
/
clean-workflows.yml
File metadata and controls
256 lines (248 loc) · 10 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
#
# SPDX-FileCopyrightText: 2021 Ching Chow.
# SPDX-FileCopyrightText: 2021-2025 Jens A. Koch.
# SPDX-License-Identifier: BSL-1.0
#
# This file is part of https://github.com/jakoch/cpp-devbox
#
name: Clean Workflow Runs
on:
# This workflow runs at 03:17 daily.
# The irregular time is used to avoid Github high load times.
schedule:
- cron: "17 3 * * *" # GMT
# You can manually run this workflow.
workflow_dispatch:
jobs:
cleanup-workflows:
name: "Clean Workflows"
runs-on: ubuntu-24.04
timeout-minutes: 10
# List Workflow Runs: https://api.github.com/repos/jakoch/cpp-devbox/actions/workflows
steps:
- name: ✂ Remove cancelled or skipped workflow runs
uses: actions/github-script@v8 # https://github.com/actions/github-script
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const cancelled = await github.rest.actions.listWorkflowRunsForRepo({
owner: context.repo.owner,
per_page: 50,
repo: context.repo.repo,
status: 'cancelled',
});
const skipped = await github.rest.actions.listWorkflowRunsForRepo({
owner: context.repo.owner,
per_page: 50,
repo: context.repo.repo,
status: 'skipped',
});
for (const response of [cancelled, skipped]) {
for (const run of response.data.workflow_runs) {
console.log(`[Deleting] Run id ${run.id} of '${run.name}' is a cancelled or skipped run.`);
await github.rest.actions.deleteWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: run.id
});
}
}
- name: ✂ Remove 30 days old workflows runs
uses: actions/github-script@v8 # https://github.com/actions/github-script
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const days_to_expiration = 14;
const ms_in_day = 86400000;
const now = Date.now();
const pages = 5;
// add the workflows runs to remove here
const workflows = [
'release.yml',
'check-spelling.yml'
]
let runs_to_delete = [];
for (const workflow of workflows) {
for (let page = 0; page < pages; page += 1) {
let response = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
page: page,
per_page: 50,
repo: context.repo.repo,
workflow_id: workflow
});
if (response.data.workflow_runs.length === 0) break;
if (response.data.workflow_runs.length > 0) {
for (const run of response.data.workflow_runs) {
if (now - Date.parse(run.created_at) > ms_in_day * days_to_expiration) {
runs_to_delete.push([run.id, run.name]);
}
}
}
if (response.data.workflow_runs.length < 50) break;
}
}
for (const run of runs_to_delete) {
console.log(`[Deleting] Run id ${run[0]} of '${run[1]}' is older than ${days_to_expiration} days.`);
try {
await github.rest.actions.deleteWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: run[0]
});
} catch (error) {
// ignore errors
}
}
# https://api.github.com/repos/jakoch/cpp-devbox/actions/workflows/clean-workflows.yml/runs
- name: ✂ Remove runs of the cleanup workflow itself
uses: actions/github-script@v8 # https://github.com/actions/github-script
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const pages = 5;
let runs_to_delete = [];
// Get all workflows
const { data: workflowsData } = await github.rest.actions.listRepoWorkflows({
owner: context.repo.owner,
repo: context.repo.repo
});
// Find workflow by name
const workflowName = "Clean Workflow Runs";
const workflow = workflowsData.workflows.find(w => w.name === workflowName);
if (!workflow) {
console.log(`❌ No workflow named ${workflowName} found.`);
return;
}
const workflowId = workflow.id;
console.log(`✅ Found workflow ID: ${workflowId}`);
// Fetch workflow runs
for (let page = 0; page < pages; page += 1) {
const response = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: workflowId, // Use dynamically fetched ID
page: page,
per_page: 50,
});
if (response.data.workflow_runs.length === 0) break;
if (response.data.workflow_runs.length > 0) {
for (const run of response.data.workflow_runs) {
runs_to_delete.push([run.id, run.name]);
}
}
if (response.data.workflow_runs.length < 50) break;
}
// Delete workflow runs
for (const run of runs_to_delete) {
console.log(`[Deleting] Run id ${run[0]} of '${run[1]}'.`);
try {
await github.rest.actions.deleteWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: run[0]
});
} catch (error) {
// ignore errors
}
}
- name: ✂ Remove Dependabot workflow runs
uses: actions/github-script@v8 # https://github.com/actions/github-script
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const MAX_PAGES = 20;
const PER_PAGE = 100;
let runs_to_delete = [];
console.log("🔍 Scanning workflow runs...");
for (let page = 1; page <= MAX_PAGES; page++) {
const response = await github.rest.actions.listWorkflowRunsForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
per_page: PER_PAGE,
page: page
});
const runs = response.data.workflow_runs;
if (!runs || runs.length === 0) break;
for (const run of runs) {
if (run.actor?.login === "dependabot[bot]") {
runs_to_delete.push([run.id, run.name]);
}
}
if (runs.length < PER_PAGE) break;
}
if (runs_to_delete.length === 0) {
console.log("✅ No Dependabot runs found.");
return;
}
console.log(`🗑️ Found ${runs_to_delete.length} runs to delete.`);
for (const [runId, runName] of runs_to_delete) {
console.log(`[Deleting] Run #${runId} (${runName})`);
try {
await github.rest.actions.deleteWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: runId
});
} catch (error) {
console.warn(`⚠️ Failed to delete run ${runId}: ${error.message}`);
}
}
console.log("✅ Cleanup complete.");
- name: ✂ Remove pages-build-deployment workflow runs except the most recent one
uses: actions/github-script@v8 # https://github.com/actions/github-script
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const pages = 5;
let runs_to_delete = [];
// Get all workflows
const { data: workflowsData } = await github.rest.actions.listRepoWorkflows({
owner: context.repo.owner,
repo: context.repo.repo
});
// Find workflow by name
const workflowName = "pages-build-deployment";
const workflow = workflowsData.workflows.find(w => w.name === workflowName);
if (!workflow) {
console.log(`❌ No workflow named ${workflowName} found.`);
return;
}
const workflowId = workflow.id;
console.log(`✅ Found workflow ID: ${workflowId}`);
let isFirstRun = true;
// Fetch workflow runs
for (let page = 1; page <= pages; page++) {
const response = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: workflowId,
page: page,
per_page: 50,
});
const runs = response.data.workflow_runs;
if (runs.length === 0) break;
for (const run of runs) {
// Skip the newest run
if (isFirstRun) {
console.log(`⏭ Keeping latest run ${run.id}`);
isFirstRun = false;
continue;
}
runs_to_delete.push([run.id, run.name]);
}
if (runs.length < 50) break;
}
// Delete workflow runs
for (const run of runs_to_delete) {
console.log(`[Deleting] Run id ${run[0]} of '${run[1]}'.`);
try {
await github.rest.actions.deleteWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: run[0]
});
} catch (error) {
// ignore errors
}
}