From c6bd6d424103996b9e50c8f6ffdaa37a1acbda4f Mon Sep 17 00:00:00 2001 From: Andriy Oblivantsev Date: Wed, 11 Nov 2026 16:28:21 +0000 Subject: [PATCH] chore(ci): bound GHCR sha-* retention (#11) Add a weekly retention job that deletes old sha-<12 hex> image versions and keeps the newest 20. Only versions whose complete tag list is sha-* are candidates, so api/latest/v* can never be deleted. actions/delete-package-versions is not used: it matches ignore-versions against the version's name field only, never against metadata.container.tags, and this workflow pushes api + sha-* as the same manifest. The schedule event runs the retention job only (guard/test/secret-scan/ocr are skipped) and uses its own concurrency group so it cannot cancel a CI run. --- .github/workflows/ci.yml | 106 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 80d75a0..9fd047e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,9 +22,14 @@ on: description: "Backfill: существующий тег-источник" type: string default: "api" + # Weekly GHCR cleanup (#11): deletes only old `sha-*` versions, see `retention`. + schedule: + - cron: '17 3 * * 1' +# The schedule run must not cancel an in-flight CI run on the same ref, so it +# gets its own concurrency group. Everything else keeps the old `ci-` key. concurrency: - group: ci-${{ github.ref }} + group: ci-${{ github.ref }}-${{ github.event_name == 'schedule' && 'retention' || 'ci' }} cancel-in-progress: true permissions: @@ -33,6 +38,8 @@ permissions: jobs: guard: name: Guard (paths, pycache) + # `schedule` exists only for the retention job (#11); nothing else runs. + if: github.event_name != 'schedule' runs-on: ubuntu-latest timeout-minutes: 5 steps: @@ -59,6 +66,7 @@ jobs: test: name: Test + if: github.event_name != 'schedule' runs-on: ubuntu-latest timeout-minutes: 30 @@ -154,6 +162,7 @@ jobs: secret-scan: name: Secret scan (gitleaks) + if: github.event_name != 'schedule' runs-on: ubuntu-latest timeout-minutes: 10 steps: @@ -198,7 +207,9 @@ jobs: ocr: name: OCR (tesseract fixture) - if: github.server_url == 'https://github.com' + if: >- + github.server_url == 'https://github.com' + && github.event_name != 'schedule' runs-on: ubuntu-latest timeout-minutes: 15 steps: @@ -456,3 +467,94 @@ jobs: -t "$REPO:$VERSION" \ -t "$REPO:latest" \ "$REPO:$SOURCE" + + # GHCR retention (#11): bound the number of `sha-<12 hex>` image versions. + # + # Why not actions/delete-package-versions@v5: its `ignore-versions` is matched + # against the package version's `name` field only (src/version/get-versions.ts + # maps `version: version.name`), never against the container `tags` list. This + # workflow pushes `api` + `sha-*` as the SAME manifest (plus `v*`/`latest` on a + # release run), so the version that carries a protected tag is not identifiable + # by `name`, and a wrong regex would delete `api`/`latest`/`v*` aliases. + # The step below reads `metadata.container.tags` and therefore can only ever + # delete a version whose tags are ALL `sha-<12 hex>`; the newest 20 such + # versions are kept. `api`, `latest` and `v*` are unreachable by construction. + retention: + name: Retention (GHCR sha-* versions) + if: >- + github.server_url == 'https://github.com' + && (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: read + packages: write + + steps: + - name: Delete old sha-* versions, keep 20 + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + KEEP: "20" + run: | + set -euo pipefail + + OWNER="${GITHUB_REPOSITORY%%/*}" + PACKAGE="${GITHUB_REPOSITORY#*/}" + + # The package owner is a user account, so the /users/ endpoints apply. + BASE="/users/$OWNER/packages/container/$PACKAGE/versions" + + # Newest first, with the full tag list — `gh api --paginate` follows + # the Link header, so this covers the whole package. + gh api --paginate -H 'Accept: application/vnd.github+json' \ + "$BASE?per_page=100" \ + --jq '.[] | {id, created_at, tags: (.metadata.container.tags // [])}' \ + > /tmp/versions.jsonl + + total="$(jq -s 'length' /tmp/versions.jsonl)" + if [ "$total" -eq 0 ]; then + echo "::error::no versions listed for $OWNER/$PACKAGE — check packages: write access" + exit 1 + fi + + # Deletion candidates: at least one tag, and every tag is sha-<12 hex>. + jq -c 'select((.tags | length) > 0) + | select([.tags[] | test("^sha-[0-9a-f]{12}$")] | all)' \ + /tmp/versions.jsonl > /tmp/sha.jsonl + + # Everything else is a protected/moving alias (`api`, `latest`, `v*`) + # or an untagged version; neither is touched below. + jq -c 'select((.tags | length) == 0 + or ([.tags[] | test("^sha-[0-9a-f]{12}$")] | all | not))' \ + /tmp/versions.jsonl > /tmp/kept.jsonl + + jq -s 'sort_by(.created_at) | reverse' /tmp/sha.jsonl \ + | jq -r --argjson keep "$KEEP" '.[$keep:][] | "\(.id) \(.tags | join(","))"' \ + > /tmp/delete.txt + + echo "versions: $total total | sha-only: $(wc -l < /tmp/sha.jsonl)" \ + "| protected/untagged: $(wc -l < /tmp/kept.jsonl)" + echo "retaining the newest $KEEP sha-only versions; deleting $(wc -l < /tmp/delete.txt)" + jq -r '" protected: \(if (.tags | length) == 0 + then "" else (.tags | join(",")) end)"' \ + /tmp/kept.jsonl + + # Belt and braces: every id we are about to delete must carry sha-* tags + # only. A stray protected tag aborts the run before any DELETE request. + if [ -s /tmp/delete.txt ] \ + && grep -Ev '^[0-9]+ sha-[0-9a-f]{12}(,sha-[0-9a-f]{12})*$' /tmp/delete.txt; then + echo "::error::refusing to delete versions with non-sha tags (see above)" + exit 1 + fi + + failed=0 + while read -r id tags; do + [ -n "$id" ] || continue + echo "delete $id [$tags]" + gh api -X DELETE "$BASE/$id" || failed=$((failed + 1)) + done < /tmp/delete.txt + + if [ "$failed" -ne 0 ]; then + echo "::error::$failed version(s) could not be deleted" + exit 1 + fi