Skip to content

Commit 4c98ab1

Browse files
committed
Pruning old cache images
1 parent 9e8b1db commit 4c98ab1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

rocksteady

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,30 @@ sub_build() {
205205
buildx_cmd+=( --push --progress=plain )
206206

207207
"${buildx_cmd[@]}"
208+
prune_stale_cache_artifacts
209+
}
210+
211+
prune_stale_cache_artifacts() {
212+
# Stale cache artifacts become untagged images in the ECR repository when new ones are pushed.
213+
echo "Pruning untagged ECR cache artifacts for repository $name"
214+
215+
local digests
216+
digests=$(AWS_ACCESS_KEY_ID=$aws_access_key_id AWS_SECRET_ACCESS_KEY=$aws_secret_access_key aws ecr list-images --repository-name "$name" --region="$aws_region" --filter tagStatus=UNTAGGED --query 'imageIds[].imageDigest' --output text)
217+
218+
if [ -z "$digests" ]; then
219+
echo "No untagged cache artifacts to prune"
220+
return 0
221+
fi
222+
223+
local args=()
224+
for digest in $digests; do
225+
args+=( imageDigest=$digest )
226+
done
227+
228+
local count=$(echo $digests | wc -w | tr -d ' ')
229+
230+
echo "Deleting $count untagged cache artifacts"
231+
AWS_ACCESS_KEY_ID=$aws_access_key_id AWS_SECRET_ACCESS_KEY=$aws_secret_access_key aws ecr batch-delete-image --repository-name "$name" --region="$aws_region" --image-ids "${args[@]}"
208232
}
209233

210234
sub_deploy() {

0 commit comments

Comments
 (0)