@@ -187,16 +187,36 @@ runs:
187187 configFile : ${{ inputs.action_path }}/.devcontainer.json
188188 push : ${{ env.RERUN_RUNNER == 'true' && 'always' || 'never' }}
189189
190- - name : 💎 Variables
190+ - name : 🗑️ Delete 15 Oldest Docker Images
191191 id : variables
192192 shell : bash
193+ env :
194+ MAX_DELETIONS : 15
193195 run : |
194- BASE_NAME=$(basename ${{ inputs.image_name }})
195- IFS=', '; array=($(curl -L -s 'https://registry.hub.docker.com/v2/repositories/${{ inputs.image_name }}/tags?page_size=1024' | jq -rc '."results"[]["name"]' | yq eval -P | sed "s/ /, /g"))
196- for ((i=0; i < ${#array[@]}; i++)); do
197- if [ "${array[$i]}" != "latest" ]; then
198- HUB_TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d "{\"username\": \"${{ inputs.hub_username }}\", \"password\": \"${{ inputs.hub_password }}\"}" https://hub.docker.com/v2/users/login/ | jq -r .token)
199- curl -s -i -X DELETE -H "Accept: application/json" -H "Authorization: JWT $HUB_TOKEN" https://hub.docker.com/v2/namespaces/${{ inputs.hub_username }}/repositories/$BASE_NAME/tags/${array[$i]}
200- echo "deleted: ${{ inputs.image_name }}:${array[$i]}"
196+ echo "Fetching manifests to delete..."
197+ HUB_TOKEN=$(curl -s -H "Content-Type: application/json" -X POST \
198+ -d "{\"username\": \"${{ inputs.hub_username }}\", \"password\": \"${{ inputs.hub_password }}\"}" \
199+ https://hub.docker.com/v2/users/login/ | jq -r .token)
200+ [ -z "$HUB_TOKEN" ] && { echo "❌ Authentication failed"; exit 1; }
201+
202+ MANIFESTS=$(curl -s -H "Authorization: JWT $HUB_TOKEN" \
203+ "https://hub.docker.com/v2/repositories/${{ inputs.image_name }}/tags/?page_size=${{ env.MAX_DELETIONS }}&ordering=last_updated" \
204+ | jq -r '.results[] | select(.images != null) | .images[].digest' \
205+ | sort | uniq | head -n ${{ env.MAX_DELETIONS }})
206+
207+ DELETED_MANIFESTS=0
208+ for SHA in $MANIFESTS; do
209+ echo "Deleting manifest ${SHA:0:12}..."
210+ RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE \
211+ -H "Authorization: JWT $HUB_TOKEN" \
212+ "https://hub.docker.com/v2/repositories/${{ inputs.image_name }}/manifests/$SHA")
213+
214+ if [ "$RESPONSE" -eq 202 ]; then
215+ ((DELETED_MANIFESTS++))
216+ echo "✅ Deleted manifest"
217+ sleep 1
218+ else
219+ echo "❌ Failed to delete (HTTP $RESPONSE)"
201220 fi
202221 done
222+ echo "Deleted manifests: $DELETED_MANIFESTS"
0 commit comments