-
Notifications
You must be signed in to change notification settings - Fork 4
100 lines (85 loc) · 3.88 KB
/
Copy pathcleanup-workflow-runs.yml
File metadata and controls
100 lines (85 loc) · 3.88 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
name: Cleanup Workflow Runs
on:
workflow_call:
inputs:
workflow_name:
required: true
type: string
description: 'Name of the workflow to cleanup runs for'
keep_runs:
required: false
type: number
default: 50
description: 'Target number of recent runs to keep after cleanup'
keep_threshold:
required: false
type: number
default: 10
description: 'Buffer threshold - cleanup only starts when runs exceed keep_runs + keep_threshold'
jobs:
cleanup:
name: Cleanup Old Workflow Runs
runs-on: ubuntu-latest
permissions:
actions: write
steps:
- name: Cleanup old workflow runs
uses: actions/github-script@v7
env:
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true # Ensure we're using Node 24 for GitHub Script'
with:
script: |
const workflowName = '${{ inputs.workflow_name }}';
const keepRuns = ${{ inputs.keep_runs }};
const keepThreshold = ${{ inputs.keep_threshold }};
const cleanupThreshold = keepRuns + keepThreshold;
console.log(`🧹 Starting cleanup check for workflow: "${workflowName}"`);
console.log(`📌 Configuration: keep ${keepRuns} runs, threshold ${cleanupThreshold} (keep_runs + keep_threshold)`);
// Get the current workflow ID
const workflows = await github.rest.actions.listRepoWorkflows({
owner: context.repo.owner,
repo: context.repo.repo,
});
const workflow = workflows.data.workflows.find(w => w.name === workflowName);
if (!workflow) {
console.log(`❌ Workflow "${workflowName}" not found`);
return;
}
console.log(`✅ Found workflow: ${workflow.name} (ID: ${workflow.id})`);
// Get all runs for this workflow
const runs = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: workflow.id,
per_page: 100, // Get up to 100 runs
});
const totalRuns = runs.data.workflow_runs.length;
console.log(`📊 Total runs found: ${totalRuns}`);
// Check if cleanup is needed
if (totalRuns <= cleanupThreshold) {
console.log(`✅ No cleanup needed (${totalRuns} runs ≤ ${cleanupThreshold} threshold)`);
return;
}
console.log(`⚠️ Cleanup threshold reached! (${totalRuns} runs > ${cleanupThreshold} threshold)`);
console.log(`🧹 Starting cleanup: will keep ${keepRuns} runs, deleting ${totalRuns - keepRuns} runs`);
// Sort runs by creation date (newest first)
const sortedRuns = runs.data.workflow_runs.sort(
(a, b) => new Date(b.created_at) - new Date(a.created_at)
);
// Keep only the most recent runs
const runsToDelete = sortedRuns.slice(keepRuns);
console.log(`📌 Keeping ${keepRuns} most recent runs, deleting ${runsToDelete.length} older runs`);
// Delete old runs
for (const run of runsToDelete) {
try {
await github.rest.actions.deleteWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: run.id,
});
console.log(`🗑️ Deleted run: ${run.id} (created: ${run.created_at})`);
} catch (error) {
console.error(`❌ Failed to delete run ${run.id}: ${error.message}`);
}
}
console.log(`✅ Workflow cleanup completed`);