awk script inside jenkins pipeline - jenkins

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.

Related

Passing Jenkins environment variable with spaces to shell script

I am unable to properly pass a variable with spaces in Jenkinsfile to shell command
I have tried using quotes (double and single) backspaces and various other combination. Jenkins will treat the string like as 2 item and single quoted them.
The variable in question in the Jenkins file.
MYTIME = 2022-01-02 03:04:05
In Jenkinsfile
stage('list') {
steps{
sh script:'ansible-playbook -i ./hosts.ini ./ping_playbook.yml -e time=\\"$TIME\\" --limit ${ENV}', label: "ping test"
}
}
Jenkins will run it as
ansible-playbook -i ./lzhxjp-test-update/lzhxjp/hosts.ini .ping_playbook.yml -e 'time="2022-01-02' '11:22:33"' --limit sdktest
How do I put it so that Jenkins interpret it as -e time="2022-01-02 11:22:33"
Inside single-quoted strings no variables are expanded by Jenkins (Groovy to be exact).
Single-quoted strings are plain java.lang.String and don’t support interpolation.
https://groovy-lang.org/syntax.html#all-strings
Thus it is passed directly into the shell and therefore already subject to shell quoting, you can remove the \\ before the " and then the shell the will expand "$TIME" correctly.

Remove substring from filename in jenkins groovy script

Hi I am trying to remove substring "-unsigned" from filename in jenkins pipeline script.
where filePattern app/build/outputs/**/-release.apk".
I wrote below groovy script
findFiles(glob: filePattern).each { file ->
sh """
mv ${file.path} "${file.path//-unsigned/}"
"""
}
getting error unexpected char : 0XFFFF.
Can suggest where exactly I am missing. or suggest how to remove substring from file name in groovy.
not sure it's the best way to rename files however:
findFiles(glob: filePattern).each { file ->
sh """
mv ${file.path} "${file.path - '-unsigned'}"
"""
}
issue in your code that you have // in this expression ${file.path // ...}
and compiler could take it as a single line comment
try to run this in groovy console:
"""
${'abc' //no matter what here}
"""
//comment here
^^^ compilation error: unexpected char: 0xFFFF
See bash(1) - Linux man page:
EXPANSION
[...]
Parameter Expansion
[...]
${parameter/pattern/string}
Pattern substitution. [...] Parameter is expanded and the longest match of pattern against its value is replaced with string.
So it should be "${file.path/-unsigned//}".

Jenkins pipeline - groovy.lang.MissingPropertyException

I am trying to write Pipeline script in Jenkins for that I want to store only image name from Docker Repo but IN Unix cmd the code is working but in pipeline script, I am getting an error like: groovy.lang.MissingPropertyException: No such property: x for class: groovy.lang.Binding
def Image_name="$(sudo docker images | grep -e 'hello-world.*latest' | awk -v x=1 '{print $x}')"
echo $Image_name
Double quoted strings are interpolated first in groovy. There is no $x groovy variable defined and so you are getting this error.
You can use single quotes instead of double quotes or escape the dollar sign as \$x in double quoted string.

What is use of sh ''' <command > ''' - three ticks - in a Jenkinsfile?

I have a Jenkinsfile which uses three tick marks surrounding a command to execute as in:
sh ''' command '''
We have no idea why three tick marks are required or what role they perform.
This syntax is seen in the Jenkinsfile doc set.
This has nothing at all to do with bash (in which triple-quotes have no special meaning at all), and everything to do with Groovy (the separate, non-bash interpreter that parses Jenkinsfiles).
In Groovy, but not in bash, strings must use triple-quotes to span multiple lines.
In the context of a sh directive in a Jenkinsfile, the content of your triple-quoted string is passed to a shell as a script to execute; however, the syntax is parsed by Groovy, so it's only Groovy that cares about the quotes themselves (as opposed to the quoted content).
Can you give more idea about what kind of command is it, is it a unix command or some script ?
The single quote and its variation like '''(3 ticks) as mentioned in question skip the variable expansion, and it could used to show what is being executed.
echo '''Updating JAVA_HOME variable :
export $JAVA_HOME="$NEW_JAVA_HOME" '''
However in your question, a command (some string) is enclosed between 3 ticks marks and sh tries to execute this command or script. One such example below
$ echo "echo hello" > /tmp/tesh.sh
$ sh '''/tmp/test.sh'''
hello

Escaping characters in a Jenkins build

I am trying to escape the '#' in the below code for a Jenkins build:
sh "curl --dH "#$Tom" http://google.com"
How do I escape it?
Edit: If I use a \ in front of the # as displayed below:
sh "curl --dH "\#$Tom" http://google.com"
I get another error, stating unexpected character "\".
Try this. no need to escape # character instead of that you need to escape double quotes and $ mark
sh "curl --dH \"#\${Tom}\" http://google.com"
Update: if Tom is a variable,It can be inject into string like this ${Tom}
jenkins pipeline syntax is groovy so you can try them with this online groovy ide
https://www.jdoodle.com/execute-groovy-online

Resources