AWS CLI: ECR list-images, get newest

自作多情 提交于 2020-01-12 05:02:09

问题


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 say the latest image is tagged bar. What query do I use to return bar?

I got as far as

aws ecr list-images --repository-name foo

and then realized that the list-images documentation gives no reference to the date as a queryable field. Sticking the above in a terminal gives me keypairs with just the tag and digest, no date.

Is there still some way to get the "latest" image? Can I assume it'll always be the first, or the last in the returned output?


回答1:


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




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/43331418/aws-cli-ecr-list-images-get-newest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!