Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,39 @@ jobs:
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

# Keep only three newest SHA-only versions; preserve releases and untagged manifests.
- name: Prune old SHA-only image versions
uses: actions/github-script@v8
env:
GHCR_CLEANUP_PACKAGES: rerun
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const owner = context.repo.owner;
const packageType = 'container';
const packageNames = process.env.GHCR_CLEANUP_PACKAGES.split(',').filter(Boolean);
for (const packageName of packageNames) {
const versions = await github.paginate(
github.rest.packages.getAllPackageVersionsForPackageOwnedByUser,
{ username: owner, package_type: packageType, package_name: packageName, per_page: 100 }
);
// A semver/latest tag on the same digest protects the entire version.
// Untagged versions (including multi-platform child manifests) are untouched.
const candidates = versions.filter(v => {
const tags = v.metadata?.container?.tags ?? [];
return tags.length === 1 && /^sha-[a-f0-9]{7,40}$/.test(tags[0]);
}).sort((a, b) =>
Date.parse(b.created_at) - Date.parse(a.created_at) || b.id - a.id
);
core.info(`${packageName}: ${candidates.length} SHA-only versions; retaining newest 5.`);
for (const version of candidates.slice(5)) {
core.info(`Deleting ${packageName} ${version.metadata.container.tags[0]} (version ${version.id})`);
await github.rest.packages.deletePackageVersionForUser({
username: owner,
package_type: packageType,
package_name: packageName,
package_version_id: version.id,
});
}
}