65 lines
2.3 KiB
Django/Jinja
65 lines
2.3 KiB
Django/Jinja
#!/bin/bash
|
|
# Forgejo container registry cleanup: keep only the latest 2 image tags per repo
|
|
set -euo pipefail
|
|
|
|
TOKEN="{{ forgejo_admin_password }}"
|
|
BASE="http://127.0.0.1:3000"
|
|
USER="{{ forgejo_admin_user }}"
|
|
|
|
# Get a real API token
|
|
API_TOKEN=$(curl -s -X POST "$BASE/api/v1/users/$USER/tokens" \
|
|
-u "$TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"name":"cleanup-'"$(date +%s)"'","scopes":["all"]}' \
|
|
| python3 -c "import sys,json; print(json.load(sys.stdin).get('sha1',''))" 2>/dev/null)
|
|
|
|
if [ -z "$API_TOKEN" ]; then
|
|
echo "Failed to get API token"
|
|
exit 1
|
|
fi
|
|
|
|
# List all repos
|
|
repos=$(curl -s -H "Authorization: token $API_TOKEN" "$BASE/api/v1/user/repos?limit=100" \
|
|
| python3 -c "import sys,json; [print(r['name']) for r in json.load(sys.stdin)]" 2>/dev/null)
|
|
|
|
for repo in $repos; do
|
|
# List docker packages in the repo
|
|
pkgs=$(curl -s -H "Authorization: token $API_TOKEN" \
|
|
"$BASE/api/v1/packages/$USER/$repo?type=docker" 2>/dev/null \
|
|
| python3 -c "import sys,json;d=json.load(sys.stdin);[print(p['id'],p['name']) for p in d]" 2>/dev/null)
|
|
|
|
while IFS= read -r line; do
|
|
pkg_id=$(echo "$line" | awk '{print $1}')
|
|
pkg_name=$(echo "$line" | awk '{$1=""; print $0}' | sed 's/^ //')
|
|
[ -z "$pkg_id" ] && continue
|
|
|
|
# Get all versions of this package
|
|
versions=$(curl -s -H "Authorization: token $API_TOKEN" \
|
|
"$BASE/api/v1/packages/$USER/$repo/$pkg_id/versions?page=-1" 2>/dev/null \
|
|
| python3 -c "
|
|
import sys,json
|
|
d=json.load(sys.stdin)
|
|
if isinstance(d,list):
|
|
for v in sorted(d, key=lambda x: x.get('created_at',0), reverse=True):
|
|
print(v['id'], v['version'])
|
|
" 2>/dev/null)
|
|
|
|
count=$(echo "$versions" | wc -l)
|
|
if [ "$count" -le 2 ]; then
|
|
continue
|
|
fi
|
|
|
|
# Keep the first 2 (latest), delete the rest
|
|
echo "$versions" | tail -n +3 | while IFS= read -r vline; do
|
|
ver_id=$(echo "$vline" | awk '{print $1}')
|
|
echo "Deleting $pkg_name version $ver_id (keeping latest 2 of $count)"
|
|
curl -s -X DELETE -H "Authorization: token $API_TOKEN" \
|
|
"$BASE/api/v1/packages/$USER/$repo/$pkg_id/versions/$ver_id" -o /dev/null -w '%{http_code}'
|
|
echo ""
|
|
done
|
|
done <<< "$pkgs"
|
|
done
|
|
|
|
# Clean up the temporary token
|
|
curl -s -X DELETE -H "Authorization: token $API_TOKEN" \
|
|
"$BASE/api/v1/users/$USER/tokens/cleanup-$(date +%s)" -o /dev/null 2>/dev/null || true
|