-
Notifications
You must be signed in to change notification settings - Fork 5
59 lines (53 loc) · 2.16 KB
/
preview-image-cleanup.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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'