Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,36 @@ jobs:

- name: Delete untagged images from GHCR
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
uses: actions/delete-package-versions@v4
with:
package-name: 'net-utils'
package-type: 'container'
min-versions-to-keep: 5
delete-only-untagged-versions: 'true'
env:
TOKEN: ${{ secrets.GH_PAT }}
GH_TOKEN: ${{ secrets.GH_PAT }}
PACKAGE_NAME: net-utils
OWNER: ${{ github.repository_owner }}
run: |
set -euo pipefail

function check_package() {
local path="$1"
if gh api -H "Accept: application/vnd.github+json" "$path" >/dev/null 2>&1; then
echo "$path"
return 0
fi
return 1
}

PACKAGE_API=""
if PACKAGE_API=$(check_package "/orgs/${OWNER}/packages/container/${PACKAGE_NAME}" 2>/dev/null); then
:
elif PACKAGE_API=$(check_package "/users/${OWNER}/packages/container/${PACKAGE_NAME}" 2>/dev/null); then
:
else
echo "Package not found in org or user scope, skipping cleanup."
exit 0
fi

echo "Cleaning untagged versions for package: ${PACKAGE_API}"
gh api -H "Accept: application/vnd.github+json" "${PACKAGE_API}/versions?per_page=100" --paginate --jq '.[] | select(.metadata.container.tags | length == 0) | .id' | while read -r version_id; do
if [[ -n "$version_id" ]]; then
echo "Deleting untagged version ${version_id}"
gh api -X DELETE -H "Accept: application/vnd.github+json" "${PACKAGE_API}/versions/${version_id}"
fi
done
Loading