Skip to content

Commit 5aa1db2

Browse files
authored
Merge pull request jenkinsci#123 from ndeloof/JENKINS-33510
[FIX JENKINS-33510] exec --workdir has been introduced in docker 17.12
2 parents ed59809 + ba96994 commit 5aa1db2

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public static class Execution extends AbstractStepExecutionImpl {
185185
DockerFingerprints.addRunFacet(dockerClient.getContainerRecord(env, container), run);
186186
ImageAction.add(step.image, run);
187187
getContext().newBodyInvoker().
188-
withContext(BodyInvoker.mergeLauncherDecorators(getContext().get(LauncherDecorator.class), new Decorator(container, envHost, ws, toolName))).
188+
withContext(BodyInvoker.mergeLauncherDecorators(getContext().get(LauncherDecorator.class), new Decorator(container, envHost, ws, toolName, dockerVersion))).
189189
withCallback(new Callback(container, toolName)).
190190
start();
191191
return false;
@@ -212,12 +212,16 @@ private static class Decorator extends LauncherDecorator implements Serializable
212212
private final String[] envHost;
213213
private final String ws;
214214
private final @CheckForNull String toolName;
215+
private final boolean hasEnv;
216+
private final boolean hasWorkdir;
215217

216-
Decorator(String container, EnvVars envHost, String ws, String toolName) {
218+
Decorator(String container, EnvVars envHost, String ws, String toolName, VersionNumber dockerVersion) {
217219
this.container = container;
218220
this.envHost = Util.mapToEnv(envHost);
219221
this.ws = ws;
220222
this.toolName = toolName;
223+
this.hasEnv = dockerVersion != null && dockerVersion.compareTo(new VersionNumber("1.13.0")) >= 0;
224+
this.hasWorkdir = dockerVersion != null && dockerVersion.compareTo(new VersionNumber("17.12")) >= 0;
221225
}
222226

223227
@Override public Launcher decorate(final Launcher launcher, final Node node) {
@@ -229,13 +233,18 @@ private static class Decorator extends LauncherDecorator implements Serializable
229233
} catch (InterruptedException x) {
230234
throw new IOException(x);
231235
}
232-
List<String> prefix = new ArrayList<>(Arrays.asList(executable, "exec", container, "env"));
236+
List<String> prefix = new ArrayList<>(Arrays.asList(executable, "exec"));
233237
if (ws != null) {
234238
FilePath cwd = starter.pwd();
235239
if (cwd != null) {
236240
String path = cwd.getRemote();
237241
if (!path.equals(ws)) {
238-
launcher.getListener().getLogger().println("JENKINS-33510: working directory will be " + ws + " not " + path);
242+
if (hasWorkdir) {
243+
prefix.add("--workdir");
244+
prefix.add(path);
245+
} else {
246+
launcher.getListener().getLogger().println("Docker version is older than 17.12, working directory will be " + ws + " not " + path);
247+
}
239248
}
240249
}
241250
} // otherwise we are loading an old serialized Decorator
@@ -250,7 +259,17 @@ private static class Decorator extends LauncherDecorator implements Serializable
250259
}
251260
}
252261
LOGGER.log(Level.FINE, "(exec) reduced environment: {0}", envReduced);
253-
prefix.addAll(envReduced);
262+
if (hasEnv) {
263+
for (String e : envReduced) {
264+
prefix.add("--env");
265+
prefix.add(e);
266+
}
267+
prefix.add(container);
268+
} else {
269+
prefix.add(container);
270+
prefix.add("env");
271+
prefix.addAll(envReduced);
272+
}
254273

255274
// Adapted from decorateByPrefix:
256275
starter.cmds().addAll(0, prefix);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.io.File;
3333
import java.util.Collections;
3434
import java.util.logging.Level;
35+
36+
import hudson.util.VersionNumber;
3537
import org.apache.commons.fileupload.FileItem;
3638
import org.apache.commons.io.FileUtils;
3739
import org.hamcrest.Matchers;
@@ -256,12 +258,11 @@ public class WithContainerStepTest {
256258
});
257259
}
258260

259-
@Ignore("TODO no fix yet")
260261
@Issue("JENKINS-33510")
261262
@Test public void cd() throws Exception {
262263
story.addStep(new Statement() {
263264
@Override public void evaluate() throws Throwable {
264-
DockerTestUtil.assumeDocker();
265+
DockerTestUtil.assumeDocker(new VersionNumber("17.12"));
265266
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "prj");
266267
p.setDefinition(new CpsFlowDefinition(
267268
"node {\n" +

0 commit comments

Comments
 (0)