ashish bustler
ashish bustler

Reputation: 500

Fetch a particular tagged latest image from AWS ECR repo

Is it possible to fetch latest image from ECR with a particular docker tag which starts from develop like developXXX? I am able to see latest image from a repo with this:

aws ecr describe-images --repository-name reponame --output text --region eu-west-1 --query 'sort_by(imageDetails,& imagePushedAt)[*].imageTags[*]' | tr '\t' '\n' | tail -1

Upvotes: 0

Views: 1280

Answers (3)

Duane Guthrie
Duane Guthrie

Reputation: 1

One on the contributors above had most of this, but after a day to get it to work, I would put the final version here.

aws ecr describe-images --repository-name sensor-base --output json --query 'reverse(sort_by(imageDetails[?imageTags[?starts_with(@,`develop`)]],&imagePushedAt))[0].imageTags[]'

The following returns a single json object with a single json element imageDetails:

aws ecr describe-images --repository-name sensor-base --output json

The next part specifies which fields, elements and order you want things returned.

--query
imageDetails[

Says you want content from that object.

?imageTags

Says select items from list based on that field (which its self is a list).

?starts_with(@,`develop`)]

Says select the items in the imageTags list based on element (@) strings starting with "develop".

sort_by( ... ,&imagePushedAt)

Says even though you are selecting imageTags elements, you want to order the list by the imagePushedAt field.

reverse( ... )[0] 

Says place last pushed first and select only one element. ([-1]) should also work.

Now we do not want the entire imageDetails element we want one tag for that (there can be 0 or more).

.imageTags[]

Provides a flattened list of the tags for that element. imageTags[0] would return a single string.

Note: If you call this from python:

  1. Use ret=subprocess.Popen( not subprocess.run(.
  2. Pass args as a string not list of strings.
  3. Pass shell=True, stdout = subprocess.PIPE to Popen (be careful of security)
  4. Use ret.wait() so it is done.
  5. Use json.loads(push.stdout.read()) to turn into a dictionary. Good luck out there.

Upvotes: 0

andrew lorien
andrew lorien

Reputation: 2688

This query will get the latest image with a tag containing "develop".

--query 'reverse(sort_by(imageDetails, &imagePushedAt)).[[?imageTags[?contains(@,`develop`)]].[imagePushedAt,imageDigest,imageTags][0]]'

First it sorts so the most recently pushed image is first, then selects images with a tag containing "develop", then selects the first result. If it's working for you, you probably want to return only the imageDigest and add --output text --no-paginate so you just get the ID as a string.

Upvotes: 0

PROlific
PROlific

Reputation: 11

Matching 'develop' keyword from all fetched image and returning the latest one with tail -1.

aws ecr describe-images --repository-name reponame --output text --region eu-west-1 --query 'sort_by(imageDetails,& imagePushedAt)[*].imageTags[*]' | grep -w "develop" | tail -1

You can change logic in grep -w "develop" part which can fit to your condition

Upvotes: 1

Related Questions