Skip to content

Commit 0e239c7

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

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+
# 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+
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)