Skip to content

Commit b08546a

Browse files
committed
feat: add image docker cleanup workflows (#19)
SE-6574 Signed-off-by: Gabor Boros <gabor@opencraft.com>
1 parent aae08b6 commit b08546a

File tree

3 files changed

+610
-0
lines changed

3 files changed

+610
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
name: Cleanup Container Packages
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
RUNNER_WORKFLOW_LABEL:
7+
description: "The label of the runner workflow to run"
8+
required: false
9+
type: string
10+
default: "ubuntu-latest"
11+
CLEANUP_POLICY:
12+
description: "Retention mode: max_count or retention_days"
13+
required: false
14+
type: string
15+
default: "max_count"
16+
MAX_IMAGES_PER_INSTANCE:
17+
description: "Used with max_count: max number of images to keep per instance"
18+
required: false
19+
type: number
20+
default: 5
21+
RETENTION_DAYS:
22+
description: "Used with retention_days: delete images older than this many days"
23+
required: false
24+
type: number
25+
default: 30
26+
PACKAGE_SUFFIXES_JSON:
27+
description: 'JSON array of package name suffixes (GHCR package is "<repo>/<suffix>")'
28+
required: false
29+
type: string
30+
default: '["openedx","mfe"]'
31+
INSTANCE_MARKER_FILE:
32+
description: "Only directories with this marker file are treated as instances"
33+
required: false
34+
type: string
35+
default: "config.yml"
36+
HASH_LENGTH:
37+
description: "Expected random hash length in image tags"
38+
required: false
39+
type: number
40+
default: 8
41+
DRY_RUN:
42+
description: "Print deletions without deleting versions"
43+
required: false
44+
type: boolean
45+
default: false
46+
47+
jobs:
48+
cleanup:
49+
name: Cleanup ${{ matrix.package_suffix }}
50+
runs-on: ${{ inputs.RUNNER_WORKFLOW_LABEL }}
51+
permissions:
52+
contents: read
53+
packages: write
54+
strategy:
55+
fail-fast: false
56+
matrix:
57+
package_suffix: ${{ fromJSON(inputs.PACKAGE_SUFFIXES_JSON) }}
58+
steps:
59+
- name: Checkout repository
60+
uses: actions/checkout@v4
61+
62+
- name: Checkout workflow scripts
63+
uses: actions/checkout@v4
64+
with:
65+
repository: open-craft/launchpad-cluster-template
66+
ref: main
67+
path: workflow-scripts
68+
69+
- name: Setup Python
70+
uses: actions/setup-python@v5
71+
with:
72+
python-version: "3.12"
73+
74+
- name: Cleanup registry
75+
shell: bash
76+
env:
77+
GITHUB_TOKEN: ${{ github.token }}
78+
# TODO: add a suffix to the tag name "/${{ matrix.package_suffix }}",
79+
# so that we can do granular cleanup of multiple packages from the
80+
# same repository. Right now this is not possible due to the way the
81+
# images are tagged.
82+
PACKAGE_NAME: ${{ github.event.repository.name }}
83+
CLEANUP_POLICY: ${{ inputs.CLEANUP_POLICY }}
84+
MAX_IMAGES_PER_INSTANCE: ${{ inputs.MAX_IMAGES_PER_INSTANCE }}
85+
RETENTION_DAYS: ${{ inputs.RETENTION_DAYS }}
86+
INSTANCE_MARKER_FILE: ${{ inputs.INSTANCE_MARKER_FILE }}
87+
HASH_LENGTH: ${{ inputs.HASH_LENGTH }}
88+
DRY_RUN: ${{ inputs.DRY_RUN }}
89+
run: |
90+
set -euo pipefail
91+
args=(
92+
--package-name "${PACKAGE_NAME}"
93+
--instances-root "instances"
94+
--instance-marker-file "${INSTANCE_MARKER_FILE}"
95+
--hash-length "${HASH_LENGTH}"
96+
)
97+
98+
if [ "${DRY_RUN}" = "true" ]; then
99+
args+=(--dry-run)
100+
fi
101+
102+
case "${CLEANUP_POLICY}" in
103+
max_count)
104+
args+=(--max-per-instance "${MAX_IMAGES_PER_INSTANCE}")
105+
;;
106+
retention_days)
107+
args+=(--retention-days "${RETENTION_DAYS}")
108+
;;
109+
*)
110+
echo "Invalid CLEANUP_POLICY: ${CLEANUP_POLICY}. Expected max_count or retention_days."
111+
exit 1
112+
;;
113+
esac
114+
115+
python "workflow-scripts/.github/workflows/scripts/cleanup_ghcr_instance_images.py" "${args[@]}"

0 commit comments

Comments
 (0)