Skip to content

Commit 0cda0aa

Browse files
Copilotcsharpfritz
andcommitted
Enhance container cleanup action with better error handling and logging
Co-authored-by: csharpfritz <[email protected]>
1 parent 6dc9399 commit 0cda0aa

1 file changed

Lines changed: 49 additions & 11 deletions

File tree

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

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,23 @@ jobs:
4343
rg_name="rg-${{ env.AZURE_ENV_NAME }}"
4444
echo "Resource group: $rg_name"
4545
46+
# Verify the resource group exists
47+
if ! az group show --name "$rg_name" >/dev/null 2>&1; then
48+
echo "Resource group '$rg_name' not found. Skipping cleanup."
49+
echo "acr_name=" >> $GITHUB_OUTPUT
50+
exit 0
51+
fi
52+
4653
# Get the ACR name from the resource group
47-
acr_name=$(az acr list --resource-group "$rg_name" --query "[0].name" --output tsv)
48-
echo "ACR Name: $acr_name"
49-
echo "acr_name=$acr_name" >> $GITHUB_OUTPUT
54+
acr_name=$(az acr list --resource-group "$rg_name" --query "[0].name" --output tsv 2>/dev/null)
55+
56+
if [ -z "$acr_name" ] || [ "$acr_name" = "null" ]; then
57+
echo "No Azure Container Registry found in resource group '$rg_name'."
58+
echo "acr_name=" >> $GITHUB_OUTPUT
59+
else
60+
echo "Found ACR: $acr_name"
61+
echo "acr_name=$acr_name" >> $GITHUB_OUTPUT
62+
fi
5063
5164
- name: Cleanup Old Container Images
5265
run: |
@@ -57,23 +70,30 @@ jobs:
5770
exit 0
5871
fi
5972
60-
echo "Cleaning up old images in ACR: $acr_name"
73+
echo "Starting container image cleanup for ACR: $acr_name"
74+
echo "Retention policy: Keep 5 most recent images per repository"
6175
6276
# Get all repositories in the ACR
63-
repositories=$(az acr repository list --name "$acr_name" --output tsv)
77+
repositories=$(az acr repository list --name "$acr_name" --output tsv 2>/dev/null)
6478
6579
if [ -z "$repositories" ]; then
6680
echo "No repositories found in ACR. Nothing to clean up."
6781
exit 0
6882
fi
6983
84+
total_deleted=0
85+
total_kept=0
86+
7087
# Process each repository
88+
echo "Found repositories: $(echo "$repositories" | wc -l)"
7189
for repo in $repositories; do
90+
echo ""
7291
echo "Processing repository: $repo"
7392
7493
# Get all tags for this repository, sorted by creation time (newest first)
94+
# Include detailed information to help with debugging
7595
tags=$(az acr repository show-tags --name "$acr_name" --repository "$repo" \
76-
--orderby time_desc --output tsv)
96+
--orderby time_desc --output tsv 2>/dev/null)
7797
7898
if [ -z "$tags" ]; then
7999
echo " No tags found for repository $repo"
@@ -87,20 +107,38 @@ jobs:
87107
# If we have more than 5 tags, delete the older ones
88108
if [ "$tag_count" -gt 5 ]; then
89109
# Skip the first 5 tags (most recent) and delete the rest
110+
tags_to_keep=$(echo "$tags" | head -5)
90111
tags_to_delete=$(echo "$tags" | tail -n +6)
91112
delete_count=$(echo "$tags_to_delete" | wc -l)
92113
93-
echo " Deleting $delete_count old tags from repository $repo"
114+
echo " Keeping 5 most recent tags:"
115+
for tag in $tags_to_keep; do
116+
echo " Keeping: $repo:$tag"
117+
done
118+
119+
echo " Deleting $delete_count old tags:"
94120
95121
for tag in $tags_to_delete; do
96-
echo " Deleting tag: $repo:$tag"
97-
az acr repository delete --name "$acr_name" --image "$repo:$tag" --yes
122+
echo " Deleting: $repo:$tag"
123+
124+
# Add error handling for individual deletions
125+
if az acr repository delete --name "$acr_name" --image "$repo:$tag" --yes 2>/dev/null; then
126+
echo " ✓ Successfully deleted $repo:$tag"
127+
((total_deleted++))
128+
else
129+
echo " ✗ Failed to delete $repo:$tag (may no longer exist)"
130+
fi
98131
done
99132
100-
echo " Successfully deleted $delete_count old tags from repository $repo"
133+
((total_kept += 5))
101134
else
102135
echo " Repository $repo has $tag_count tags (≤5), no cleanup needed"
136+
((total_kept += tag_count))
103137
fi
104138
done
105139
106-
echo "Container image cleanup completed!"
140+
echo ""
141+
echo "=== Cleanup Summary ==="
142+
echo "Total images kept: $total_kept"
143+
echo "Total images deleted: $total_deleted"
144+
echo "Container image cleanup completed successfully!"

0 commit comments

Comments
 (0)