From 22292f533b168c1587723c26f53a5918111d1f59 Mon Sep 17 00:00:00 2001 From: feco Date: Wed, 18 Nov 2026 18:02:02 +0000 Subject: [PATCH] CI: keep the last 5 commit images in the registry, clean the runner and the server Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL --- .gitea/workflows/ci.yml | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index e66a999..69bda50 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -94,11 +94,47 @@ jobs: build backend -f services/backend/Dockerfile . build worker -f services/worker/Dockerfile . build frontend services/frontend + # Only the last 5 commit images of each service stay in the registry, + # plus `main`, `prod`, `latest` and the commit running in production + # (the server pulls it by its commit tag). + - name: Prune the registry + env: + TOKEN: ${{ secrets.REGISTRY_TOKEN }} + API: ${{ github.server_url }}/api/v1/packages/${{ github.repository_owner }} + run: | + set -eu + apk add --no-cache -q curl jq + prod=$(git ls-remote origin refs/heads/prod | cut -c1-12) + for svc in horoscope-api backend worker frontend; do + name="sorsfonalak-$svc" + page=1 + : > versions.tsv + while :; do + batch=$(curl -fsS -H "Authorization: token $TOKEN" "$API?type=container&q=$name&limit=50&page=$page") + [ "$(echo "$batch" | jq length)" -gt 0 ] || break + echo "$batch" | jq -r --arg n "$name" \ + '.[] | select(.name == $n and (.version | test("^[0-9a-f]{12}$"))) | [.created_at, .version] | @tsv' \ + >> versions.tsv + page=$((page + 1)) + done + sort -r versions.tsv | tail -n +6 | cut -f2 | while read -r version; do + [ "$version" = "$prod" ] && continue + curl -fsS -X DELETE -H "Authorization: token $TOKEN" "$API/container/$name/$version" + echo "deleted $name:$version" + done + done + rm -f versions.tsv + # The images are in the registry: the local copies and old build cache + # would fill the runner's disk. - name: Clean up if: always() run: | docker logout "$REGISTRY" || true - docker image prune -f --filter label=sorsfonalak.ci=1 >/dev/null + for svc in horoscope-api backend worker frontend; do + docker images "$IMAGE_PREFIX-$svc" --format '{{.Repository}}:{{.Tag}}' | xargs -r docker rmi >/dev/null 2>&1 || true + done + docker image prune -f >/dev/null + docker builder prune -af --filter until=72h >/dev/null || true deploy: needs: build @@ -135,7 +171,7 @@ jobs: (grep -q '^TAG=' .env && sed -i 's/^TAG=.*/TAG=$tag/' .env || echo 'TAG=$tag' >> .env) && \ docker compose pull -q && docker compose up -d --remove-orphans --wait && \ docker compose exec -T caddy caddy reload --config /etc/caddy/Caddyfile --adapter caddyfile && \ - docker image prune -f >/dev/null" + docker image prune -af >/dev/null" # A release (and its tag, e.g. v2026.11.17-163612e) for every deployment, # listing the commits since the previous one. - name: Release