Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape Jenkins parameterized build variables

I use Jenkins ver. 1.522 and I want to pass a long string with spaces and quotes as a parameter in the parameterized build section. The job only runs a python script.

My problem is that I can't find a way to escape my string so that jenkins passes it correctly to the script.

Assuming...

string: fixVersion in ("foo") AND issuetype in (Bug, Improvement) AND resolution = Fixed ORDER BY resolution ASC, assignee ASC, key DESC

variable name: bar

script name: coco.py

When I run the script in the terminal, everything is fine: python coco.py --option 'fixVersion in ("foo") AND issuetype in (Bug, Improvement) AND resolution = Fixed ORDER BY resolution ASC, assignee ASC, key DESC'

When I run the same script with jenkins using the parametrized build and try to escape the variable so it end up taken as one parameter by the py script it is oddly espacped by jenkins.

In my jenkins job I call the script: python coco.py --option \'${BAR}\'

and it ends up as:

python coco.py --option '"fixVersion' in '('\''foo'\'')' AND issuetype in '(Bug,' 'Improvement)' in '(Production,' 'Stage)' AND resolution = Fixed ORDER BY resolution ASC, assignee ASC, key 'DESC"'

I also tried \"${BAR}\", \"$BAR\",\'$BAR\'

What it the right way do acheive it?

like image 559
vinni_f Avatar asked Mar 19 '23 13:03

vinni_f


1 Answers

Try

python coco.py --option "${BAR}"

Alternatively, if you need the single quotes surrounding everything

python coco.py --option \'"${BAR}"\'

In the cases you listed, bash will treat the spaces as delimiters. Putting the double quotes around a variable will preserve the whitespace in a string. Example

aString='foo bar'
for x in $aString; do echo $x; done
# foo
# bar
for x in "$aString"; do echo $x; done
# foo bar
like image 52
racosta Avatar answered Apr 24 '23 10:04

racosta