|
| 1 | +name: Cleanup Workflow Runs |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + workflow_name: |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + description: 'Name of the workflow to cleanup runs for' |
| 10 | + keep_runs: |
| 11 | + required: false |
| 12 | + type: number |
| 13 | + default: 50 |
| 14 | + description: 'Target number of recent runs to keep after cleanup' |
| 15 | + keep_threshold: |
| 16 | + required: false |
| 17 | + type: number |
| 18 | + default: 10 |
| 19 | + description: 'Buffer threshold - cleanup only starts when runs exceed keep_runs + keep_threshold' |
| 20 | + |
| 21 | +jobs: |
| 22 | + cleanup: |
| 23 | + name: Cleanup Old Workflow Runs |
| 24 | + runs-on: ubuntu-latest |
| 25 | + permissions: |
| 26 | + actions: write |
| 27 | + steps: |
| 28 | + - name: Cleanup old workflow runs |
| 29 | + uses: actions/github-script@v7 |
| 30 | + env: |
| 31 | + FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true # Ensure we're using Node 24 for GitHub Script' |
| 32 | + with: |
| 33 | + script: | |
| 34 | + const workflowName = '${{ inputs.workflow_name }}'; |
| 35 | + const keepRuns = ${{ inputs.keep_runs }}; |
| 36 | + const keepThreshold = ${{ inputs.keep_threshold }}; |
| 37 | + const cleanupThreshold = keepRuns + keepThreshold; |
| 38 | + |
| 39 | + console.log(`🧹 Starting cleanup check for workflow: "${workflowName}"`); |
| 40 | + console.log(`📌 Configuration: keep ${keepRuns} runs, threshold ${cleanupThreshold} (keep_runs + keep_threshold)`); |
| 41 | + |
| 42 | + // Get the current workflow ID |
| 43 | + const workflows = await github.rest.actions.listRepoWorkflows({ |
| 44 | + owner: context.repo.owner, |
| 45 | + repo: context.repo.repo, |
| 46 | + }); |
| 47 | + |
| 48 | + const workflow = workflows.data.workflows.find(w => w.name === workflowName); |
| 49 | + if (!workflow) { |
| 50 | + console.log(`❌ Workflow "${workflowName}" not found`); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + console.log(`✅ Found workflow: ${workflow.name} (ID: ${workflow.id})`); |
| 55 | + |
| 56 | + // Get all runs for this workflow |
| 57 | + const runs = await github.rest.actions.listWorkflowRuns({ |
| 58 | + owner: context.repo.owner, |
| 59 | + repo: context.repo.repo, |
| 60 | + workflow_id: workflow.id, |
| 61 | + per_page: 100, // Get up to 100 runs |
| 62 | + }); |
| 63 | + |
| 64 | + const totalRuns = runs.data.workflow_runs.length; |
| 65 | + console.log(`📊 Total runs found: ${totalRuns}`); |
| 66 | + |
| 67 | + // Check if cleanup is needed |
| 68 | + if (totalRuns <= cleanupThreshold) { |
| 69 | + console.log(`✅ No cleanup needed (${totalRuns} runs ≤ ${cleanupThreshold} threshold)`); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + console.log(`⚠️ Cleanup threshold reached! (${totalRuns} runs > ${cleanupThreshold} threshold)`); |
| 74 | + console.log(`🧹 Starting cleanup: will keep ${keepRuns} runs, deleting ${totalRuns - keepRuns} runs`); |
| 75 | + |
| 76 | + // Sort runs by creation date (newest first) |
| 77 | + const sortedRuns = runs.data.workflow_runs.sort( |
| 78 | + (a, b) => new Date(b.created_at) - new Date(a.created_at) |
| 79 | + ); |
| 80 | + |
| 81 | + // Keep only the most recent runs |
| 82 | + const runsToDelete = sortedRuns.slice(keepRuns); |
| 83 | + |
| 84 | + console.log(`📌 Keeping ${keepRuns} most recent runs, deleting ${runsToDelete.length} older runs`); |
| 85 | + |
| 86 | + // Delete old runs |
| 87 | + for (const run of runsToDelete) { |
| 88 | + try { |
| 89 | + await github.rest.actions.deleteWorkflowRun({ |
| 90 | + owner: context.repo.owner, |
| 91 | + repo: context.repo.repo, |
| 92 | + run_id: run.id, |
| 93 | + }); |
| 94 | + console.log(`🗑️ Deleted run: ${run.id} (created: ${run.created_at})`); |
| 95 | + } catch (error) { |
| 96 | + console.error(`❌ Failed to delete run ${run.id}: ${error.message}`); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + console.log(`✅ Workflow cleanup completed`); |
0 commit comments