Update to detect user type and pagination

This commit is contained in:
Huaaudio 2026-03-09 07:32:43 +01:00
parent 356587ca10
commit 4c1da506c1

View file

@ -166,11 +166,22 @@ jobs:
gh release delete "$old_tag" --cleanup-tag -y || true gh release delete "$old_tag" --cleanup-tag -y || true
fi fi
done done
echo "Determining GHCR owner type..."
OWNER_TYPE=$(gh api -H "Accept: application/vnd.github+json" \
/users/${{ github.repository_owner }} --jq '.type')
if [ "$OWNER_TYPE" = "Organization" ]; then
BASE_URL="/orgs/${{ github.repository_owner }}/packages/container"
else
BASE_URL="/users/${{ github.repository_owner }}/packages/container"
fi
PACKAGE_URL="$BASE_URL/${{ github.event.repository.name }}/versions"
echo "Cleaning up all untagged Docker images on GHCR..." echo "Cleaning up all untagged Docker images on GHCR..."
# Get all version IDs for the package that have no tags UNTAGGED_VERSIONS=$(gh api --paginate -H "Accept: application/vnd.github+json" \
UNTAGGED_VERSIONS=$(gh api -H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \ -H "X-GitHub-Api-Version: 2022-11-28" \
/user/packages/container/picoclaw/versions \ "$PACKAGE_URL" \
--jq '.[] | select((.metadata.container.tags | length) == 0) | .id' 2>/dev/null || true) --jq '.[] | select((.metadata.container.tags | length) == 0) | .id' 2>/dev/null || true)
for version_id in $UNTAGGED_VERSIONS; do for version_id in $UNTAGGED_VERSIONS; do
@ -178,22 +189,21 @@ jobs:
echo "Deleting untagged Docker image version ID: $version_id" echo "Deleting untagged Docker image version ID: $version_id"
gh api --method DELETE -H "Accept: application/vnd.github+json" \ gh api --method DELETE -H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \ -H "X-GitHub-Api-Version: 2022-11-28" \
"/user/packages/container/picoclaw/versions/$version_id" || true "$PACKAGE_URL/$version_id" || true
fi fi
done done
echo "Cleaning up old 'nightly-YYYYMMDD-SHA' Docker images on GHCR..." echo "Cleaning up old 'nightly-YYYYMMDD-SHA' Docker images on GHCR..."
# Fetch all version IDs where at least one tag starts with "nightly-", excluding the current newly created $TAG OLD_NIGHTLY_VERSIONS=$(gh api --paginate -H "Accept: application/vnd.github+json" \
OLD_NIGHTLY_VERSIONS=$(gh api -H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \ -H "X-GitHub-Api-Version: 2022-11-28" \
/user/packages/container/picoclaw/versions \ "$PACKAGE_URL" \
--jq ". | map(select(any(.metadata.container.tags[]; startswith(\"nightly-\") and (. != \"nightly\") and (. != \"$TAG\"))) | .id) | unique[]" 2>/dev/null || true) --jq ".[] | select(.metadata.container.tags[] | startswith(\"nightly-\") and (. != \"nightly\") and (. != \"$TAG\")) | .id" 2>/dev/null || true)
for version_id in $OLD_NIGHTLY_VERSIONS; do for version_id in $OLD_NIGHTLY_VERSIONS; do
if [ -n "$version_id" ]; then if [ -n "$version_id" ]; then
echo "Deleting old nightly Docker image version ID: $version_id" echo "Deleting old nightly Docker image version ID: $version_id"
gh api --method DELETE -H "Accept: application/vnd.github+json" \ gh api --method DELETE -H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \ -H "X-GitHub-Api-Version: 2022-11-28" \
"/user/packages/container/picoclaw/versions/$version_id" || true "$PACKAGE_URL/$version_id" || true
fi fi
done done