Quantcast

Can't capture command results in Kubernetes Pipeline Jenkinsfile

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can't capture command results in Kubernetes Pipeline Jenkinsfile

Mark Nuttall
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 =



 

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/bebe7b4a-3a02-4827-8980-eab053aeb234%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Can't capture command results in Kubernetes Pipeline Jenkinsfile

thomas.lehmann
Easy thing ;)
The variable "c" is not know by the shell script

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




On Thursday, April 20, 2017 at 1:55:34 PM UTC+2, Mark Nuttall wrote:
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 =



 

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/c1de8baf-f18b-432f-9d41-94b75220c9d1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Can't capture command results in Kubernetes Pipeline Jenkinsfile

Mark Nuttall
In reply to this post by Mark Nuttall
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.  

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/b9dcfe8d-e311-4664-b4b0-e18dd194c270%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Can't capture command results in Kubernetes Pipeline Jenkinsfile

Mark Nuttall
In reply to this post by Mark Nuttall
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"

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/c6de5e7b-e504-41de-93c9-e96ae4499687%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...