Skip to content

Preview Image Cleanup #140

Preview Image Cleanup

Preview Image Cleanup #140

name: Preview Image Cleanup
on:
workflow_dispatch: {}
schedule:
- cron: '0 5 * * *'
permissions:
packages: write
jobs:
preview-image:
runs-on: ubuntu-latest
env:
PACKAGE_NAME: netzgrafik-editor-backend-preview
PR_TAG_PREFIX: pr
steps:
# Deletes all container images from closed pull requests
- uses: actions/github-script@v7
with:
script: |
const { owner, repo } = context.repo;
const pullRequests = await github.paginate(
github.rest.pulls.list.endpoint.merge({ owner, repo })
);
const isExpiredPrTag = (version) => {
const prNumber = +version.metadata?.container?.tags
?.find((t) => t.startsWith(process.env.PR_TAG_PREFIX))?.split(process.env.PR_TAG_PREFIX)[1];
return !!prNumber && !pullRequests.find((p) => p.number === prNumber);
};
const params = {
package_type: 'container',
package_name: `${process.env.PACKAGE_NAME}`,
username: owner
};
const { data: versions } = await github.rest.packages.getAllPackageVersionsForPackageOwnedByUser(params);
let packageDeletionFailed = false;
for (const version of versions.filter(isExpiredPrTag)) {
try {
await github.rest.packages.deletePackageVersionForUser({ ...params, package_version_id: version.id });
console.log(`Deleted ${version.name} (${version.metadata.container.tags.join(', ')})`);
} catch(e) {
console.error(`Failed to delete ${version.name} (${version.metadata.container.tags.join(', ')})`);
console.error(e);
packageDeletionFailed = true;
}
}
if (packageDeletionFailed) {
throw new Error('A package deletion failed, please check the log.');
}
# Deletes all container images without a tag
- uses: actions/delete-package-versions@v4
with:
package-name: ${{ env.PACKAGE_NAME }}
package-type: 'container'
delete-only-untagged-versions: 'true'