@@ -185,7 +185,7 @@ public static class Execution extends AbstractStepExecutionImpl {
185
185
DockerFingerprints .addRunFacet (dockerClient .getContainerRecord (env , container ), run );
186
186
ImageAction .add (step .image , run );
187
187
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 ))).
189
189
withCallback (new Callback (container , toolName )).
190
190
start ();
191
191
return false ;
@@ -212,12 +212,16 @@ private static class Decorator extends LauncherDecorator implements Serializable
212
212
private final String [] envHost ;
213
213
private final String ws ;
214
214
private final @ CheckForNull String toolName ;
215
+ private final boolean hasEnv ;
216
+ private final boolean hasWorkdir ;
215
217
216
- Decorator (String container , EnvVars envHost , String ws , String toolName ) {
218
+ Decorator (String container , EnvVars envHost , String ws , String toolName , VersionNumber dockerVersion ) {
217
219
this .container = container ;
218
220
this .envHost = Util .mapToEnv (envHost );
219
221
this .ws = ws ;
220
222
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 ;
221
225
}
222
226
223
227
@ Override public Launcher decorate (final Launcher launcher , final Node node ) {
@@ -229,13 +233,18 @@ private static class Decorator extends LauncherDecorator implements Serializable
229
233
} catch (InterruptedException x ) {
230
234
throw new IOException (x );
231
235
}
232
- List <String > prefix = new ArrayList <>(Arrays .asList (executable , "exec" , container , "env" ));
236
+ List <String > prefix = new ArrayList <>(Arrays .asList (executable , "exec" ));
233
237
if (ws != null ) {
234
238
FilePath cwd = starter .pwd ();
235
239
if (cwd != null ) {
236
240
String path = cwd .getRemote ();
237
241
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
+ }
239
248
}
240
249
}
241
250
} // otherwise we are loading an old serialized Decorator
@@ -250,7 +259,17 @@ private static class Decorator extends LauncherDecorator implements Serializable
250
259
}
251
260
}
252
261
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
+ }
254
273
255
274
// Adapted from decorateByPrefix:
256
275
starter .cmds ().addAll (0 , prefix );
0 commit comments