@@ -25,22 +25,26 @@ jobs:
2525 sudo apt-get update
2626 sudo apt-get install -y gh
2727
28- - name : Delete non-latest versions
28+ - name : Delete all but the newest version
2929 env :
3030 OWNER : ${{ github.repository_owner }}
3131 IMAGE : deep-learning-crash-course
3232 run : |
33- echo "Fetching all versions of $OWNER/$IMAGE…"
34- gh api -H "Accept: application/vnd.github.v3+json" \
35- /orgs/$OWNER/packages/container/$IMAGE/versions \
36- | jq -c '.[]' \
37- | while read version; do
38- id=$(echo "$version" | jq -r '.id')
39- tags=$(echo "$version" | jq -r '.metadata.container.tags[]')
40- if [[ ! " $tags " =~ " latest " ]]; then
41- echo "Deleting version $id (tags: $tags)"
42- gh api -X DELETE \
43- -H "Accept: application/vnd.github.v3+json" \
44- /orgs/$OWNER/packages/container/$IMAGE/versions/$id
45- fi
46- done
33+ echo "Fetching all versions of $OWNER/$IMAGE..."
34+ versions=$(gh api --paginate -H "Accept: application/vnd.github.v3+json" \
35+ /orgs/$OWNER/packages/container/$IMAGE/versions)
36+ # find the newest by creation timestamp
37+ newest_id=$(echo "$versions" \
38+ | jq -r 'sort_by(.created_at) | reverse | .[0].id')
39+ echo "🛡 Keeping version $newest_id (most recent)"
40+ # loop through all and delete those that aren't the newest
41+ echo "$versions" | jq -c '.[]' | while read ver; do
42+ id=$(echo "$ver" | jq -r '.id')
43+ if [ "$id" != "$newest_id" ]; then
44+ echo "Deleting version $id"
45+ gh api -X DELETE \
46+ -H "Accept: application/vnd.github.v3+json" \
47+ /orgs/$OWNER/packages/container/$IMAGE/versions/$id
48+ fi
49+ done
50+
0 commit comments