forked from jakoch/install-vulkan-sdk-action
-
Notifications
You must be signed in to change notification settings - Fork 0
184 lines (176 loc) · 6.98 KB
/
cleanup-workflows.yml
File metadata and controls
184 lines (176 loc) · 6.98 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
#
# SPDX-FileCopyrightText: 2021 Ching Chow.
# SPDX-FileCopyrightText: 2021-2026 Jens A. Koch.
# SPDX-License-Identifier: BSL-1.0
#
# This file is part of https://github.com/jakoch/install-vulkan-sdk-action
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: "Cleanup Workflows"
runs-on: ubuntu-latest
timeout-minutes: 5
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,
repo: context.repo.repo,
per_page: 100,
status: 'cancelled'
});
const skipped = await github.rest.actions.listWorkflowRunsForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100,
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 7 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 = 7;
const ms_in_day = 86400000;
const now = Date.now();
const pages = 5;
// list the workflows runs to remove here
const workflows = [
'build.yml',
'check-dist.yml'
]
let runs_to_delete = [];
for (const workflow of workflows) {
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: workflow,
page: page,
per_page: 100
});
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@v8 # 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) {
const 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.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 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 with name "Dependabot Updates"
const dependabotWorkflow = workflowsData.workflows.find(w => w.name === "Dependabot Updates");
if (!dependabotWorkflow) {
console.log("❌ No workflow named 'Dependabot Updates' found.");
return;
}
const workflowId = dependabotWorkflow.id;
console.log(`✅ Found Dependabot 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: 100,
});
if (response.data.workflow_runs.length > 0) {
for (const run of response.data.workflow_runs) {
if (run.triggering_actor?.login === 'dependabot[bot]') {
runs_to_delete.push([run.id, run.name]);
}
}
}
}
// 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
}
}