|
16 | 16 | timeout-minutes: 10 |
17 | 17 | steps: |
18 | 18 | - name: Delete workflow runs older than 30 days |
19 | | - uses: Mattraks/delete-workflow-runs@v2 |
| 19 | + uses: actions/github-script@v7 |
20 | 20 | with: |
21 | | - token: ${{ github.token }} |
22 | | - repository: ${{ github.repository }} |
23 | | - retain_days: 30 |
24 | | - keep_minimum_runs: 6 |
| 21 | + script: | |
| 22 | + const days_to_keep = 30; |
| 23 | + const minimum_runs_to_keep = 6; |
| 24 | + const cutoff_date = new Date(Date.now() - (days_to_keep * 24 * 60 * 60 * 1000)); |
| 25 | + |
| 26 | + const workflows = await github.rest.actions.listRepoWorkflows({ |
| 27 | + owner: context.repo.owner, |
| 28 | + repo: context.repo.repo |
| 29 | + }); |
| 30 | + |
| 31 | + for (const workflow of workflows.data.workflows) { |
| 32 | + console.log(`Processing workflow: ${workflow.name} (${workflow.id})`); |
| 33 | + |
| 34 | + const runs = await github.rest.actions.listWorkflowRuns({ |
| 35 | + owner: context.repo.owner, |
| 36 | + repo: context.repo.repo, |
| 37 | + workflow_id: workflow.id, |
| 38 | + per_page: 100 |
| 39 | + }); |
| 40 | + |
| 41 | + // Sort runs by created date, newest first |
| 42 | + const sortedRuns = runs.data.workflow_runs.sort((a, b) => |
| 43 | + new Date(b.created_at) - new Date(a.created_at) |
| 44 | + ); |
| 45 | + |
| 46 | + // Keep the most recent runs and delete older ones |
| 47 | + for (let i = 0; i < sortedRuns.length; i++) { |
| 48 | + const run = sortedRuns[i]; |
| 49 | + const run_date = new Date(run.created_at); |
| 50 | + |
| 51 | + // Keep minimum number of recent runs |
| 52 | + if (i < minimum_runs_to_keep) { |
| 53 | + console.log(`Keeping recent run #${run.id} (${run.name})`); |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + // Delete runs older than cutoff date |
| 58 | + if (run_date < cutoff_date) { |
| 59 | + console.log(`Deleting old run #${run.id} from ${run.created_at}`); |
| 60 | + try { |
| 61 | + await github.rest.actions.deleteWorkflowRun({ |
| 62 | + owner: context.repo.owner, |
| 63 | + repo: context.repo.repo, |
| 64 | + run_id: run.id |
| 65 | + }); |
| 66 | + } catch (error) { |
| 67 | + console.log(`Failed to delete run #${run.id}: ${error.message}`); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + console.log('Cleanup complete!'); |
| 74 | +
|
0 commit comments