Skip to content

Commit 9c472f2

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

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

rocksteady

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,28 @@ 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+
echo "Pruning stale (now untagged) ECR cache artifacts for repository $name"
213+
214+
local digests
215+
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)
216+
217+
if [ -z "$digests" ]; then
218+
echo "No untagged cache artifacts to prune"
219+
return 0
220+
fi
221+
222+
# Build arguments for batch-delete-image in one call. Each imageDigest becomes an image-ids entry.
223+
local args=()
224+
for digest in $digests; do
225+
args+=( imageDigest=$digest )
226+
done
227+
228+
echo "Deleting $(echo $digests | wc -w | tr -d ' ') untagged cache artifacts"
229+
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[@]}"
208230
}
209231

210232
sub_deploy() {

0 commit comments

Comments
 (0)