Cleanup Old Artifacts #199
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Cleanup Old Artifacts | |
| on: | |
| # Run daily at 2 AM UTC | |
| schedule: | |
| - cron: "0 2 * * *" | |
| # Allow manual triggering | |
| workflow_dispatch: | |
| inputs: | |
| dry_run: | |
| description: "Dry run mode (will not actually delete artifacts)" | |
| required: false | |
| default: "true" | |
| type: boolean | |
| jobs: | |
| cleanup: | |
| runs-on: ubuntu-latest | |
| name: Delete old artifacts | |
| permissions: | |
| actions: write # Required to delete artifacts | |
| steps: | |
| - name: Delete artifacts older than 7 days | |
| uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9 | |
| with: | |
| script: | | |
| const daysToKeep = 7; | |
| const cutoffDate = new Date(); | |
| cutoffDate.setDate(cutoffDate.getDate() - daysToKeep); | |
| const dryRun = ${{ github.event.inputs.dry_run || 'true' }}; | |
| console.log(`${dryRun ? '[DRY RUN] ' : ''}Deleting artifacts older than ${cutoffDate.toISOString()}`); | |
| const artifacts = await github.rest.actions.listArtifactsForRepo({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| per_page: 100 | |
| }); | |
| let deletedCount = 0; | |
| let deletedSize = 0; | |
| for (const artifact of artifacts.data.artifacts) { | |
| const createdAt = new Date(artifact.created_at); | |
| if (createdAt < cutoffDate) { | |
| console.log(`${dryRun ? '[DRY RUN] Would delete' : 'Deleting'} artifact: ${artifact.name} (${artifact.size_in_bytes} bytes, created ${artifact.created_at})`); | |
| if (!dryRun) { | |
| await github.rest.actions.deleteArtifact({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| artifact_id: artifact.id | |
| }); | |
| } | |
| deletedCount++; | |
| deletedSize += artifact.size_in_bytes; | |
| } | |
| } | |
| const deletedSizeMB = (deletedSize / 1024 / 1024).toFixed(2); | |
| console.log(`\n✅ Cleanup complete!`); | |
| console.log(` ${dryRun ? 'Would delete' : 'Deleted'} ${deletedCount} artifacts`); | |
| console.log(` ${dryRun ? 'Would free' : 'Freed'} ${deletedSizeMB} MB of storage`); | |
| // Add to job summary | |
| await core.summary | |
| .addHeading(`Artifact Cleanup Summary${dryRun ? ' (DRY RUN)' : ''}`) | |
| .addRaw(`- ${dryRun ? 'Would delete' : 'Deleted'} artifacts: **${deletedCount}**`) | |
| .addRaw(`\n- Storage ${dryRun ? 'that would be freed' : 'freed'}: **${deletedSizeMB} MB**`) | |
| .addRaw(`\n- Cutoff date: ${cutoffDate.toISOString()}`) | |
| .write(); | |
| - name: Delete artifacts from failed runs | |
| uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9 | |
| with: | |
| script: | | |
| const dryRun = ${{ github.event.inputs.dry_run || 'true' }}; | |
| console.log(`${dryRun ? '[DRY RUN] ' : ''}Deleting artifacts from failed/cancelled runs...`); | |
| const runs = await github.rest.actions.listWorkflowRunsForRepo({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| status: 'completed', | |
| per_page: 100 | |
| }); | |
| let deletedCount = 0; | |
| for (const run of runs.data.workflow_runs) { | |
| if (run.conclusion === 'failure' || run.conclusion === 'cancelled') { | |
| const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| run_id: run.id | |
| }); | |
| for (const artifact of artifacts.data.artifacts) { | |
| console.log(`${dryRun ? '[DRY RUN] Would delete' : 'Deleting'} artifact from failed run: ${artifact.name}`); | |
| if (!dryRun) { | |
| await github.rest.actions.deleteArtifact({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| artifact_id: artifact.id | |
| }); | |
| } | |
| deletedCount++; | |
| } | |
| } | |
| } | |
| console.log(`\n✅ ${dryRun ? 'Would delete' : 'Deleted'} ${deletedCount} artifacts from failed/cancelled runs`); |