Skip to content

Commit de0448f

Browse files
committed
enhance: refactor container image cleanup logic and improve purge command execution
1 parent 13107a0 commit de0448f

1 file changed

Lines changed: 23 additions & 100 deletions

File tree

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

Lines changed: 23 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ jobs:
6464
echo "Found ACR: $acr_name"
6565
echo "acr_name=$acr_name" >> $GITHUB_OUTPUT
6666
fi
67-
67+
6868
- name: Cleanup Old Container Images
6969
run: |
7070
acr_name="${{ steps.get-acr.outputs.acr_name }}"
@@ -78,10 +78,6 @@ jobs:
7878
echo "Starting container image cleanup for ACR: $acr_name"
7979
echo "Retention policy: Keep 5 most recent images per repository"
8080
81-
if [ "$dry_run" = "true" ]; then
82-
echo "🔍 DRY RUN MODE: Will show what would be deleted without actually deleting"
83-
fi
84-
8581
# Get all repositories in the ACR
8682
repositories=$(az acr repository list --name "$acr_name" --output tsv 2>/dev/null)
8783
@@ -90,108 +86,35 @@ jobs:
9086
exit 0
9187
fi
9288
93-
total_deleted=0
94-
total_kept=0
95-
total_failed=0
96-
97-
# Process each repository
9889
echo "Found repositories: $(echo "$repositories" | wc -l)"
90+
91+
# Build the purge command with filters for each repository
92+
PURGE_FILTERS=""
9993
for repo in $repositories; do
100-
echo ""
101-
echo "Processing repository: $repo"
102-
103-
# Get all tags for this repository, sorted by creation time (newest first)
104-
# Include detailed information to help with debugging
105-
tags=$(az acr repository show-tags --name "$acr_name" --repository "$repo" \
106-
--orderby time_desc --output tsv 2>/dev/null)
107-
108-
if [ -z "$tags" ]; then
109-
echo " No tags found for repository $repo"
110-
continue
111-
fi
112-
113-
# Count total tags
114-
tag_count=$(echo "$tags" | wc -l)
115-
echo " Found $tag_count tags in repository $repo"
116-
117-
# If we have more than 5 tags, delete the older ones
118-
if [ "$tag_count" -gt 5 ]; then
119-
# Skip the first 5 tags (most recent) and delete the rest
120-
tags_to_keep=$(echo "$tags" | head -5)
121-
tags_to_delete=$(echo "$tags" | tail -n +6)
122-
delete_count=$(echo "$tags_to_delete" | wc -l)
123-
124-
echo " Keeping 5 most recent tags:"
125-
for tag in $tags_to_keep; do
126-
echo " Keeping: $repo:$tag"
127-
done
128-
129-
if [ "$dry_run" = "true" ]; then
130-
echo " 🔍 Would delete $delete_count old tags (DRY RUN):"
131-
for tag in $tags_to_delete; do
132-
echo " Would delete: $repo:$tag"
133-
done
134-
((total_deleted += delete_count))
135-
else
136-
echo " Deleting $delete_count old tags:"
137-
138-
for tag in $tags_to_delete; do
139-
echo " Deleting: $repo:$tag"
140-
141-
# Add better error handling and retry logic for deletions
142-
max_retries=3
143-
retry_count=0
144-
success=false
145-
146-
while [ $retry_count -lt $max_retries ] && [ "$success" = "false" ]; do
147-
if [ $retry_count -gt 0 ]; then
148-
echo " Retry attempt $retry_count for $repo:$tag"
149-
sleep 5
150-
fi
151-
152-
if az acr repository delete --name "$acr_name" --image "$repo:$tag" --yes >/dev/null 2>&1; then
153-
echo " ✓ Successfully deleted $repo:$tag"
154-
success=true
155-
((total_deleted++))
156-
break
157-
else
158-
((retry_count++))
159-
if [ $retry_count -eq $max_retries ]; then
160-
echo " ✗ Failed to delete $repo:$tag after $max_retries attempts"
161-
((total_failed++))
162-
fi
163-
fi
164-
done
165-
done
166-
fi
167-
168-
((total_kept += 5))
169-
else
170-
echo " Repository $repo has $tag_count tags (≤5), no cleanup needed"
171-
((total_kept += tag_count))
172-
fi
94+
PURGE_FILTERS="$PURGE_FILTERS --filter '$repo:.*'"
17395
done
17496
175-
echo ""
176-
echo "=== Cleanup Summary ==="
177-
echo "Total images kept: $total_kept"
97+
# Construct the purge command
98+
PURGE_CMD="acr purge $PURGE_FILTERS --ago 0d --keep 5 --untagged"
99+
178100
if [ "$dry_run" = "true" ]; then
179-
echo "Total images that would be deleted: $total_deleted"
180-
echo "🔍 DRY RUN MODE: No images were actually deleted"
181-
else
182-
echo "Total images deleted: $total_deleted"
183-
echo "Total images failed to delete: $total_failed"
184-
echo "Container image cleanup completed!"
101+
PURGE_CMD="$PURGE_CMD --dry-run"
102+
echo "🔍 DRY RUN MODE: Will show what would be deleted without actually deleting"
185103
fi
186104
187-
# Exit with error only if all deletions failed
188-
if [ "$total_failed" -gt 0 ] && [ "$total_deleted" -eq 0 ]; then
189-
echo "❌ All deletion attempts failed. Please check ACR permissions and image locks."
190-
exit 1
191-
fi
105+
echo "Running purge command..."
106+
# Set longer timeout (1 hour) for large registries
107+
az acr run \
108+
--cmd "$PURGE_CMD" \
109+
--registry "$acr_name" \
110+
--timeout 3600 \
111+
/dev/null
192112
193-
# Exit successfully if we deleted at least some images
194-
if [ "$total_deleted" -gt 0 ] || [ "$dry_run" = "true" ]; then
113+
exit_code=$?
114+
115+
if [ $exit_code -eq 0 ]; then
195116
echo "✅ Cleanup process completed successfully!"
196-
exit 0
117+
else
118+
echo "❌ Cleanup process failed with exit code $exit_code"
119+
exit $exit_code
197120
fi

0 commit comments

Comments
 (0)