CI: keep the last 5 commit images in the registry, clean the runner and the server
CI / test (push) Successful in 58s
CI / build (push) Successful in 25s
CI / deploy (push) Skipped

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-18 18:02:02 +00:00
co-authored by Claude Opus 5.5
parent 27de820091
commit 22292f533b
+38 -2
View File
@@ -94,11 +94,47 @@ jobs:
build backend -f services/backend/Dockerfile . build backend -f services/backend/Dockerfile .
build worker -f services/worker/Dockerfile . build worker -f services/worker/Dockerfile .
build frontend services/frontend 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 - name: Clean up
if: always() if: always()
run: | run: |
docker logout "$REGISTRY" || true 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: deploy:
needs: build needs: build
@@ -135,7 +171,7 @@ jobs:
(grep -q '^TAG=' .env && sed -i 's/^TAG=.*/TAG=$tag/' .env || echo 'TAG=$tag' >> .env) && \ (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 pull -q && docker compose up -d --remove-orphans --wait && \
docker compose exec -T caddy caddy reload --config /etc/caddy/Caddyfile --adapter caddyfile && \ 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, # A release (and its tag, e.g. v2026.11.17-163612e) for every deployment,
# listing the commits since the previous one. # listing the commits since the previous one.
- name: Release - name: Release