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.
Related
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.
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.
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
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
I am having these lines in my Jenkinsfile:
parameters {
string(name: 'DATABASE', defaultValue: 'jenkinsdatabase',
description: 'The name of the database')
}
(...)
Now I want to use the value of ${params.DATABASE} in a step of a stage e.g.
sh 'mysql --user ${USER} -p${PASSWORD} --host ${HOST} -e "DROP DATABASE IF EXISTS ${params.DATABASE};CREATE DATABASE ${params.DATABASE} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; commit;";export GRADLE_OPTS="-Xms1536m -Xmx1536m"'
But this ends with a exeception: Bad substitution
Can anybody help me?
Your sh script body uses single quotes around it.
sh '...'
Since these are single quotes, string interpolation will not occur. This means that all of your $ strings will use the shell variable substitution instead of the Groovy substitution. You are getting a Bad substitution in the shell because the shell won't be able to substitute ${params.DATABASE} because that is an invalid shell substitution.
params is a Groovy variable, so switching your single quotes to double quotes will perform string interpolation of the variables.
sh "..."