Skip to content

Commit 9311627

Browse files
committed
JENKINS-74912 Fix Docker Windows non-C drive mounts
1 parent 98addf9 commit 9311627

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/main/java/org/jenkinsci/plugins/docker/workflow/WithContainerStep.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,24 @@ public Execution() {
192192
}
193193
} else {
194194
listener.getLogger().println(node.getDisplayName() + " does not seem to be running inside a container");
195-
volumes.put(ws, ws);
196-
volumes.put(tmp, tmp);
195+
196+
if (launcher.isUnix()) {
197+
volumes.put(ws, ws);
198+
volumes.put(tmp, tmp);
199+
} else {
200+
char wsDrive = Character.toUpperCase(ws.charAt(0));
201+
202+
if (wsDrive != 'C') {
203+
listener.getLogger().println("Detected workspace on drive '" + wsDrive + "'. Mounting entire drive.");
204+
205+
// JENKINS-74912 Docker for windows does not support mounting non-root non-C volumes
206+
// Target mount must not end with '/'. Note tmp will be on the same drive.
207+
volumes.put(wsDrive + ":/", wsDrive + ":");
208+
} else {
209+
volumes.put(ws, ws);
210+
volumes.put(tmp, tmp);
211+
}
212+
}
197213
}
198214

199215
String command = launcher.isUnix() ? "cat" : "cmd.exe";

src/test/java/org/jenkinsci/plugins/docker/workflow/WithContainerStepTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import hudson.util.Secret;
3939
import hudson.util.StreamTaskListener;
4040
import java.io.File;
41+
import java.nio.file.Files;
42+
import java.nio.file.Path;
4143
import java.util.Collection;
4244
import java.util.Collections;
4345
import java.util.logging.Level;
@@ -69,6 +71,7 @@
6971
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
7072
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
7173
import org.junit.Assume;
74+
import static org.junit.Assert.assertTrue;
7275
import static org.junit.Assume.assumeTrue;
7376
import org.junit.ClassRule;
7477
import org.junit.Ignore;
@@ -515,4 +518,37 @@ private static final class Execution extends SynchronousNonBlockingStepExecution
515518
}
516519
});
517520
}
521+
522+
@Issue("JENKINS-74912")
523+
@Test public void windowsRunningWindowsContainerAlternateDriveWorkspace() {
524+
// Run with another drive ("D") if it is mounted
525+
story.addStep(new Statement() {
526+
@Override public void evaluate() throws Throwable {
527+
DockerTestUtil.assumeWindows();
528+
DockerTestUtil.assumeDocker(DockerTestUtil.DockerOsMode.WINDOWS);
529+
DockerTestUtil.assumeDrive('D');
530+
531+
// Manually create instead of using a Rule since not all executions will have the D drive mounted
532+
Path tempDir = Files.createTempDirectory(Path.of("D:/"), "j ws");
533+
tempDir.toFile().deleteOnExit();
534+
535+
// Kernel must match when running Windows containers on docker on Windows
536+
String releaseTag = DockerTestUtil.getWindowsImageTag();
537+
538+
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "prj");
539+
p.setDefinition(new CpsFlowDefinition(
540+
"node {\n" +
541+
" ws('" + tempDir.toString().replace("\\", "\\\\") + "') {\n" +
542+
" withDockerContainer('mcr.microsoft.com/windows/nanoserver:" + releaseTag + "') { \n" +
543+
" bat 'echo bar > foo.txt' \n" +
544+
" bat 'echo ran OK' \n" +
545+
" }\n" +
546+
" }\n" +
547+
"}", true));
548+
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
549+
story.j.assertLogContains("ran OK", b);
550+
assertTrue("Mounted workspace contains foo.txt", tempDir.resolve("foo.txt").toFile().exists());
551+
}
552+
});
553+
}
518554
}

0 commit comments

Comments
 (0)