Cleanup Old Artifacts #3
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: | |
| - cron: '0 2 * * 0' | |
| workflow_dispatch: | |
| jobs: | |
| cleanup: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Cleanup old artifacts | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const artifacts = await github.rest.actions.listArtifactsForRepo({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| }); | |
| const sortedArtifacts = artifacts.data.artifacts.sort((a, b) => | |
| new Date(b.created_at) - new Date(a.created_at) | |
| ); | |
| const toDelete = sortedArtifacts.slice(5); | |
| for (const artifact of toDelete) { | |
| console.log(`Deleting artifact: ${artifact.name} (${artifact.created_at})`); | |
| await github.rest.actions.deleteArtifact({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| artifact_id: artifact.id, | |
| }); | |
| } | |
| console.log(`Cleaned up ${toDelete.length} old artifacts`); |