55 schedule :
66 - cron : ' 0 2 * * *' # Daily at 2 AM UTC
77 workflow_dispatch : # Allow manual triggering
8+ inputs :
9+ dry_run :
10+ description : ' Dry run mode (show what would be deleted without actually deleting)'
11+ required : false
12+ default : ' false'
13+ type : boolean
814
915# Set up permissions for Azure authentication
1016permissions :
6470 - name : Cleanup Old Container Images
6571 run : |
6672 acr_name="${{ steps.get-acr.outputs.acr_name }}"
73+ dry_run="${{ github.event.inputs.dry_run || 'false' }}"
6774
6875 if [ -z "$acr_name" ]; then
6976 echo "No Azure Container Registry found. Skipping cleanup."
7380 echo "Starting container image cleanup for ACR: $acr_name"
7481 echo "Retention policy: Keep 5 most recent images per repository"
7582
83+ if [ "$dry_run" = "true" ]; then
84+ echo "🔍 DRY RUN MODE: Will show what would be deleted without actually deleting"
85+ fi
86+
7687 # Get all repositories in the ACR
7788 repositories=$(az acr repository list --name "$acr_name" --output tsv 2>/dev/null)
7889
@@ -116,19 +127,27 @@ jobs:
116127 echo " Keeping: $repo:$tag"
117128 done
118129
119- echo " Deleting $delete_count old tags:"
120-
121- for tag in $tags_to_delete; do
122- echo " Deleting: $repo:$tag"
130+ if [ "$dry_run" = "true" ]; then
131+ echo " 🔍 Would delete $delete_count old tags (DRY RUN):"
132+ for tag in $tags_to_delete; do
133+ echo " Would delete: $repo:$tag"
134+ done
135+ ((total_deleted += delete_count))
136+ else
137+ echo " Deleting $delete_count old tags:"
123138
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
131- done
139+ for tag in $tags_to_delete; do
140+ echo " Deleting: $repo:$tag"
141+
142+ # Add error handling for individual deletions
143+ if az acr repository delete --name "$acr_name" --image "$repo:$tag" --yes 2>/dev/null; then
144+ echo " ✓ Successfully deleted $repo:$tag"
145+ ((total_deleted++))
146+ else
147+ echo " ✗ Failed to delete $repo:$tag (may no longer exist)"
148+ fi
149+ done
150+ fi
132151
133152 ((total_kept += 5))
134153 else
@@ -140,5 +159,10 @@ jobs:
140159 echo ""
141160 echo "=== Cleanup Summary ==="
142161 echo "Total images kept: $total_kept"
143- echo "Total images deleted: $total_deleted"
144- echo "Container image cleanup completed successfully!"
162+ if [ "$dry_run" = "true" ]; then
163+ echo "Total images that would be deleted: $total_deleted"
164+ echo "🔍 DRY RUN MODE: No images were actually deleted"
165+ else
166+ echo "Total images deleted: $total_deleted"
167+ echo "Container image cleanup completed successfully!"
168+ fi
0 commit comments