-
-
Notifications
You must be signed in to change notification settings - Fork 0
95 lines (84 loc) · 3.19 KB
/
cleanup.yml
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
# This workflow prunes old workflow runs for an entire repository.
name: Prune old builds
on:
schedule:
- cron: '0 0 * * *'
workflow_dispatch:
jobs:
prune:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Prune cancelled/skipped runs
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GH_TOKEN }}
script: |
const cancelled = await github.rest.actions.listWorkflowRunsForRepo({
owner: context.repo.owner,
per_page: 100,
repo: context.repo.repo,
status: 'cancelled',
});
const skipped = await github.rest.actions.listWorkflowRunsForRepo({
owner: context.repo.owner,
per_page: 100,
repo: context.repo.repo,
status: 'skipped',
});
for (const response of [cancelled, skipped]) {
for (const run of response.data.workflow_runs) {
console.log(`Run id ${run.id} of '${run.name}' is a cancelled/skipped run. Deleting...`);
await github.rest.actions.deleteWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: run.id
});
}
}
- name: Prune runs older than 3 days
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GH_TOKEN }}
script: |
const days_to_expiration = 3;
const ms_in_day = 86400000;
const now = Date.now();
const pages = 5;
// we don't want to prune old runs from test.yml
// because we track the duration of runs over time
const workflows = [
'build.yml',
'cleanup.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: 100,
repo: context.repo.repo,
workflow_id: workflow
});
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]);
}
}
}
}
}
for (const run of runs_to_delete) {
console.log(`Run id ${run[0]} of '${run[1]}' is older than ${days_to_expiration} days. Deleting...`);
try {
await github.rest.actions.deleteWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: run[0]
});
} catch (error) {
// ignore errors
}
}