Skip to content

Instantly share code, notes, and snippets.

@scoheb
Last active July 26, 2017 20:13
Show Gist options
  • Save scoheb/d93a48600e5c1314378fd5bac934e685 to your computer and use it in GitHub Desktop.
Save scoheb/d93a48600e5c1314378fd5bac934e685 to your computer and use it in GitHub Desktop.
podTemplate(name: 'some-slave', label: 'some-slave', cloud: 'openshift', serviceAccount: 'jenkins',
containers: [
// This adds the custom slave container to the pod. Must be first with name 'jnlp'
containerTemplate(name: 'jnlp',
image: '172.30.1.1:5000/continuous-infra/jenkins-continuous-infra-slave:latest',
ttyEnabled: false,
args: '${computer.jnlpmac} ${computer.name}',
command: '',
workingDir: '/tmp'),
// This adds the rpmbuild container to the pod.
containerTemplate(name: 'rpmbuild',
image: '172.30.1.1:5000/ci-containers/rpmbuild',
ttyEnabled: false,
command: '',
workingDir: '/tmp'),
],
// This mounts a volume that is accessible from containers
volumes: [emptyDirVolume(memory: false, mountPath: '/results')]) {
node('some-slave') {
// This variable will be available in the container
stage = "rpmbuild"
// This variable will contain a return string from
// the container execution
containerReturnValue = ""
stage(stage) {
sh 'ls -l /results'
container('rpmbuild') {
// Here is the variable being passed in...
// and we add it to a file that will be available
// after we are done with container.
sh '/bin/echo stage:' + stage + ' > /results/1.txt'
// The shell step captures some output and assigns it
// to a variable
containerReturnValue = sh(script: 'id -u', returnStdout: true).trim()
}
echo "containerReturnValue: " + containerReturnValue
sh 'ls -l /results'
// We should see stage:rpmbuild here in 1.txt
sh 'cat /results/1.txt'
}
}
}
@scoheb
Copy link
Author

scoheb commented Jul 26, 2017

Resulting log

[Pipeline] podTemplate
[Pipeline] {
[Pipeline] node
Running on some-slave-42583a802ba545e993e591c2b629bb06-9c40cd82e5f3 in /tmp/workspace/inline-podTemplate
[Pipeline] {
[Pipeline] stage
[Pipeline] { (rpmbuild)
[Pipeline] sh
[inline-podTemplate] Running shell script
+ ls -l /results
total 0
[Pipeline] container
[Pipeline] {
[Pipeline] sh
[inline-podTemplate] Running shell script
Executing shell script inside container [rpmbuild] of pod [some-slave-42583a802ba545e993e591c2b629bb06-9c40cd82e5f3]
+ /bin/echo stage:rpmbuild
[Pipeline] sh
[inline-podTemplate] Running shell script
Executing shell script inside container [rpmbuild] of pod [some-slave-42583a802ba545e993e591c2b629bb06-9c40cd82e5f3]
+ id -u
[Pipeline] }
[Pipeline] // container
[Pipeline] echo
containerReturnValue: 1000060000
[Pipeline] sh
[inline-podTemplate] Running shell script
+ ls -l /results
total 4
-rw-r--r-- 1 default 1000060000 15 Jul 26 17:32 1.txt
[Pipeline] sh
[inline-podTemplate] Running shell script
+ cat /results/1.txt
stage:rpmbuild
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
Finished: SUCCESS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment