Help with Jenkinsfile script syntax error please

951 views
Skip to first unread message

ZillaYT

unread,
May 31, 2018, 11:51:32 AM5/31/18
to Jenkins Users
I have the following code to try to get a docker image

stage("Cache ECS Image") {
 image
= sh(returnStdout: true, script: 'docker image ls | grep ${env.DBName} | tr -s \" \" | cut -d \" \" -f3').trim()
 echo
"Image ID: $image"
 echo
"Image ID: ${image}"
 
// some other code follow
}

..and get the following error.
11:32:23 /home/jenkins/workspace/PRM - ChrisF Pipeline@tmp/durable-bcbc328d/script.sh: 2: /home/jenkins/workspace/PRM - ChrisF Pipeline@tmp/durable-bcbc328d/script.sh: Bad substitution 11:32:23 + tr -s
11:32:23 + cut -d -f3
[Pipeline] echo
11:32:23 Image ID:
[Pipeline] echo
11:32:23 Image ID:


What is my syntax error?

John Mellor

unread,
May 31, 2018, 12:26:46 PM5/31/18
to jenkins...@googlegroups.com
Is that going to work? You are trying to expand a groovy variable ${env.DBName} inside single quotes. I think you need to either use triple double-quotes, or switch the double quotes and the single quotes. IMHO, this kind of construct is used often, but is very hard to do correctly in groovy pipelines.

ZillaYT

unread,
May 31, 2018, 1:46:55 PM5/31/18
to Jenkins Users
John, see my header please. ;-)

ZillaYT

unread,
May 31, 2018, 2:58:09 PM5/31/18
to Jenkins Users
Using double quotes and escaping the internal double quotes was the trick. so this works...

stage("Cache ECS Image") {
 image
= sh(returnStdout: true, script: "docker image ls | grep ${env.DBName} | tr -s \" \" | cut -d \" \" -f3").trim()
 echo
"Image ID: $image"

 
// some other code follow
}

I get a valid value for the ImageID.

DmitryB

unread,
May 31, 2018, 5:13:40 PM5/31/18
to Jenkins Users

ZillaYT

unread,
May 31, 2018, 5:18:05 PM5/31/18
to Jenkins Users
Thanks, that works too
Reply all
Reply to author
Forward
0 new messages