Home > Mobile >  How to make shell script work at Jenkins including quotes and *
How to make shell script work at Jenkins including quotes and *

Time:04-04

i have a script:

aws s3 rm "s3://my-bucket/" --recursive --dryrun --exclude "*" --include "my-folder/*"

i need to use it somehow in Jenkins pipeline, so i've tried to do it like that:

def bucketNameRoute = 's3://my-bucket/'
def folderNameRoute = 'my-folder/*'
sh "aws s3 rm ${bucketNameRoute} --recursive --dryrun --exclude "*" --include ${folderNameRoute}"

and getting an error:

hudson.remoting.proxyexception groovy.lang.missingmethodexception no signature of method: java.lang.String.multiply() is aplicable for argument types: 
(org.codehause.groovy.runtime.GStringImpl) values: [--include myfolder/*]

How is it possible to solve that issue? Thank you

CodePudding user response:

As a practice, I always wrap any commands with '''

You can have other quotes such as "" inside it, and you can make commands span for several lines. Also note that this is applicable for bash, batch, PowerShell, or any other commands you need to run like this. This quoting style is also applicable for Groovy and Declarative Jenkins, so you should be able to solve the issue regardless of how your pipeline is written.

So in your case, this would be

sh '''aws s3 rm ${bucketNameRoute} --recursive --dryrun --exclude "*" --include ${folderNameRoute}'''

More about triple-quoted strings: http://groovy-lang.org/syntax.html#_triple_single_quoted_string

  • Related