Skip to content

Commit 92f9c2f

Browse files
committed
enhance: improve error handling and retry logic for container image deletions
1 parent bc4bc8d commit 92f9c2f

1 file changed

Lines changed: 39 additions & 8 deletions

File tree

.github/workflows/cleanup-container-images.yml

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ jobs:
9191
9292
total_deleted=0
9393
total_kept=0
94+
total_failed=0
9495
9596
# Process each repository
9697
echo "Found repositories: $(echo "$repositories" | wc -l)"
@@ -136,13 +137,30 @@ jobs:
136137
for tag in $tags_to_delete; do
137138
echo " Deleting: $repo:$tag"
138139
139-
# Add error handling for individual deletions
140-
if az acr repository delete --name "$acr_name" --image "$repo:$tag" --yes 2>/dev/null; then
141-
echo " ✓ Successfully deleted $repo:$tag"
142-
((total_deleted++))
143-
else
144-
echo " ✗ Failed to delete $repo:$tag (may no longer exist)"
145-
fi
140+
# Add better error handling and retry logic for deletions
141+
max_retries=3
142+
retry_count=0
143+
success=false
144+
145+
while [ $retry_count -lt $max_retries ] && [ "$success" = "false" ]; do
146+
if [ $retry_count -gt 0 ]; then
147+
echo " Retry attempt $retry_count for $repo:$tag"
148+
sleep 5
149+
fi
150+
151+
if az acr repository delete --name "$acr_name" --image "$repo:$tag" --yes >/dev/null 2>&1; then
152+
echo " ✓ Successfully deleted $repo:$tag"
153+
success=true
154+
((total_deleted++))
155+
break
156+
else
157+
((retry_count++))
158+
if [ $retry_count -eq $max_retries ]; then
159+
echo " ✗ Failed to delete $repo:$tag after $max_retries attempts"
160+
((total_failed++))
161+
fi
162+
fi
163+
done
146164
done
147165
fi
148166
@@ -161,5 +179,18 @@ jobs:
161179
echo "🔍 DRY RUN MODE: No images were actually deleted"
162180
else
163181
echo "Total images deleted: $total_deleted"
164-
echo "Container image cleanup completed successfully!"
182+
echo "Total images failed to delete: $total_failed"
183+
echo "Container image cleanup completed!"
184+
fi
185+
186+
# Exit with error only if all deletions failed
187+
if [ "$total_failed" -gt 0 ] && [ "$total_deleted" -eq 0 ]; then
188+
echo "❌ All deletion attempts failed. Please check ACR permissions and image locks."
189+
exit 1
190+
fi
191+
192+
# Exit successfully if we deleted at least some images
193+
if [ "$total_deleted" -gt 0 ] || [ "$dry_run" = "true" ]; then
194+
echo "✅ Cleanup process completed successfully!"
195+
exit 0
165196
fi

0 commit comments

Comments
 (0)