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/