Sorting AWS ECR images by date with Jenkins groovy script
1 min readSep 25, 2020
I updated the groovy script at https://kublr.com/blog/advanced-jenkins-groovy-scripting-for-live-fetching-of-docker-images/ to use describe-images to sort AWS ECR images by date.
Don’t forget to update MYREPO
with your repo name and aws region! Assumption:
- Your Jenkins instance has right IAM role to access AWS ECR.
- Installed Active Choice Parameter or Extended Choice Parameter Plug-In
- Installed awscli on Jenkins instance
import groovy.json.JsonSlurper
def cmd = ["aws", "ecr", "describe-images", "--region", "eu-west-1", "--repository-name", "MYREPO", "--filter", "tagStatus=TAGGED", "--query", "reverse(sort_by(imageDetails,& imagePushedAt))[*]"]
// Parse JSON into Groovy object
def data = new JsonSlurper().parseText(ecr_images_json.text)
// Prepare the results list
def ecr_images = [];
data.imageTags.each {
for (String item : it) {
ecr_images.push(item)
}
}
// Return the list for Jenkins "select-box"
return ecr_images
You can get the code from