-
Notifications
You must be signed in to change notification settings - Fork 1
73 lines (62 loc) · 2.36 KB
/
Copy pathcleanup.yml
File metadata and controls
73 lines (62 loc) · 2.36 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
# Copyright the Hyperledger Fabric contributors. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
name: Cleanup Old Workflow Runs
on:
schedule:
- cron: '0 2 * * *'
workflow_dispatch:
inputs:
keep_runs:
description: Number of latest completed runs to keep per workflow
required: false
default: '10'
jobs:
cleanup:
runs-on: ubuntu-latest
timeout-minutes: 30
concurrency:
group: workflow-cleanup
cancel-in-progress: false
permissions:
actions: write
steps:
- name: Delete old workflow runs
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const defaultKeep = 10;
const inputKeep = Number(context.payload.inputs?.keep_runs || defaultKeep);
const runsToKeep = Number.isFinite(inputKeep) && inputKeep > 0 ? inputKeep : defaultKeep;
console.log(`Keeping latest ${runsToKeep} completed runs per workflow`);
const workflows = await github.paginate(github.rest.actions.listRepoWorkflows, {
owner,
repo,
per_page: 100,
});
let deletedCount = 0;
let failedCount = 0;
for (const workflow of workflows) {
const runs = await github.paginate(github.rest.actions.listWorkflowRuns, {
owner,
repo,
workflow_id: workflow.id,
status: 'completed',
per_page: 100,
});
runs.sort((a, b) => new Date(b.created_at) - new Date(a.created_at));
const staleRuns = runs.slice(runsToKeep);
console.log(`${workflow.name}: found ${runs.length} completed runs, deleting ${staleRuns.length}`);
for (const run of staleRuns) {
try {
await github.rest.actions.deleteWorkflowRun({ owner, repo, run_id: run.id });
deletedCount++;
} catch (error) {
failedCount++;
console.log(`Failed to delete run ${run.id}: ${error.message}`);
}
}
}
console.log(`Deleted ${deletedCount} workflow runs, ${failedCount} failures`);