Cleanup Old Packages #13
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 Packages | |
| on: | |
| schedule: | |
| - cron: '0 0 * * 0' # Run every Sunday at midnight | |
| workflow_dispatch: # Allow manual trigger | |
| jobs: | |
| cleanup: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| packages: write | |
| contents: read | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Cleanup old packages | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const { data: packages } = await github.rest.packages.getAllPackageVersionsForPackageOwnedByAuthenticatedUser({ | |
| package_type: 'container', | |
| package_name: process.env.GITHUB_REPOSITORY.split('/')[1] | |
| }); | |
| // Identify versions with only a single SHA tag (format: sha-<7 hexadecimal characters>) | |
| const shaOnlyVersions = packages.filter(pkg => { | |
| const tags = pkg.metadata.container.tags; | |
| return tags.length === 1 && tags[0].match(/^sha-[0-9a-f]{7}$/); | |
| }); | |
| // Delete the SHA-only versions | |
| for (const version of shaOnlyVersions) { | |
| await github.rest.packages.deletePackageVersionForAuthenticatedUser({ | |
| package_type: 'container', | |
| package_name: process.env.GITHUB_REPOSITORY.split('/')[1], | |
| package_version_id: version.id | |
| }); | |
| console.log(`Deleted package version ${version.id} with SHA tag ${version.metadata.container.tags[0]}`); | |
| } |