-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
56 lines (49 loc) · 1.73 KB
/
cleanup.yml
File metadata and controls
56 lines (49 loc) · 1.73 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
name: Cleanup old workflow runs
on:
schedule:
- cron: '0 3 * * *'
workflow_dispatch:
jobs:
cleanup:
runs-on: ubuntu-latest
if: github.event.repository.fork == false
permissions:
actions: write
steps:
- uses: actions/github-script@v8
with:
script: |
const KEEP = 0;
const workflowNames = ["VirusTotal Scan", "Cleanup old workflow runs"];
// Get workflow ID
const workflows = await github.rest.actions.listRepoWorkflows({
owner: context.repo.owner,
repo: context.repo.repo
});
for (const name of workflowNames) {
const wf = workflows.data.workflows.find(w => w.name === name);
if (!wf) {
core.info(`Workflow "${name}" not found, skipping`);
continue;
}
// List runs
const runs = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: wf.id,
per_page: 100
});
const toDelete = runs.data.workflow_runs.slice(KEEP);
core.info(`Deleting ${toDelete.length} old runs`);
for (const run of toDelete) {
try {
await github.rest.actions.deleteWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: run.id
});
} catch (e) {
core.info(`Skip run ${run.id}: ${e.message}`);
}
}
}