Skip to content

Commit ceddb3a

Browse files
authored
fix: clear ecr batch (#3378)
* feat: add logs * fix: ecr * fix: ecr * fix: remove images * fix: remove images * fix: remove images
1 parent 5c4ab37 commit ceddb3a

1 file changed

Lines changed: 41 additions & 4 deletions

File tree

.github/workflows/push-to-ecr.yml

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,47 @@ jobs:
103103
env:
104104
AWS_REGION: ${{ inputs.environment == 'DEV' && secrets.devRegion || inputs.environment == 'PROD' && secrets.prodRegion }}
105105
run: |
106-
UNTAGGED_IMAGES=$(aws ecr list-images --region "${AWS_REGION}" --repository-name lambda-perf --filter "tagStatus=UNTAGGED" --query 'imageIds[*]' --output json)
107-
if [[ "$UNTAGGED_IMAGES" == "[]" ]]; then
106+
echo "Fetching untagged images..."
107+
IMAGES_JSON=$(aws ecr list-images \
108+
--region "${AWS_REGION}" \
109+
--repository-name lambda-perf \
110+
--filter "tagStatus=UNTAGGED" \
111+
--query 'imageIds' \
112+
--output json)
113+
114+
COUNT=$(echo "$IMAGES_JSON" | jq length)
115+
if (( COUNT == 0 )); then
108116
echo "No untagged images found. Skipping deletion."
109-
else
110-
aws ecr batch-delete-image --region "${AWS_REGION}" --repository-name lambda-perf --image-ids "$UNTAGGED_IMAGES"
117+
exit 0
111118
fi
112119
120+
echo "Found $COUNT untagged images. Deleting in batches of 100..."
121+
122+
# read all images into an array
123+
mapfile -t IMAGES_ARRAY < <(echo "$IMAGES_JSON" | jq -c '.[]')
124+
125+
BATCH=()
126+
for IMG in "${IMAGES_ARRAY[@]}"; do
127+
BATCH+=("$IMG")
128+
129+
if (( ${#BATCH[@]} == 100 )); then
130+
BATCH_JSON=$(printf '%s\n' "${BATCH[@]}" | jq -s '.')
131+
echo "Deleting batch of 100 images..."
132+
aws ecr batch-delete-image \
133+
--region "${AWS_REGION}" \
134+
--repository-name lambda-perf \
135+
--image-ids "$BATCH_JSON"
136+
BATCH=()
137+
fi
138+
done
139+
140+
# delete leftover images
141+
if (( ${#BATCH[@]} > 0 )); then
142+
BATCH_JSON=$(printf '%s\n' "${BATCH[@]}" | jq -s '.')
143+
LEFT_COUNT=${#BATCH[@]}
144+
echo "Deleting final batch of $LEFT_COUNT images..."
145+
aws ecr batch-delete-image \
146+
--region "${AWS_REGION}" \
147+
--repository-name lambda-perf \
148+
--image-ids "$BATCH_JSON"
149+
fi

0 commit comments

Comments
 (0)