2
votes

I have a ECR repository setup and it now contains two images with sequential tags 1 and 2.I am planning to push the docker image automatically from this time, My requirement is when next time i push an image,its tag should be 3,so i have to somehow identify that the next number should be 3 since latest version is 2. I do not want to keep this versioning locally. So i need this information from the ECR itself. Any thoughts? the following command will give list of all the images in the repository

aws ecr list-images 
2

2 Answers

6
votes

Thanks @Matt for pointing out about jq. After installing jq the following command will give the latest version

aws ecr list-images --repository-name REPOSITORY_NAME | jq '.imageIds | map (.imageTag)|sort|.[]' | sort -r | head -1
1
votes

The ECR ListImages API endpoint will list all the image shasums and any associated tags that are in a repository. You can filter down to those that are TAGGED as well.

There is a Java SDK for it (but I don't deal in Java so won't be much help).

The Node.js Javascript is pretty simple with a bit of lodash chain processing for the returned object/array/objects.

const _ = require('lodash')
const Promise = require('bluebird')
const AWS = require('aws-sdk')

let config = {
  region: 'us-west-1',
  repo = 'myrepo'
}

const ecr = new AWS.ECR({region: config.region})
Promise.promisifyAll(ecr)

ecr.listImagesAsync({ repositoryName: config.repo, filter: { tagStatus: 'TAGGED'} })
.then( data => {
  let img = _(data.imageIds).sortBy('imageTag').last()
  console.log(img.imageTag)
})

You can install the dependencies for the script in the current directory with

npm init -y && npm install aws-sdk lodash bluebird --save

Try jq if you want to process the aws ecr list-images output in the shell.