-
Notifications
You must be signed in to change notification settings - Fork 3
138 lines (128 loc) · 4.73 KB
/
cleanup-workflows.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
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
#
# .github/workflows/cleanup-workflows.yml
#
# Copyright 2021 Ching Chow, Jens A. Koch.
# SPDX-License-Identifier: BSL-1.0
# Origin: https://github.community/t/delete-old-workflow-results/16152/42
# This file is part of https://github.com/jakoch/csgo-cli
#
name: Clean Workflow Runs
on:
# This workflow runs at 00:00 daily.
schedule:
- cron: '0 0 * * *' # GMT
# You can manually run this workflow.
workflow_dispatch:
jobs:
# ---------------------------------------------------------------------------------------
cleanup-workflows:
# ---------------------------------------------------------------------------------------
name: "Clean Workflows"
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: ✂ Remove cancelled or skipped workflow runs
uses: actions/github-script@v7 # 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: 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(`[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@v7 # 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 = [
'build.yml',
'publish-PR-test-results.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(`[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
}
}
- name: ✂ Remove runs of the cleanup workflow itself
uses: actions/github-script@v7 # https://github.com/actions/github-script
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const pages = 5;
let runs_to_delete = [];
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: 'cleanup-workflows.yml'
});
if (response.data.workflow_runs.length > 0) {
for (const run of response.data.workflow_runs) {
runs_to_delete.push([run.id, run.name]);
}
}
}
for (const run of runs_to_delete) {
console.log(`[Deleting] Run id ${run[0]} of '${run[1]}'.`);
try {
await github.actions.deleteWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: run[0]
});
} catch (error) {
// ignore errors
}
}