my-ostree-OS/.forgejo/workflows/cleanup.yml

43 lines
1.6 KiB
YAML
Raw Normal View History

2026-05-07 22:30:48 -03:00
name: Clean up Forgejo Container Registry
2026-05-07 22:21:19 -03:00
on:
schedule:
2026-05-07 22:30:48 -03:00
- cron: '0 0 * * 0' # Runs every Sunday at midnight
workflow_dispatch: # Allows manual triggering
2026-05-07 22:21:19 -03:00
jobs:
delete-old-images:
runs-on: almalinux-10
steps:
2026-05-07 22:30:48 -03:00
- name: Delete old versions
2026-05-07 22:21:19 -03:00
run: |
REGISTRY="forge.pc-rytteren.dk"
OWNER="${{ github.repository_owner }}"
IMAGE="my-ostree-os"
MIN_KEEP=50
TOKEN="${{ secrets.PACKAGE_TOKEN }}"
2026-05-07 22:30:48 -03:00
# Fetch all container packages with this name, sorted oldest first (by id)
# API returns a list of package objects, each with "id" and "version"
2026-05-07 22:27:29 -03:00
RAW=$(curl -s -H "Authorization: token ${TOKEN}" \
"https://${REGISTRY}/api/v1/packages/${OWNER}?type=container&q=${IMAGE}&limit=200")
2026-05-07 22:25:27 -03:00
2026-05-07 22:30:48 -03:00
# Filter only packages with the correct name and extract id, sort oldest first
2026-05-07 22:27:29 -03:00
IDS=$(echo "$RAW" | tr '{' '\n' | grep "\"name\":\"${IMAGE}\"" | \
sed 's/.*"id":\([0-9]*\).*/\1/' | sort -n)
2026-05-07 22:21:19 -03:00
2026-05-07 22:27:29 -03:00
TOTAL=$(echo "$IDS" | grep -c '[0-9]' || true)
2026-05-07 22:30:48 -03:00
echo "Found ${TOTAL} packages total, keeping ${MIN_KEEP}"
2026-05-07 22:21:19 -03:00
if [ "$TOTAL" -le "$MIN_KEEP" ]; then
2026-05-07 22:30:48 -03:00
echo "No packages to delete"
2026-05-07 22:21:19 -03:00
exit 0
fi
2026-05-07 22:30:48 -03:00
# Delete the oldest (lowest ids) beyond MIN_KEEP
2026-05-07 22:27:29 -03:00
TO_DELETE=$(echo "$IDS" | head -n $(( TOTAL - MIN_KEEP )))
for ID in $TO_DELETE; do
2026-05-07 22:30:48 -03:00
echo "Deleting package id: ${ID}"
2026-05-07 22:21:19 -03:00
curl -s -X DELETE -H "Authorization: token ${TOKEN}" \
2026-05-07 22:27:29 -03:00
"https://${REGISTRY}/api/v1/packages/${OWNER}/${ID}"
2026-05-07 22:21:19 -03:00
done