Skip to content

Commit 004bf6c

Browse files
committed
fix: remove images
1 parent 00232e6 commit 004bf6c

1 file changed

Lines changed: 17 additions & 19 deletions

File tree

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

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,48 +104,46 @@ jobs:
104104
AWS_REGION: ${{ inputs.environment == 'DEV' && secrets.devRegion || inputs.environment == 'PROD' && secrets.prodRegion }}
105105
run: |
106106
echo "Fetching untagged images..."
107-
108-
IMAGES=$(aws ecr list-images \
107+
IMAGES_JSON=$(aws ecr list-images \
109108
--region "${AWS_REGION}" \
110109
--repository-name lambda-perf \
111110
--filter "tagStatus=UNTAGGED" \
112111
--query 'imageIds' \
113112
--output json)
114113
115-
COUNT=$(echo "$IMAGES" | jq length)
116-
114+
COUNT=$(echo "$IMAGES_JSON" | jq length)
117115
if (( COUNT == 0 )); then
118116
echo "No untagged images found. Skipping deletion."
119117
exit 0
120118
fi
121119
122120
echo "Found $COUNT untagged images. Deleting in batches of 100..."
123121
124-
# Process each imageId as a separate JSON object
125-
echo "$IMAGES" | jq -c '.[]' | while read -r IMG; do
126-
BATCH="$BATCH$IMG"$'\n'
127-
((N++))
122+
# read all images into an array
123+
mapfile -t IMAGES_ARRAY < <(echo "$IMAGES_JSON" | jq -c '.[]')
128124
129-
# if we reached 100, delete
130-
if (( N == 100 )); then
131-
BATCH_JSON=$(echo "$BATCH" | jq -s '.')
132-
echo "Deleting a batch of 100 images..."
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..."
133132
aws ecr batch-delete-image \
134133
--region "${AWS_REGION}" \
135134
--repository-name lambda-perf \
136135
--image-ids "$BATCH_JSON"
137-
BATCH=""
138-
N=0
136+
BATCH=()
139137
fi
140138
done
141139
142-
# delete leftover images (less than 100)
143-
if [[ -n "$BATCH" ]]; then
144-
BATCH_JSON=$(echo "$BATCH" | jq -s '.')
145-
LEFT_COUNT=$(echo "$BATCH_JSON" | jq length)
140+
# delete leftover images
141+
if (( ${#BATCH[@]} > 0 )); then
142+
BATCH_JSON=$(printf '%s\n' "${BATCH[@]}" | jq -s '.')
143+
LEFT_COUNT=${#BATCH[@]}
146144
echo "Deleting final batch of $LEFT_COUNT images..."
147145
aws ecr batch-delete-image \
148146
--region "${AWS_REGION}" \
149147
--repository-name lambda-perf \
150148
--image-ids "$BATCH_JSON"
151-
fi
149+
fi

0 commit comments

Comments
 (0)