2828 sudo apt-get update
2929 sudo apt-get install -y gh
3030
31- - name : Re-tag newest as “latest” and prune the rest
31+ - name : Delete all but the newest version for CPU & GPU images
3232 env :
3333 GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
3434 OWNER : ${{ github.repository_owner }}
@@ -37,27 +37,32 @@ jobs:
3737
3838 for IMAGE in deep-learning-crash-course deep-learning-crash-course-gpu; do
3939 echo
40- echo "🔍 Processing $OWNER/$IMAGE"
40+ echo "🔍 Processing package: $OWNER/$IMAGE"
4141 versions=$(gh api --paginate \
4242 -H "Accept: application/vnd.github.v3+json" \
4343 /orgs/$OWNER/packages/container/$IMAGE/versions)
4444
45- # pick the newest by creation date
45+ # pick most recent by creation date
4646 newest_id=$(echo "$versions" \
47- | jq -r 'sort_by(.created_at) | last | .id')
48- echo "🛡 Retagging version $newest_id as latest"
49- gh api -X POST \
50- -H "Accept: application/vnd.github.v3+json" \
51- /orgs/$OWNER/packages/container/$IMAGE/versions/$newest_id/tags \
52- -f name=latest
53-
54- # delete all other versions
55- echo "$versions" \
56- | jq -r --arg keep "$newest_id" '.[] | select(.id != ($keep|tonumber)) | .id' \
57- | while read id; do
58- echo "→ Deleting version $id"
59- gh api -X DELETE \
60- -H "Accept: application/vnd.github.v3+json" \
61- /orgs/$OWNER/packages/container/$IMAGE/versions/$id
47+ | jq -r 'sort_by(.created_at) | reverse | .[0].id')
48+ echo "🛡 Keeping version $newest_id for $IMAGE"
49+
50+ # delete every version except the newest
51+ echo "$versions" | jq -c '.[]' | while read version; do
52+ id=$(echo "$version" | jq -r '.id')
53+ if [[ "$id" != "$newest_id" ]]; then
54+ echo "→ Deleting version $id of $IMAGE"
55+ for attempt in 1 2 3; do
56+ if gh api -X DELETE \
57+ -H "Accept: application/vnd.github.v3+json" \
58+ /orgs/$OWNER/packages/container/$IMAGE/versions/$id; then
59+ echo " ✅ Deleted $id"
60+ break
61+ else
62+ echo " ⚠️ Attempt $attempt failed, retrying in $((5*attempt))s…"
63+ sleep $((5*attempt))
64+ fi
6265 done
66+ fi
67+ done
6368 done
0 commit comments