【问题标题】:AWS CLI: ECR list-images, get newestAWS CLI:ECR 列表图像,获取最新
【发布时间】:2017-04-10 19:24:37
【问题描述】:

使用 AWS CLI 和 jq(如果需要),我正在尝试 获取特定存储库中最新图像的标签

让我们调用 repo foo,并说最新的图像被标记为 bar。我用什么查询来返回bar

我做到了

aws ecr list-images --repository-name foo

然后意识到list-images documentation 没有将日期作为可查询字段引用。将上述内容粘贴在终端中,我得到的密钥对只有标签和摘要,没有日期。

还有什么方法可以获得“最新”图像吗?我可以假设它总是返回输出中的第一个或最后一个吗?

【问题讨论】:

    标签: amazon-web-services amazon-ec2 aws-cli


    【解决方案1】:

    您可以改用describe-images

    aws ecr describe-images --repository-name foo 
    

    返回imagePushedAt,这是一个时间戳属性,您可以使用它来过滤。

    我的帐户中没有可供测试的示例,但以下内容应该可以使用

    aws ecr describe-images --repository-name foo \
    --query 'sort_by(imageDetails,& imagePushedAt)[*]'
    

    如果你想要另一种使用排序方法的方式,你可以查看this post

    【讨论】:

    • 谢谢!值得注意的是,这只适用于 awscli 1.11+。
    • 这个对我有用:aws ecr describe-images --output json --repository-name $DOCKER_IMAGE_NAME --query 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]' | jq . --raw-output。如果没有jq,很少有存储库会显示 2 张图片。
    • 不知道为什么,但我得到了多个图像版本。我使用| tr '\t' '\n' | head 获取第一个版本
    • 在我的例子中,这返回了多个版本,但那是因为图像被标记了很多次,而我们的构建产生了相同的图像。
    • 这是升序排列,最新的在前。您可以通过添加reverse(sort_by(... 来解决这个问题,但是当分页列表很长时,它仍然只对输出的第一页进行排序。因此,如果您有很多条目,它就不起作用。
    【解决方案2】:

    要补充弗雷德里克的答案,如果你想要最新的,你可以使用[-1]:

    aws ecr describe-images --repository-name foo \
    --query 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]'
    

    假设您在图像上使用单个标签...否则您可能需要使用 imageTags[*] 并做更多工作来获取您想要的标签。

    【讨论】:

    • aws ecr describe-images --repository-name gnk-stage-ar --query 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]' 1550 1553 最新 1558 1547 1511 new-13 1541 1545 1561 1563 1557 1534 1539 1562 1554 它没有打印最新的图像编号。但相同的命令在 ubuntu 中运行,而不是在 centos 中
    • 如果标签很多,请添加--no-paginate
    【解决方案3】:

    仅获取最新图像,而无需上述答案所需的特殊字符次要添加。

    aws ecr describe-images --repository-name foo --query 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]' --output text
    

    【讨论】:

      【解决方案4】:

      列出最近推送到 ECR 的 3 张图片

      aws ecr describe-images --repository-name gvh \
      --query 'sort_by(imageDetails,& imagePushedAt)[*].imageTags[0]' --output yaml \
      | tail -n 3 | awk -F'- ' '{print $2}'
      

      列出推送到 ECR 的前 3 张图片

      aws ecr describe-images --repository-name gvh \
      --query 'sort_by(imageDetails,& imagePushedAt)[*].imageTags[0]' --output yaml \
      | head -n 3 | awk -F'- ' '{print $2}'
      

      数字'3'可以根据用户要求在head或tail命令中泛化

      【讨论】:

        【解决方案5】:

        无需对结果进行排序,您可以在图像 ID 上指定 imageTag=latest 来过滤它们,如下所示:

        aws ecr describe-images --repository-name foo --image-ids imageTag=latest --output text
        

        这将只返回一个带有最新图像的结果,即标记为latest的那个

        【讨论】:

          【解决方案6】:

          要获取最新的图像标签,请使用:-

          aws ecr describe-images --repository-name foo --query 'imageDetails[*].imageTags[ * ]' --output text | sort -r | head -n 1
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-03-18
            • 1970-01-01
            • 2014-01-20
            • 1970-01-01
            • 2019-12-02
            • 1970-01-01
            • 1970-01-01
            • 2022-10-14
            相关资源
            最近更新 更多