AWS CLI: ECR list-images, get newest

前端 未结 5 979
不思量自难忘°
不思量自难忘° 2020-12-31 04:48

Using AWS CLI, and jq if needed, I\'m trying to get the tag of the newest image in a particular repo.

Let\'s call the repo foo, and sa

相关标签:
5条回答
  • 2020-12-31 05:31

    Without having to sort the results, you can filter them specifying the imageTag=latest on image-ids, like so:

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

    This will return only one result with the newest image, which is the one tagged as latest

    0 讨论(0)
  • 2020-12-31 05:33

    List latest 3 images

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

    Skip latest 3 images

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

    Number '3' can be generalized in either head or tail command based on user requirement

    0 讨论(0)
  • 2020-12-31 05:35

    To get only latest image with out special character minor addition required for above answer.

    aws ecr describe-images --repository-name foo --query 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]' --output text
    
    0 讨论(0)
  • 2020-12-31 05:41

    To add to Frederic's answer, if you want the latest, you can use [-1]:

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

    Assuming you are using a singular tag on your images... otherwise you might need to use imageTags[*] and do a little more work to grab the tag you want.

    0 讨论(0)
  • 2020-12-31 05:46

    You can use describe-images instead.

    aws ecr describe-images --repository-name foo 
    

    returns imagePushedAt which is a timestamp property which you can use to filter.

    I dont have examples in my account to test with but something like following should work

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

    If you want another flavor of using sort method, you can review this post

    0 讨论(0)
提交回复
热议问题