Access of ECR image tags as Active Choice in Jenkins

Vinod Kumar
2 min readNov 11, 2019

There are sometimes requirement to have dynamic fetching of Docker image tags from AWS ECR repository even before triggering the deployment in Jenkins. For example below:

Fortunately there is Active Choice Parameter plugin https://wiki.jenkins.io/display/JENKINS/Active+Choices+Plugin available in Jenkins which allow users to select value(s) for a job parameter.

The technique explained below uses Jenkins Groovy scripting to query external API to populate a parameter box dynamically with Docker image tags. This technique is especially helpful for those who want to:

  • Deploy from Jenkins to a Kubernetes cluster
  • Choose dynamic values to pass into Helm charts as variables when using Jenkins pipelines to deploy into Kubernetes cluster with Helm
  • Pass those values to a raw “kubectl” shell command, for a quick-and-dirty experiment
  • Send the Docker image versions to a custom script for production Kubernetes deployment (for example written in Python using the official kubernetes-python library)

Prerequisites:

  • Install the Active Choice plugin to your Jenkins.
  • Jenkins master must have IAM role assigned for ECR access.
  • Groovy must be installed on Jenkins master.

Working Example

Now start creating a freestyle or pipeline jenkins job and chose ‘This project is parameterized’ and then select Add Parameter -> Active Choice Parameter as shown below:-

Provide a meaningful name for the name parameter of this plugin which you would like to use in your pipeline further if pipeline style job.

Now enter below groovy code or customize according to your requirement, but this was used to fetch only tags which are from branch and beginning with b as shown in top screenshot of this page.

Note: Please update <Repository Name> as per your name of the repository in AWS ECR.

This code will:

  1. List all image tags from ECR repository in JSON format. And because you specified a “–filter” to show only Tagged images, it will not show the ones with no tags at all.
  2. Parse the JSON to allow its manipulations like sorting or extracting fields values.
  3. Fill in a Groovy List of results with image tags using the imageTag field from JSON.

--

--