Can't capture command results in Kubernetes Pipeline Jenkinsfile

545 views
Skip to first unread message

Mark Nuttall

unread,
Apr 20, 2017, 7:55:34 AM4/20/17
to Jenkins Users
Hello, 
I'm porting an existing Jenkinsfile to use the kubernetes plugin and its syntax. I've hit a problem in which I need to get the result of a shell command, but get only empty strings. This code worked correctly until being ported to the kubernetes plugin syntax. 

Here's my Jenkinsfile, showing a simple command just to help explain the problem:
#!groovy

podTemplate
(
  label
: 'mypod',
  containers
: [containerTemplate(name: 'maven', image: 'maven:3.5.0-jdk-8', ttyEnabled: true, command: 'cat')],
){
  node
('mypod') {
    stage
('build') {
      container
('maven') {
        sh
'ls > out.txt'
        sh
'cat out.txt'

       
def c = sh(script: 'cat out.txt', returnStdout: true).trim();
        sh
'echo c = ${c}'


       
def c2 = readFile('out.txt').trim()
        sh
'echo c2 = ${c2}'
     
}
   
}
 
}
}

The relevant output, below, is very long but it shows that the 'ls' command runs successfully, and that I can cat the result of that file: but I am completely unable to read the file into a variable, or to execute the command and store the result as a variable, which is what I really want to do. I've highlighted the relevant sections in yellow. Please help me, I'm getting desperate :-(

Many thanks,

-- Mark

# # + ls
[Pipeline] sh
[sample.microservicebuilder.test] Running shell script
Executing shell script inside container [maven] of pod [kubernetes-991f3f512a3e4c13aa5c7960410311bb-25c6ad58b93c]
Executing command: sh -c echo $$ > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-e505a1a7/pid'; jsc=durable-85fd500743f4a1b75ec1fa7e5bc276e5; JENKINS_SERVER_COOKIE=$jsc '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-e505a1a7/script.sh' > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-e505a1a7/jenkins-log.txt' 2>&1; echo $? > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-e505a1a7/jenkins-result.txt' 
# cd "/home/jenkins/workspace/sample.microservicebuilder.test"
sh -c echo $$ > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-e505a1a7/pid'; jsc=durable-85fd500743f4a1b75ec1fa7e5bc276e5; JENKINS_SERVER_COOKIE=$jsc '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-e505a1a7/script.sh' > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-e505a1a7/jenkins-log.txt' 2>&1; echo $? > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-e505a1a7/jenkins-result.txt' 
exit
# # + cat out.txt
Dockerfile
Jenkinsfile
README.md
manifests
out.txt
pom.xml
server.xml
src
[Pipeline] sh
[sample.microservicebuilder.test] Running shell script
Executing shell script inside container [maven] of pod [kubernetes-991f3f512a3e4c13aa5c7960410311bb-25c6ad58b93c]
Executing command: sh -c echo $$ > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-9af67ea2/pid'; jsc=durable-85fd500743f4a1b75ec1fa7e5bc276e5; JENKINS_SERVER_COOKIE=$jsc '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-9af67ea2/script.sh' > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-9af67ea2/output.txt' 2> '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-9af67ea2/jenkins-log.txt'; echo $? > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-9af67ea2/jenkins-result.txt' 
# cd "/home/jenkins/workspace/sample.microservicebuilder.test"
sh -c echo $$ > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-9af67ea2/pid'; jsc=durable-85fd500743f4a1b75ec1fa7e5bc276e5; JENKINS_SERVER_COOKIE=$jsc '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-9af67ea2/script.sh' > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-9af67ea2/output.txt' 2> '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-9af67ea2/jenkins-log.txt'; echo $? > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-9af67ea2/jenkins-result.txt' 
exit
# # + cat out.txt
[Pipeline] sh
[sample.microservicebuilder.test] Running shell script
Executing shell script inside container [maven] of pod [kubernetes-991f3f512a3e4c13aa5c7960410311bb-25c6ad58b93c]
Executing command: sh -c echo $$ > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-3fd54a06/pid'; jsc=durable-85fd500743f4a1b75ec1fa7e5bc276e5; JENKINS_SERVER_COOKIE=$jsc '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-3fd54a06/script.sh' > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-3fd54a06/jenkins-log.txt' 2>&1; echo $? > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-3fd54a06/jenkins-result.txt' 
# cd "/home/jenkins/workspace/sample.microservicebuilder.test"
sh -c echo $$ > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-3fd54a06/pid'; jsc=durable-85fd500743f4a1b75ec1fa7e5bc276e5; JENKINS_SERVER_COOKIE=$jsc '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-3fd54a06/script.sh' > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-3fd54a06/jenkins-log.txt' 2>&1; echo $? > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-3fd54a06/jenkins-result.txt' 
exit
# # + echo c =
c =
[Pipeline] readFile
[Pipeline] sh
[sample.microservicebuilder.test] Running shell script
Executing shell script inside container [maven] of pod [kubernetes-991f3f512a3e4c13aa5c7960410311bb-25c6ad58b93c]
Executing command: sh -c echo $$ > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-27d59433/pid'; jsc=durable-85fd500743f4a1b75ec1fa7e5bc276e5; JENKINS_SERVER_COOKIE=$jsc '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-27d59433/script.sh' > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-27d59433/jenkins-log.txt' 2>&1; echo $? > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-27d59433/jenkins-result.txt' 
# cd "/home/jenkins/workspace/sample.microservicebuilder.test"
sh -c echo $$ > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-27d59433/pid'; jsc=durable-85fd500743f4a1b75ec1fa7e5bc276e5; JENKINS_SERVER_COOKIE=$jsc '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-27d59433/script.sh' > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-27d59433/jenkins-log.txt' 2>&1; echo $? > '/home/jenkins/workspace/sample.microservicebuilder.test@tmp/durable-27d59433/jenkins-result.txt' 
exit
# # + echo c2 =
c2 =



 

thomas....@teamaol.com

unread,
Apr 20, 2017, 8:33:20 AM4/20/17
to Jenkins Users
Easy thing ;)
The variable "c" is not know by the shell script

withEnv(["c=$c"]){
   sh
'c=${c}'
}


Mark Nuttall

unread,
Apr 20, 2017, 8:49:20 AM4/20/17
to Jenkins Users
Having asked the question I've now stumbled onto the answer: I'm just not printing the result correctly. The correct syntax to print variables is, 

echo "c = ${c}"

It needs to be double quotes and not single quotes in order for the variable substitution to work.  

Mark Nuttall

unread,
Apr 20, 2017, 11:13:19 AM4/20/17
to Jenkins Users
In case anyone else finds this topic, the key link is https://jenkins.io/doc/book/pipeline/jenkinsfile/

"Only the [double quoted form] will support the dollar-sign ($) based string interpolation"
Reply all
Reply to author
Forward
0 new messages