Skip to content

Commit 83a92e4

Browse files
Copilotneilime
andcommitted
feat: add clean-images action wrapper
Co-authored-by: neilime <314088+neilime@users.noreply.github.com> Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
1 parent 82858e2 commit 83a92e4

File tree

9 files changed

+288
-115
lines changed

9 files changed

+288
-115
lines changed

.github/workflows/__test-action-docker-prune-pull-requests-image-tags.yml

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ jobs:
5252
]
5353
5454
act:
55+
name: Act - Prune pull requests image tags
5556
runs-on: ubuntu-latest
5657
needs: [setup, arrange]
5758
permissions:
@@ -82,6 +83,7 @@ jobs:
8283
image: ${{ env.IMAGE }}
8384

8485
assert:
86+
name: Assert - Verify deleted image tags
8587
runs-on: ubuntu-latest
8688
needs: [setup, arrange, act]
8789
steps:
@@ -115,7 +117,7 @@ jobs:
115117
- name: Assert - Ensure expected packages versions have been deleted
116118
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
117119
with:
118-
github-token: ${{ github.token }}
120+
github-token: ${{ secrets.GITHUB_TOKEN }}
119121
script: |
120122
const assert = require("assert");
121123
const packagePayload = {
@@ -175,32 +177,9 @@ jobs:
175177
steps:
176178
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
177179

178-
- id: get-issue-number
179-
if: ${{ github.event_name == 'pull_request' }}
180-
uses: hoverkraft-tech/ci-github-common/actions/get-issue-number@b7dd413209df265bef8d7eb0efb117eaabc684c4 # 0.27.0
181-
182180
- name: Delete test packages
183-
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
181+
uses: ./actions/docker/clean-images
184182
with:
185-
github-token: ${{ github.token }}
186-
script: |
187-
const packagesToDelete = ["${{ env.IMAGE }}", "${{ env.IMAGE }}/cache"];
188-
189-
for (const packageName of packagesToDelete) {
190-
const packagePayload = {
191-
package_type: 'container',
192-
package_name: `ci-github-container/${packageName}`,
193-
org: 'hoverkraft-tech',
194-
};
195-
196-
try {
197-
await github.rest.packages.deletePackageForOrg(packagePayload);
198-
} catch (error) {
199-
if (error.status === 404) {
200-
core.warning(`Package "${packagePayload.package_name}" not found.`);
201-
} else {
202-
throw error;
203-
}
204-
}
205-
206-
}
183+
github-token: ${{ secrets.GITHUB_TOKEN }}
184+
package: ci-github-container/${{ env.IMAGE }},ci-github-container/${{ env.IMAGE }}/cache
185+
tags: "*"

.github/workflows/__test-workflow-docker-build-images-platforms-and-signing.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ jobs:
122122
with:
123123
registry: ghcr.io
124124
username: ${{ github.repository_owner }}
125-
password: ${{ github.token }}
125+
password: ${{ secrets.GITHUB_TOKEN }}
126126

127127
- name: Verify image exists
128128
env:
@@ -274,16 +274,16 @@ jobs:
274274
strategy:
275275
fail-fast: false
276276
matrix: ${{ fromJson(needs.arrange.outputs.matrix) }}
277+
permissions:
278+
packages: write
277279
steps:
278-
- name: Delete ephemeral test packages
279-
env:
280-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
281-
IMAGE_NAME: ${{ matrix.image-name }}
282-
run: |
283-
gh api \
284-
--method DELETE \
285-
-H "Accept: application/vnd.github+json" \
286-
-H "X-GitHub-Api-Version: 2022-11-28" \
287-
/orgs/${{ github.repository_owner }}/packages/container/ci-github-container%2F"${IMAGE_NAME}" || echo "No ${IMAGE_NAME} package to delete"
280+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
288281

282+
- name: Delete ephemeral test packages
283+
uses: ./actions/docker/clean-images
284+
continue-on-error: true
285+
with:
286+
github-token: ${{ secrets.GITHUB_TOKEN }}
287+
package: ci-github-container/${{ matrix.image-name }}
288+
tags: "*"
289289
# jscpd:ignore-end

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ _Actions that operate on OCI images across their build, metadata, and lifecycle
2424

2525
#### - [Build image](actions/docker/build-image/README.md)
2626

27+
#### - [Clean images](actions/docker/clean-images/README.md)
28+
2729
#### - [Create images manifests](actions/docker/create-images-manifests/README.md)
2830

2931
#### - [Get image metadata](actions/docker/get-image-metadata/README.md)
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<!-- header:start -->
2+
3+
# ![Icon](data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9ImN1cnJlbnRDb2xvciIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGNsYXNzPSJmZWF0aGVyIGZlYXRoZXItdHJhc2gtMiIgY29sb3I9ImJsdWUiPjxwb2x5bGluZSBwb2ludHM9IjMgNiA1IDYgMjEgNiI+PC9wb2x5bGluZT48cGF0aCBkPSJNMTkgNnYxNGEyIDIgMCAwIDEtMiAySDdhMiAyIDAgMCAxLTItMlY2bTMgMFY0YTIgMiAwIDAgMSAyLTJoNGEyIDIgMCAwIDEgMiAydjIiPjwvcGF0aD48bGluZSB4MT0iMTAiIHkxPSIxMSIgeDI9IjEwIiB5Mj0iMTciPjwvbGluZT48bGluZSB4MT0iMTQiIHkxPSIxMSIgeDI9IjE0IiB5Mj0iMTciPjwvbGluZT48L3N2Zz4=) GitHub Action: Docker - Clean images
4+
5+
<div align="center">
6+
<img src="../../../.github/logo.svg" width="60px" align="center" alt="Docker - Clean images" />
7+
</div>
8+
9+
---
10+
11+
<!-- header:end -->
12+
13+
<!-- badges:start -->
14+
15+
[![Marketplace](https://img.shields.io/badge/Marketplace-docker------clean--images-blue?logo=github-actions)](https://github.com/marketplace/actions/docker---clean-images)
16+
[![Release](https://img.shields.io/github/v/release/hoverkraft-tech/ci-github-container)](https://github.com/hoverkraft-tech/ci-github-container/releases)
17+
[![License](https://img.shields.io/github/license/hoverkraft-tech/ci-github-container)](http://choosealicense.com/licenses/mit/)
18+
[![Stars](https://img.shields.io/github/stars/hoverkraft-tech/ci-github-container?style=social)](https://img.shields.io/github/stars/hoverkraft-tech/ci-github-container?style=social)
19+
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/hoverkraft-tech/ci-github-container/blob/main/CONTRIBUTING.md)
20+
21+
<!-- badges:end -->
22+
23+
<!-- overview:start -->
24+
25+
## Overview
26+
27+
Action to clean up container images from GitHub Container Registry (ghcr.io).
28+
This is a wrapper around the ghcr-cleanup-action that provides a consistent interface
29+
for image cleanup operations including deleting untagged images, pruning old tags,
30+
and managing image lifecycle.
31+
32+
<!-- overview:end -->
33+
34+
## Permissions
35+
36+
Set permissions to write packages. This is required to delete images.
37+
38+
```yaml
39+
permissions:
40+
packages: write
41+
```
42+
43+
<!-- usage:start -->
44+
45+
## Usage
46+
47+
```yaml
48+
- uses: hoverkraft-tech/ci-github-container/actions/docker/clean-images@c5d2c29ccdc9d357ea7b0e8e2567fa4643c05758 # copilot/refactor-cleanup-action-architecture
49+
with:
50+
# Comma-separated list of packages to cleanup.
51+
# Example: "application-1,application-2"
52+
#
53+
# This input is required.
54+
package: ""
55+
56+
# Comma-separated list of tags to delete (supports wildcard syntax).
57+
# Example: `pr-*,dev`
58+
#
59+
# This input is required.
60+
tags: ""
61+
62+
# GitHub token with the packages:write and packages:delete scopes.
63+
# See https://docs.github.com/en/packages/learn-github-packages/about-permissions-for-github-packages#about-scopes-and-permissions-for-package-registries.
64+
#
65+
# Default: `${{ github.token }}`
66+
github-token: ${{ github.token }}
67+
```
68+
69+
<!-- usage:end -->
70+
71+
<!-- inputs:start -->
72+
73+
## Inputs
74+
75+
| **Input** | **Description** | **Required** | **Default** |
76+
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ----------------------- |
77+
| **`package`** | Comma-separated list of packages to cleanup. | **true** | - |
78+
| | Example: "application-1,application-2" | | |
79+
| **`tags`** | Comma-separated list of tags to delete (supports wildcard syntax). | **true** | - |
80+
| | Example: `pr-*,dev` | | |
81+
| **`github-token`** | GitHub token with the packages:write and packages:delete scopes. | **false** | `$\{\{ github.token }}` |
82+
| | See <https://docs.github.com/en/packages/learn-github-packages/about-permissions-for-github-packages#about-scopes-and-permissions-for-package-registries>. | | |
83+
84+
<!-- inputs:end -->
85+
86+
<!-- secrets:start -->
87+
<!-- secrets:end -->
88+
89+
<!-- outputs:start -->
90+
91+
## Outputs
92+
93+
| **Output** | **Description** |
94+
| ------------------------------ | --------------------------------------- |
95+
| **`deleted-package-ids`** | The package IDs that were deleted. |
96+
| **`deleted-package-versions`** | The package versions that were deleted. |
97+
98+
<!-- outputs:end -->
99+
100+
<!-- examples:start -->
101+
102+
## Examples
103+
104+
### Clean up pull request images
105+
106+
Delete images tagged with a pull request pattern after the PR is closed:
107+
108+
```yaml
109+
name: Clean PR Images
110+
on:
111+
pull_request:
112+
types: [closed]
113+
114+
jobs:
115+
cleanup:
116+
runs-on: ubuntu-latest
117+
permissions:
118+
packages: write
119+
steps:
120+
- uses: hoverkraft-tech/ci-github-container/actions/docker/clean-images@c5d2c29ccdc9d357ea7b0e8e2567fa4643c05758 # copilot/refactor-cleanup-action-architecture
121+
with:
122+
package: my-app
123+
delete-tags: pr-${{ github.event.pull_request.number }}-*
124+
```
125+
126+
### Clean up untagged images
127+
128+
Remove all untagged images to save storage:
129+
130+
```yaml
131+
name: Clean Untagged Images
132+
on:
133+
schedule:
134+
- cron: "0 3 * * *" # Daily at 3 AM
135+
136+
jobs:
137+
cleanup:
138+
runs-on: ubuntu-latest
139+
permissions:
140+
packages: write
141+
steps:
142+
- uses: hoverkraft-tech/ci-github-container/actions/docker/clean-images@c5d2c29ccdc9d357ea7b0e8e2567fa4643c05758 # copilot/refactor-cleanup-action-architecture
143+
with:
144+
package: my-app
145+
delete-untagged: true
146+
```
147+
148+
<!-- examples:end -->
149+
150+
<!--
151+
// jscpd:ignore-start
152+
-->
153+
154+
<!-- contributing:start -->
155+
156+
## Contributing
157+
158+
Contributions are welcome! Please see the [contributing guidelines](https://github.com/hoverkraft-tech/ci-github-container/blob/main/CONTRIBUTING.md) for more details.
159+
160+
<!-- contributing:end -->
161+
162+
<!-- security:start -->
163+
<!-- security:end -->
164+
165+
<!-- license:start -->
166+
167+
## License
168+
169+
This project is licensed under the MIT License.
170+
171+
SPDX-License-Identifier: MIT
172+
173+
Copyright © 2025 hoverkraft
174+
175+
For more details, see the [license](http://choosealicense.com/licenses/mit/).
176+
177+
<!-- license:end -->
178+
179+
<!-- generated:start -->
180+
181+
---
182+
183+
This documentation was automatically generated by [CI Dokumentor](https://github.com/hoverkraft-tech/ci-dokumentor).
184+
185+
<!-- generated:end -->
186+
187+
<!--
188+
// jscpd:ignore-end
189+
-->
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
name: "Docker - Clean images"
3+
description: |
4+
Action to clean up container images from GitHub Container Registry (ghcr.io).
5+
This is a wrapper around the ghcr-cleanup-action that provides a consistent interface
6+
for image cleanup operations including deleting untagged images, pruning old tags,
7+
and managing image lifecycle.
8+
author: hoverkraft
9+
branding:
10+
icon: trash-2
11+
color: blue
12+
13+
inputs:
14+
package:
15+
description: |
16+
Comma-separated list of packages to cleanup.
17+
Example: "application-1,application-2"
18+
required: true
19+
tags:
20+
description: |
21+
Comma-separated list of tags to delete (supports wildcard syntax).
22+
Example: `pr-*,dev`
23+
required: true
24+
github-token:
25+
description: |
26+
GitHub token with the packages:write and packages:delete scopes.
27+
See https://docs.github.com/en/packages/learn-github-packages/about-permissions-for-github-packages#about-scopes-and-permissions-for-package-registries.
28+
default: ${{ github.token }}
29+
30+
runs:
31+
using: "composite"
32+
steps:
33+
- id: cleanup
34+
uses: dataaxiom/ghcr-cleanup-action@cd0cdb900b5dbf3a6f2cc869f0dbb0b8211f50c4 # v1.0.16
35+
with:
36+
token: ${{ inputs.github-token }}
37+
package: ${{ inputs.package }}
38+
tags: ${{ inputs.tags }}
39+
delete-ghost-images: true
40+
delete-orphaned-images: true
41+
validate: true

0 commit comments

Comments
 (0)