Skip to content

Commit 2b854e1

Browse files
NikitaCOEURneilime
authored andcommitted
feat(prune-pull-requests-images-tags): add preserve-tags-filter
1 parent 80516e1 commit 2b854e1

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

.github/workflows/prune-pull-requests-images-tags.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ on: # yamllint disable-line rule:truthy
3737
default: "^pr-([0-9]+)(?:-|$)"
3838
type: string
3939
required: false
40+
preserve-tags-filter:
41+
description: |
42+
Optional regular expression to match tags that should be preserved (not deleted).
43+
Tags matching this pattern will never be deleted, even if they are on a package version with PR tags.
44+
Example: "^v.*" to preserve version tags like v1.0.0, v2.1.3, etc.
45+
default: ""
46+
type: string
47+
required: false
4048

4149
permissions: {}
4250

@@ -105,6 +113,7 @@ jobs:
105113
with:
106114
image: ${{ matrix.image }}
107115
pull-request-tag-filter: ${{ inputs.pull-request-tag-filter }}
116+
preserve-tags-filter: ${{ inputs.preserve-tags-filter }}
108117

109118
- uses: hoverkraft-tech/ci-github-common/actions/local-workflow-actions@b17226e57c8ef31f860719766656ebb6df017218 # 0.31.6
110119
if: always() && steps.local-workflow-actions.outputs.repository

actions/docker/prune-pull-requests-image-tags/action.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ inputs:
2323
pull-request-tag-filter:
2424
description: "The regular expression to match pull request tags. Must have a capture group for the pull request number."
2525
default: "^pr-([0-9]+)(?:-|$)"
26+
preserve-tags-filter:
27+
description: |
28+
Optional regular expression to match tags that should be preserved (not deleted).
29+
Tags matching this pattern will never be deleted, even if they are on a package version with PR tags.
30+
Example: "^v.*" to preserve version tags like v1.0.0, v2.1.3, etc.
31+
required: false
32+
default: ""
2633
github-token:
2734
description: |
2835
GitHub token with the folowing scopes: `pull-requests:read`, `packages:read` and `packages:delete`.
@@ -73,6 +80,7 @@ runs:
7380
7481
const imageName = `${{ steps.image-name.outputs.image-name }}`;
7582
const isOrganization = `${{ steps.is-organization-or-user.outputs.is-organization }}` === 'true';
83+
const preserveTagsFilter = `${{ inputs.preserve-tags-filter }}`;
7684
7785
const script = require(`${{ github.action_path }}/index.js`)
7886
const tagsToDelete = await script({
@@ -81,6 +89,7 @@ runs:
8189
core,
8290
imageName,
8391
pullRequestTagFilter,
92+
preserveTagsFilter,
8493
isOrganization,
8594
});
8695

actions/docker/prune-pull-requests-image-tags/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = async ({
44
core,
55
imageName,
66
pullRequestTagFilter,
7+
preserveTagsFilter,
78
isOrganization,
89
}) => {
910
const repositoryOwner = `${context.repo.owner}`.toLowerCase();
@@ -33,6 +34,7 @@ module.exports = async ({
3334
context,
3435
core,
3536
pullRequestTagFilter,
37+
preserveTagsFilter,
3638
packageVersion,
3739
}),
3840
),
@@ -84,6 +86,7 @@ async function getTagsToDeleteFromPackageVersion({
8486
context,
8587
core,
8688
pullRequestTagFilter,
89+
preserveTagsFilter,
8790
packageVersion,
8891
}) {
8992
const tags = packageVersion.metadata.container.tags;
@@ -123,6 +126,17 @@ async function getTagsToDeleteFromPackageVersion({
123126
return [];
124127
}
125128

129+
// Filter out tags that should be preserved
130+
if (preserveTagsFilter && preserveTagsFilter.length > 0) {
131+
const preservedTags = tags.filter((tag) => tag.match(preserveTagsFilter));
132+
if (preservedTags.length > 0) {
133+
core.debug(
134+
`Preserving tags matching filter ${preserveTagsFilter}: ${preservedTags.join(", ")}`,
135+
);
136+
}
137+
return tags.filter((tag) => !tag.match(preserveTagsFilter));
138+
}
139+
126140
return tags;
127141
}
128142

0 commit comments

Comments
 (0)