Dollar sign as a string parameter in Jenkins job is doubled - jenkins

I builded Jenkins with parameters in my configuration:
a = a$
b = b$$
c = c$$$
d = d$$$$
e = e$e$e$e$
I builded my pipeline and it contained this possibilities how to display content of environment variables:
sh """
echo "${env.a}"
echo "\$a"
echo "${env.b}"
echo "\$b"
echo "${env.c}"
echo "\$c"
echo "${env.d}"
echo "\$d"
echo "${env.e}"
echo "\$e"
"""
It returned:
+ echo 'a$'
a$
+ echo 'a$$'
a$$
+ echo 'b$'
b$
+ echo 'b$$'
b$$
+ echo c14
c14
+ echo 'c$$$$'
c$$$$
+ echo d14
d14
+ echo 'd$$$$'
d$$$$
+ echo 'eeee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$14'
eeee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$14
+ echo 'ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$'
ee$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$e$$$$
Anyone who can explain this behaviour?

First of all - very interesting problem. Results are... unusual :)
I couldn't reproduce 100% of your results on my Jenkins, it's probably version/platform dependent. But fortunately the most interesting parameter e returned all that mess. So I'll focus on in.
It looks like parameters are evaluated before they are passed to environment. So each occurrence of $e is replaced by current value of e variable (which is e$e$e$e$ so far). So, we have e then three times $e replaced by e$e$e$e$ and finally $. The result is: e e$e$e$e$ e$e$e$e$ e$e$e$e$ $ (spaces added for readability). And this is value stored in environment. You can see it printed by your last echo (but with every $ replaced by $$).
The longest output (echo "${env.e}") doesn't do anything new - it just takes ee$e$e$e$e$e$e$e$e$e$e$e$$ and evaluates it one more time, replacing every occurrence of $e by ee$e$e$e$e$e$e$e$e$e$e$e$$. And finally, replaces every $ by $$ when printing :)

Related

groovy script variable with quotation

I'm writing a jenkins File with groovy script.
I want to add to one of my variable quotation, and than echo this with shell.
my code:
VARIABLE="\"" + "i love you" + "\""
sh "echo ${VARIABLE}"
output:
i love you
desired output:
"i love you"
thanks!

Jenkinsfile and groovy script - Unexpected End of File

I need to have a one-liner sh script such as this
if ! [-s filename.txt ] then echo 'don'\''t do something' exit 2 fi
but when I put this in sh " <>" I am getting the following error
/home/jenkins/workspace/<BLA_BLA>/script.sh: line 1: unexpected EOF while looking for matching `''
Not sure how to fix it. Could someone point me in the right direction?
The first issue is that you need to escape the backslash inside the sh block, so for example:
sh "if ! [-s filename.txt ] then echo 'don'\\''t do something' exit 2 fi"
Then there is probably a problem with the oneliner script you posted as it doesn't work in plain bash for me either.
In the pipeline, you can use multiline string with """, if I reformat the script you posted, it works correctly:
sh """
if ! [ -s filename.txt ]; then
echo 'don'\\''t do something'
exit 2
fi
"""
the output then is:
+ '[' -s filename.txt ']'
+ echo 'don'\''t do something'
don't do something
+ exit 2
ERROR: script returned exit code 2
Would that work for you?

awk script inside jenkins pipeline

I am trying to execute below script inside jenkinsfile
awk 'NR==FNR{new=new $0 ORS; next} /^}]$/{printf "}],\n%s", new} 1' output2 ${WORKSPACE}/${REPO}/file1 >> output2
This script works fine in linux command promt but it gives error when tried to run inside jenkins file.I tried below
sh """
awk 'NR==FNR{new=new $0 ORS; next} /^}]$/{printf "}],\n%s", new} 1' output2 ${WORKSPACE}/${REPO}/file1 >> output2
"""
But it gives syntax error
You need to watch out for escaping single or double quotes, backslashes and dollar signs. Here I've escaped everything:
sh """
awk \'NR==FNR{new=new \$0 ORS; next} /^}]\$/{printf \"}],\\n%s\", new} 1\' output2 \${WORKSPACE}/\${REPO}/file1 >> output2
"""
Maybe you actually don't want to escape ${WORKSPACE} or ${REPO} if those are pipeline environment variables that you want substituted by groovy as opposed to by sh. And maybe you don't need to escape the single quotes inside a triple """ multistring. But you definitely need to escape the double quotes and the backslash in \\n.

GNU Parallel as job queue -- last commands not executed

Trying to follow GNU Parallel as job queue with named pipes with GNU parallel 20201222, I run into issues of parallel not executing the last commands piped into it via tail -n+0 -f.
To demonstrate, I have 3 terminals open:
# terminal 1
true > jobqueue
tail -n+0 -f jobqueue | parallel
# terminal 2
tail -n+0 -f jobqueue | cat
Adding a single small test command to the queue:
# terminal 3
echo "echo test" >> jobqueue
Only terminal 2 prints "echo test", gnu parallel does not output anything.
# terminal 3
for i in `seq 10`; do echo "echo $i" >> jobqueue; done
Only terminal 2 prints "echo 1", ..., "echo 10" (one in each line), gnu parallel does not output anything.
# terminal 3
for i in `seq 100`; do echo "echo $i" >> jobqueue; done
Terminal 2 prints "echo 1", ..., "echo 100". Terminal 1 prints the lines "test", "1", ..., "10", "1", ..., "99", the last line "100" is missing.
Rerunning tail -n+0 -f jobqueue | parallel outputs all up to "99". Rerunning this with --resume --joblog log appended, outputs one more line ("100") but then also lags behind once new lines are added to joblog. For GNU parallel 20161222, the initial run only gets to line "84".
How can I force gnu parallel to flush its input queue on every line?
From man parallel:
There is a a small issue when using GNU parallel as queue system/batch manager: You have to submit JobSlot number of jobs before they will start, and after that you can submit one at a time, and job will start immediately if free slots are available. Output from the running or completed jobs are held back and will only be printed when JobSlots more jobs has been started (unless you use --ungroup or --line-buffer, in which case the output from the jobs are printed immediately). E.g. if you have 10 jobslots then the output from the first completed job will only be printed when job 11 has started, and the output of second completed job will only be printed when job 12 has started.
In other words: The jobs are running. Output is delayed. It is easier to see if you instead of using echo in your example use touch unique-file-name.

Can we define a variable inside a Jenkins parameterized build

My scenario is, I have parameterized build and inside the build section, I have executed shell where I define a variable and then echo to print it. But it doesn't print anything in the console output.
I hope I have made myself clear. Could anyone please answer my question?
current_folder=`date +%Y%m%d-%H%M%S`
echo $current_folder
enter image description here
I'm using Jenkins ver. 2.32.3 and a simple freestyle job, running on mac OS, using an execute shell build step of:
current_folder=`date +%Y%m%d-%H%M%S`
echo $current_folder
Gives output of:
$ /bin/sh -xe /var/folders/kh/4fl0eeldofefmmsfd/T/hudson89388543547899686.sh
++ date +%Y%m%d-%H%M%S
+ current_folder=20180613-081712
+ echo 20180613-081712
20180613-081712
Finished: SUCCESS
In a similar fashion, setting the shell:
#!/bin/bash
current_folder=`date +%Y%m%d-%H%M%S`
echo $current_folder
Gives:
$ /bin/bash /var/folders/kh/by0kd93dfew5fgjhy000h6/T/hudson62702345565786787.sh
20180613-081655
Finished: SUCCESS
The same applies to a parameter that is defined as part of the Jenkins job, underneath the
This project is parameterized checkbox once set. For example, if you have a string parameter called userName with a default value of User1, then you can print it's value in an Execute Shell build step using:
echo $userName
echo ${userName}
echo "In a string ${userName}"
Giving:
User1
User1
In a string User1

Resources