diff --git a/.forgejo/workflows/cleanup.yml b/.forgejo/workflows/cleanup.yml new file mode 100644 index 0000000..61d7bee --- /dev/null +++ b/.forgejo/workflows/cleanup.yml @@ -0,0 +1,39 @@ +name: Ryd op i Forgejo Container Registry + +on: + schedule: + - cron: '0 0 * * 0' # Kører hver søndag ved midnat + workflow_dispatch: # Gør det muligt at køre den manuelt + +jobs: + delete-old-images: + runs-on: almalinux-10 + steps: + - name: Slet gamle versioner + run: | + REGISTRY="forge.pc-rytteren.dk" + OWNER="${{ github.repository_owner }}" + IMAGE="my-ostree-os" + MIN_KEEP=50 + TOKEN="${{ secrets.PACKAGE_TOKEN }}" + + # Hent alle tags for imaget via Forgejo API + TAGS=$(curl -s -H "Authorization: token ${TOKEN}" \ + "https://${REGISTRY}/api/v1/packages/${OWNER}/container/${IMAGE}/tags" \ + | tr ',' '\n' | grep '"name"' | sed 's/.*"name":"\([^"]*\)".*/\1/' | sort -r) + + TOTAL=$(echo "$TAGS" | wc -l) + echo "Fundet ${TOTAL} tags totalt, beholder ${MIN_KEEP}" + + if [ "$TOTAL" -le "$MIN_KEEP" ]; then + echo "Ingen tags at slette" + exit 0 + fi + + # Slet tags ud over MIN_KEEP (ældste først) + TO_DELETE=$(echo "$TAGS" | tail -n +$((MIN_KEEP + 1))) + for TAG in $TO_DELETE; do + echo "Sletter tag: ${TAG}" + curl -s -X DELETE -H "Authorization: token ${TOKEN}" \ + "https://${REGISTRY}/api/v1/packages/${OWNER}/container/${IMAGE}/${TAG}" + done \ No newline at end of file