fledgling
fledgling

Reputation: 1051

Pick the latest ECR image which is not tagged latest

I need to be able to pick the latest tagged image in a ECR repository which does not have the tag latest.

I am trying to achieve this through aws cli and bash/jq

aws ecr describe-images --repository-name <repo-name> --region us-east-1 --output text --query 'sort_by(imageDetails,& imagePushedAt)[*].imageTags[*]' | tr '\t' '\n' | tail -2

The imageTag output here is

ajohn-feature-123
bgates-feature-345

The list may contain latest as one of the entries. I want to skip that entry and pick the first imageTag here which is not latest.

Appreciate any help in achieving this.

Upvotes: 0

Views: 1826

Answers (1)

Raman Sailopal
Raman Sailopal

Reputation: 12877

Assuming that you are looking to look for the next entry in the output after latest, since you already have the output at text you can pipe the output through to sed and so:

aws ecr .... | sed -n '/latest/{n;p}'

Search the output of the aws cli command and search for the string latest. When finding this string, skip to the next line of output and print

With awk, taking account of multiple latest tags one after the other

aws ecr .... | awk '/latest/ { getline;while ($0=="latest") { getline } print $0 }'

Pattern match lines in the output against latest and then run a while loop that moves to the next line if another instance of the string latest is encountered.

aws ecr .... | awk '/latest/&&cnt<1 { getline;while ($0=="latest") { getline } print $0;cnt++ } { arr[NR]=$0 } END { if (cnt=="") { print arr[1] } }'

Another variation of awk using cnt to ensure that only the first instance found is printed (in case there are multiple latest tags with one not after the other) An array arr is also created containing each line. If no latest tags are found, the first tag is printed.

Upvotes: 1

Related Questions