删除特定分支的所有旧 ecr 映像

问题描述 投票:0回答:1

我想要一些 git 分支部署。对于特定分支,我想保留最新的(按 imagePushedAt 排序)并删除其余的。这就是我正在尝试的:

image_tags_json=$(aws ecr describe-images --repository-name bi-dagster --query 'sort_by(imageDetails,& imagePushedAt)[*].[imageTags[], imagePushedAt]' --output json)

# Check if there are any image tags returned
if [[ $(echo "$image_tags_json" | jq -r 'length') -eq 0 ]]; then
    echo "No image tags found."
    exit 1
fi

# Extract image tags containing the branch name along with timestamps
branch_image_tags=$(echo "$image_tags_json" | jq -r --arg branch "$branch_name" '.[] | select(.[0] | arrays) | select(.[0][] | contains($branch)) | "\(.[0]) \(.[1])"')

# Find the latest timestamp
latest_timestamp=$(echo "$branch_image_tags" | awk '{print $2}' | sort -r | head -n1)

# Output the image tags except the one with the latest timestamp
tags_to_delete=$(echo "$branch_image_tags" | awk -v latest="$latest_timestamp" '$2 != latest {print $1}')
#echo $tags_to_delete

image_digests=$(echo "$tags_to_delete" | jq -r '. | join(" ")')
echo $image_digests

for digest in $image_digests; do
    aws ecr batch-delete-image --repository-name bi-dagster --image-ids imageDigest="$digest"
done

当我回显

image_digests
时,我会得到这种格式的输出。这些是正确识别的要删除的图像标签,以空格分隔。

1233-1-DATA 238-1-DATA 157-1-DATA 661-1-DATA

但是当我尝试实际删除它们时,问题就来了。我在最后一个命令中收到此错误。

{
  "imageIds": [],
  "failures": [
    {
      "imageId": {
        "imageDigest": "661-1-DATA"
      },
      "failureCode": "InvalidImageDigest",
      "failureReason": "Invalid request parameters: image digest should satisfy the regex '[a-zA-Z0-9-_+.]+:[a-fA-F0-9]+'"
    }
  ]
}
amazon-web-services command-line-interface devops aws-cli amazon-ecr
1个回答
0
投票

您列出的图像摘要输出 (

1233-1-DATA 238-1-DATA 157-1-DATA 661-1-DATA
) 是图像标签,而不是图像摘要。将您的代码更改为如下所示:

image_tags=$(echo "$tags_to_delete" | jq -r '. | join(" ")')
echo $image_tags

for tag in $image_tags; do
    aws ecr batch-delete-image --repository-name bi-dagster --image-ids imageTag="$tag"
done
© www.soinside.com 2019 - 2024. All rights reserved.