Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy Script to list S3 files in Jenkins parameter value

I am trying to build a Jenkins Jobs. Where I am using the Extensible plugin of Jenkins. This plugin has an option to specify the Groovy script.

Can anyone help me out to write groovy script to pull the list of files in a bucket?

I want to use one of the file name in Jenkins as a parameter.

like image 957
RITESH SANJAY MAHAJAN Avatar asked Mar 03 '23 15:03

RITESH SANJAY MAHAJAN


2 Answers

You can use this script that I wrote, I'm using it as "active choice parameter" for Jenkins:

def sout = new StringBuilder(), serr = new StringBuilder()
def proc = 'aws s3 ls s3://bucket_name/'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(2000)
def values = "$sout".split('/')
def trimmedValues
def parameters=[]
values.each {  println "${it}" }
def cleanValues = "$sout".split('PRE')
def last = cleanValues.last().split('2018-12-17')[0]
cleanValues.each {  "${it}".toString(); 
                    trimmedValues = "${it}".trim();
                    parameters<<trimmedValues
                 }
parameters.remove(parameters.size() - 1);
parameters.add(last)
parameters

The "2018-12-17" split is because awscli returns the date at the end of the command:

enter image description here

This way I can use this parameter to determine what artifact/folder I'm taking from the bucket. enter image description here

like image 66
Shahar Hamuzim Rajuan Avatar answered Apr 26 '23 17:04

Shahar Hamuzim Rajuan


I'm using Active Choice Parameter in my Jenkins job with the following code:

def s3_path = "s3://bucket/path/to/files/"
def aws_s3_ls_output = "aws s3 ls ${s3_path} --region us-east-1".execute() \
                       | ['awk', '{ print $NF }'].execute()
//aws_s3_ls_output.waitFor() // might be needed if execution is too long
def files = aws_s3_ls_output.text.tokenize().reverse()

return files

enter image description here

like image 26
Oleksandr Shmyrko Avatar answered Apr 26 '23 18:04

Oleksandr Shmyrko