Skip to content

Commit 55c8169

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

File tree

3 files changed

+573
-0
lines changed

3 files changed

+573
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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: Setup Python
63+
uses: actions/setup-python@v5
64+
with:
65+
python-version: "3.12"
66+
67+
- name: Cleanup registry
68+
shell: bash
69+
env:
70+
GITHUB_TOKEN: ${{ github.token }}
71+
# TODO: add a suffix to the tag name "/${{ matrix.package_suffix }}",
72+
# so that we can do granular cleanup of multiple packages from the
73+
# same repository. Right now this is not possible due to the way the
74+
# images are tagged.
75+
PACKAGE_NAME: ${{ github.event.repository.name }}
76+
CLEANUP_POLICY: ${{ inputs.CLEANUP_POLICY }}
77+
MAX_IMAGES_PER_INSTANCE: ${{ inputs.MAX_IMAGES_PER_INSTANCE }}
78+
RETENTION_DAYS: ${{ inputs.RETENTION_DAYS }}
79+
INSTANCE_MARKER_FILE: ${{ inputs.INSTANCE_MARKER_FILE }}
80+
HASH_LENGTH: ${{ inputs.HASH_LENGTH }}
81+
DRY_RUN: ${{ inputs.DRY_RUN }}
82+
run: |
83+
set -euo pipefail
84+
args=(
85+
--package-name "${PACKAGE_NAME}"
86+
--instances-root "instances"
87+
--instance-marker-file "${INSTANCE_MARKER_FILE}"
88+
--hash-length "${HASH_LENGTH}"
89+
)
90+
91+
if [ "${DRY_RUN}" = "true" ]; then
92+
args+=(--dry-run)
93+
fi
94+
95+
case "${CLEANUP_POLICY}" in
96+
max_count)
97+
args+=(--max-per-instance "${MAX_IMAGES_PER_INSTANCE}")
98+
;;
99+
retention_days)
100+
args+=(--retention-days "${RETENTION_DAYS}")
101+
;;
102+
*)
103+
echo "Invalid CLEANUP_POLICY: ${CLEANUP_POLICY}. Expected max_count or retention_days."
104+
exit 1
105+
;;
106+
esac
107+
108+
python ".github/workflows/scripts/cleanup_ghcr_instance_images.py" "${args[@]}"

0 commit comments

Comments
 (0)