diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecorator.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecorator.java index a89cd409e3..22784759d7 100755 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecorator.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecorator.java @@ -264,7 +264,9 @@ public Proc launch(ProcStarter starter) throws IOException { containerWorkingDirStr = containerWorkingDir.get(); } - if (containerWorkingDir.isPresent() && ! containerWorkingDirFilePath.getRemote().startsWith(containerWorkingDirStr)) { + if (containerWorkingDir.isPresent() && + containerWorkingDirFilePath != null && + ! containerWorkingDirFilePath.getRemote().startsWith(containerWorkingDirStr)) { // Container has a custom workingDir, updated the pwd to match container working dir containerWorkingDirFilePathStr = containerWorkingDirFilePath.getRemote().replaceFirst( ContainerTemplate.DEFAULT_WORKING_DIR, containerWorkingDirStr); diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineTest.java index 241903f2f2..1b4dea7944 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineTest.java @@ -500,6 +500,23 @@ public void jnlpWorkingDir() throws Exception { r.assertBuildStatusSuccess(r.waitForCompletion(b)); } + @Issue("JENKINS-60517") + @Test + public void runInDynamicallyCreatedContainer() throws Exception { + List templates = cloud.getTemplates(); + while (templates.isEmpty()) { + LOGGER.log(Level.INFO, "Waiting for template to be created"); + templates = cloud.getTemplates(); + Thread.sleep(1000); + } + assertFalse(templates.isEmpty()); + PodTemplate template = templates.get(0); + assertEquals(Integer.MAX_VALUE, template.getInstanceCap()); + r.assertBuildStatusSuccess(r.waitForCompletion(b)); + r.assertLogContains("whoami", b); + r.assertLogContains("root", b); + } + @Issue("JENKINS-57256") @Test public void basicWindows() throws Exception { diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInDynamicallyCreatedContainer.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInDynamicallyCreatedContainer.groovy new file mode 100644 index 0000000000..451bea3df3 --- /dev/null +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInDynamicallyCreatedContainer.groovy @@ -0,0 +1,52 @@ +timeout ([time: 10, unit: 'MINUTES']) { + def label = "jenkins-slave-${UUID.randomUUID().toString()}" + podTemplate( + label: label, + yaml: ''' +spec: + containers: + - name: jnlp +''', + containers: [ + containerTemplate(name: 'jnlp', + image: 'jenkins/jnlp-slave:latest', + alwaysPullImage: true, + args: '${computer.jnlpmac} ${computer.name}', + ), + containerTemplate( + name: 'docker-dind', + image: 'docker:19-dind', + alwaysPullImage: true, + privileged: true, + envVars: [ + envVar(key: 'DOCKER_TLS_CERTDIR', value: '') + ], + ), + containerTemplate( + name: 'docker', + image: 'docker:19', + alwaysPullImage: true, + ttyEnabled: true, + command: 'cat', + envVars: [ + envVar(key: 'DOCKER_HOST', value: 'tcp://localhost:2375') + ], + ), + ] + ) { + node(label) { + stage('build') { + container('docker') { + sh 'echo "FROM ubuntu:bionic" > Dockerfile' + + def tag = "test:${env.BUILD_ID}".toLowerCase() + devTools = docker.build(tag, "--pull -f Dockerfile .") + + devTools.inside() { + sh 'whoami' + } + } + } + } + } +}