0
votes

Hi Guys I'm trying to list only files names in a specific bucket in my s3 , and I got suceeded with the following command in aws cli,

aws s3 ls s3://inc-eb-deployments/my-bucket/ | awk '{ print $4 }' 

But I want to add this AWS cli command in to my groovy script in Jenkins "Active Choice Parameter" Plugin, I've added it like below,

def sout = new StringBuilder(), serr = new StringBuilder()
def proc = 'aws s3 ls s3://inc-eb-deployments/my-bucket/ | awk '{ print $4 }' '.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(2000)
def values = "$sout".split('.zip')
def trimmedValues
def parameters=[]
values.each {  println "${it}" }
def cleanValues = "$sout".split('inc')
def last = cleanValues.last().split('.zip')[0]
cleanValues.each {  "${it}".toString(); 
                    trimmedValues = "${it}".trim();
                    parameters<<trimmedValues
                 }
parameters.remove(parameters.size() - 1);
parameters.add(last)
parameters

But When I run the Job I don't see any filename outputs on the build page.

enter image description here

Anyone can help me where's the issue in this Groovy script ?

3

3 Answers

1
votes

This can not work, as String.execute() does "stupid things" (it splits on whitespace) and execute at all does not run a shell at all - it just execs. If you want to use shellism in the groovy world use:

def proc = ["/bin/sh", "-c", "aws s3 ls s3://inc-eb-deployments/my-bucket/ | awk '{ print \$4 }' "].execute()
0
votes
def proc = ["/bin/sh", "-c", "aws s3 ls s3://inc-eb-deployments/my-bucket/ | awk '{ print \$4 }' "].execute()

It works for me

0
votes

I use 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