Fetching Amazon s3 file versions dynamically in Jenkins

Manivannan Selvaraj
2 min readSep 19, 2020

I worked on a use case where I wanted the user to see the list of versions for a file stored in a versioned Amazon S3 bucket when the user clicks on build in a Jenkins job.

I did some googling I found that https://plugins.jenkins.io/uno-choice/ might be the one I’m looking for. But I couldn’t find any examples that is an exact match to my use case. So I thought after I got this working, I’d write up what I had to do so that it might help someone later when they’re looking for a similar use case.

  1. First install the active choices parameter plugin in your Jenkins instance. There are multiple ways to install a plugin. Easiest way is to go to Manage Jenkins -> Manage Plugins -> Available and search for “Active Choices Plug-in” and do an install.
  2. Create a Jenkins job / Go to your existing job and create a parameter of type Active Choices Parameter . Give a name to your parameter. I’ve used version_timestamp as the name.
  3. Select Groovy Script under the parameter and copy paste the groovy script below.

Note: You’d need to modify the script accordingly to your bucket and the filename you want to read the versions from. Also, this script uses aws cli to talk to s3 and read the versions of the file before listing in the parameter.

import groovy.json.JsonSlurperClassictry {
// List all the available versions for this object in S3
def bucket = "my-test-bucket"; // my bucket name
def file = "abc.txt"; // object / file that is in bucket
def max_items = 10; // Number of versions to retrieve
def str_cmd = "/usr/local/bin/aws s3api list-object-versions --bucket %s --prefix %s --max-items %d";

def cmd = sprintf(str_cmd, bucket, file, max_items);
def list = cmd.execute().text
def data = new JsonSlurperClassic().parseText(list)
def versionTimeStampList = []

data.Versions.each { version ->
versionTimeStampList.add(sprintf("%s (%s)", version.VersionId, version.LastModified));
}
return versionTimeStampList} catch (Exception e) {
// exceptions like timeout, connection errors, etc.
println(e)
}

4. Now in Build section, create an Execute Shell and paste the code below which’ll have the version that the user selected.

version=$(echo $version_timestamp | cut -d' ' -f 1)echo $version

5. Save the job

6. When you click on Build With Parameters , you should see the versions of your s3 file listed for you to select. You can select the version you want and click on Build

7. Once you run, you should see the version that was selected echoed in the console output.

8. That’s it! Now, it’s up-to you to do whatever you want with the version of the file :)

Ref :

https://medium.com/the-devops-ship/jenkins-use-cases-series-use-active-choice-parameter-plugin-for-parameterized-build-with-values-3d87bccb3330

https://kublr.com/blog/advanced-jenkins-groovy-scripting-for-live-fetching-of-docker-images/

--

--