Cleanup Old Artifacts #85
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: | |
| schedule: | |
| # Run daily at 2 AM UTC | |
| - cron: "0 2 * * *" | |
| workflow_dispatch: | |
| permissions: | |
| actions: write | |
| contents: read | |
| jobs: | |
| cleanup: | |
| name: Delete Old Artifacts | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Cleanup old artifacts | |
| id: cleanup | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const owner = context.repo.owner; | |
| const repo = context.repo.repo; | |
| // Get all artifacts | |
| const artifacts = await github.rest.actions.listArtifactsForRepo({ | |
| owner, | |
| repo, | |
| per_page: 100 | |
| }); | |
| // Calculate cutoff date (keep artifacts from last 7 days) | |
| const cutoffDate = new Date(); | |
| cutoffDate.setDate(cutoffDate.getDate() - 7); | |
| let deletedCount = 0; | |
| let keptCount = 0; | |
| for (const artifact of artifacts.data.artifacts) { | |
| const createdAt = new Date(artifact.created_at); | |
| if (createdAt < cutoffDate) { | |
| console.log(`🗑️ Deleting artifact: ${artifact.name} (created ${artifact.created_at})`); | |
| await github.rest.actions.deleteArtifact({ | |
| owner, | |
| repo, | |
| artifact_id: artifact.id | |
| }); | |
| deletedCount++; | |
| } else { | |
| keptCount++; | |
| } | |
| } | |
| console.log(`✅ Cleanup complete:`); | |
| console.log(` - Deleted: ${deletedCount} artifacts`); | |
| console.log(` - Kept: ${keptCount} artifacts`); | |
| // Set outputs for summary | |
| core.setOutput('deleted', deletedCount); | |
| core.setOutput('kept', keptCount); | |
| // Write summary directly from this step to avoid relying on external outputs | |
| const lines = [ | |
| '## 🧹 Artifact Cleanup Summary', | |
| '', | |
| `- **Deleted**: ${deletedCount} old artifacts`, | |
| `- **Kept**: ${keptCount} recent artifacts`, | |
| '- **Retention Policy**: 7 days', | |
| '', | |
| ]; | |
| const summaryPath = process.env.GITHUB_STEP_SUMMARY; | |
| if (summaryPath) { | |
| const fs = require('fs'); | |
| fs.appendFileSync(summaryPath, lines.join('\n')); | |
| } |