Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 104 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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-<ref>` key.
concurrency:
group: ci-${{ github.ref }}
group: ci-${{ github.ref }}-${{ github.event_name == 'schedule' && 'retention' || 'ci' }}
cancel-in-progress: true

permissions:
Expand All @@ -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:
Expand All @@ -59,6 +66,7 @@ jobs:

test:
name: Test
if: github.event_name != 'schedule'
runs-on: ubuntu-latest
timeout-minutes: 30

Expand Down Expand Up @@ -154,6 +162,7 @@ jobs:

secret-scan:
name: Secret scan (gitleaks)
if: github.event_name != 'schedule'
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 "<untagged>" 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
Loading