Acces groovy var in sh in jenkinsfile - jenkins

I am using enviroment variable ARTIFACT_VERSION, and want to put it in shell script
sh "wget -q http://nexus.com/$polygon/$env.ARTIFACT_VERSION/$polygon-$env.ARTIFACT_VERSION.zip"
And got this errors
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field java.lang.String zip
And if I input var with this style ${env.ARTIFACT_VERSION}
I get in output result like I am using \n (but version input is correct)
wget -q http://nexus.com/polygon/myversion
/polygon-myversion
.zip
The link is correct when i used it without vars - all is ok

myversion parameter was getting as a result from grep command
and it had \n at the end

Related

Parsing config file with sections in Jenkins Pipeline and get specific section

I have to parse a config with section values in Jenkins Pipeline . Below is the example config file
[deployment]
10.7.1.14
[control]
10.7.1.22
10.7.1.41
10.7.1.17
[worker]
10.7.1.45
10.7.1.42
10.7.1.49
10.7.1.43
10.7.1.39
[edge]
10.7.1.13
Expected Output:
control1 = 10.7.1.17 ,control2 = 10.7.1.22 ,control3 = 10.7.1.41
I tried the below code in my Jenkins Pipeline script section . But it seems to be incorrect function to use
def cluster_details = readProperties interpolate: true, file: 'inventory'
echo cluster_details
def Var1= cluster_details['control']
echo "Var1=${Var1}"
Could you please help me with the approach to achieve the expected result
Regarding to documentation readProperties is to read Java properties file. But not INI files.
https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#readproperties-read-properties-from-files-in-the-workspace-or-text
I think to read INI file you have find available library for that,
e.g. https://ourcodeworld.com/articles/read/839/how-to-read-parse-from-and-write-to-ini-files-easily-in-java
Hi i got the solution for the problem
control_nodes = sh (script: """
manish=\$(ansible control -i inventory --list-host |sort -t . -g -k1,1 -k2,2 -k3,3 -k4,4 |awk '{if(NR>1)print}' |awk '{\$1=\$1;print}') ; \
echo \$manish
""",returnStdout: true).trim()
echo "Cluster Control Nodes are : ${control_nodes}"
def (control_ip1,control_ip2,control_ip3) = control_nodes.split(' ')
//println c1 // this also works
echo "Control 1: ${control_ip1}"
echo "Control 2: ${control_ip2}"
echo "Control 3: ${control_ip3}"
Explaination:
In the script section . I am getting the list of hostnames.Using sort i am sorting the hostname based on dot(.) delimeter. then using awk removing the first line in output. Using the later awk i am removing the leading white spaces.
Using returnStdout to save the shell variable output to jenkins property, which has list of ips separated by white space.
Now once i have the values in jenkins property variable, extracting the individual IPs using split methods.
Hope it helps.

Clean composer output from non readable characters in Jenkins' console output page

I have a Jenkins job to tweak, but no administration right on Jenkins itself.
I'd like to clean composer output from non readable characters, e.g:
the command is composer update --no-progress --ansi which outputs
in Jenkins'console.
I didn't exactly get the the reason why Jenkins cannot output some characters correctly.
As per https://medium.com/pacroy/how-to-fix-jenkins-console-log-encoding-issue-on-windows-a1f4b26e0db4, I perhaps could have tried to specify -Dfile.encoding=UTF8 for java, but as I said I don't have rights for Jenkins administration.
How could I get rid of these 'squares' characters ?
By pasting output lines into Notepad++, i noticed that these characters were backspaces. Hereafter how I've managed to embellish the output for Jenkins console :
# run the command, redirect the output into composer.out file
bin/composer.sh update --no-progress --ansi >composer.out 2>&1
# getting rid of backspaces
composer_out=$(cat composer.out | tr -d '\b')
# adding line feeds instead of numerous spaces
composer_out=$(echo "$composer_out" | sed -r 's/\)\s*(\w+)/\)\n\1/g')
echo "$composer_out"

JQ adds single quotes while saving in environment variables

OK, this might be a silly question. I've got the test.json file:
{
"timestamp": 1234567890,
"report": "AgeReport"
}
What I want to do is to extract timestamp and report values and store them in some env variables:
export $(cat test.json | jq -r '#sh "TIMESTAMP=\(.timestamp) REPORT=\(.report)"')
and the result is:
echo $TIMESTAMP $REPORT
1234567890 'AgeReport'
The problem is that those single quotes break other commands.
How can I get rid of those single quotes?
NOTE: I'm gonna leave the accepted answer as is, but see #Inian's answer for a better solution.
Why make it convoluted with using eval and have a quoting mess? Rather simply emit the variables by joining them with NULL (\u0000) and read it back in the shell environment
{
IFS= read -r -d '' TIMESTAMP
IFS= read -r -d '' REPORT
} < <(jq -r '(.timestamp|tostring) + "\u0000" + .report + "\u0000"' test.json)
This makes your parsing more robust by making the fields joined by NULL delimiter, which can't be part of your string sequence.
From the jq man-page, the #sh command converts its input to be
escaped suitable for use in a command-line for a POSIX shell.
So, rather than attempting to splice the output of jq into the shell's export command which would require carefully removing some quoting, you can generate the entire commandline inside jq, and then execute it with eval:
eval "$(
cat test.json |\
jq -r '#sh "export TIMESTAMP=\(.timestamp) REPORT=\(.report)"'
)"

Why this command does not work in Jenkins?

I am a Jenkins beginner.
Why does this command work?
sed -i -E s/'image: '(.*)${stack_name}-${service_name}:.*\$/'image: '\1${stack_name}-${service_name}:${version}/g
And why does the same command not work when it is included in a Jenkinsfile?
sh "sed -i -E s/'image: '(.*)${stack_name}-${service_name}:.*\$/'image: '\1${stack_name}-${service_name}:${version}/g"
The error is:
/opt/jenkins_data/workspace/secuview-front_master-Z2ADTSIGTSEJOG3UYRU4FPDUF5VZMB3SMQLEOUD46TUZG4POWKYQ#tmp/durable-a484faaf/script.sh: line 2: syntax error near unexpected token `('
Under the hood Jenkinsfiles are essentially Apache Groovy scripts, therefore string escaping rules for Groovy apply. When you have slashes they need to be escaped (e.g. \ -> \\) and when you're using double quotes using ${} literals actually get interpreted by the script instead of being passed to the shell step.
Try this instead:
sh 'sed -i -E s/\'image: \'\\(.*\\)${stack_name}-${service_name}:.*\\$/\'image: \'\\1${stack_name}-${service_name}:${version}/g'

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?
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
I am using Jenkins v1.606 and ran into this same issue!
The issue that I saw passing user defined string params containing spaces into an execution shell would not properly format the string (only with a parameter that had 1 or more spaces). What you have to watch out for is reviewing the 'output' log. Jenkins will not properly display the string param value within the log.
Example (correct format for containing spaces):
docker exec -i container-base /bin/bash -c "cd /container/path/to/code/ && ./gradlew test_xml -P DISPLAY_NAME='${DISPLAY_NAME}' -P USERNAME='${USERNAME}' -P SERVER_NAME='${SERVER_NAME}'"
Jenkins Output of string (notice the string values format):
+ docker exec -i container-base /bin/bash -c 'cd /container/path/to/code/ && ./gradlew test_xml -P DISPLAY_NAME='\''VM10 USER D33PZ3R0'\'' -P USERNAME='\''d33pz3r0#stackoverflow.com'\'' -P SERVER_NAME='\''stackoverflow.com'\'''
Conclusion:
In my example, the literal command was encapsulated with <">, followed by surrounding the parameters with <'> to escape the literal cmd string and control the Jenkins string syntax. Remember not to just watch your Jenkins output log as it lead me wrong for an entire day while I fought with this! This should be the same for your issue as well, you do not need to escape with \' or other escape characters. Hope this helps!!

Resources