Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -285,68 +285,80 @@
masksPrefixList.add(false);
} else {
String safePath = path.replace("'", "'\"'\"'");
starter.cmds().addAll(0, Arrays.asList("sh", "-c", "cd '" + safePath + "'; exec \"$@\"", "--"));
if (super.isUnix()) {
starter.cmds().addAll(0, Arrays.asList("sh", "-c", "cd '" + safePath + "'; exec \"$@\"", "--"));
} else {
starter.cmds().addAll(0, Arrays.asList("cmd", "/c", "cd \"" + safePath + "\"", "&&"));
}
}
}
}
} // otherwise we are loading an old serialized Decorator
Set<String> envReduced = new TreeSet<String>(Arrays.asList(starter.envs()));
envReduced.removeAll(Arrays.asList(envHost));

// Remove PATH or invalid variable during `exec` as well.
Iterator<String> it = envReduced.iterator();
while (it.hasNext()) {
final String envVar = it.next();
if (envVar.startsWith("PATH=") || "=".equals(envVar.trim())) {
it.remove();
}
}
LOGGER.log(Level.FINE, "(exec) reduced environment: {0}", envReduced);
if (hasEnv) {
for (String e : envReduced) {
prefix.add("--env");
masksPrefixList.add(false);
if (super.isUnix()) {
prefix.add(e);
} else {
prefix.add(WindowsUtil.quoteArgument(e));
}
masksPrefixList.add(true);
}
prefix.add(container);
masksPrefixList.add(false);
} else {
prefix.add(container);
masksPrefixList.add(false);
prefix.add("env");
masksPrefixList.add(false);
if (super.isUnix()) {
prefix.addAll(envReduced);
} else {
prefix.addAll(envReduced.stream()
.map(v -> WindowsUtil.quoteArgument(v))
.collect(Collectors.toList()));
}
masksPrefixList.addAll(envReduced.stream()
.map(v -> true)
.collect(Collectors.toList()));
}

boolean[] originalMasks = starter.masks();
if (originalMasks == null) {
originalMasks = new boolean[starter.cmds().size()];
}

List<String> cmds = new ArrayList<>();
cmds.addAll(prefix);

if (!super.isUnix() && starter.cmds().size() >= 3 && "cmd".equals(starter.cmds().get(0)) && "/c".equalsIgnoreCase(starter.cmds().get(1))) {
// JENKINS-75102 Docker exec on Windows processes character escaping differently.
// Modify launch to work with special characters in a way that docker exec can handle.
cmds.addAll(starter.cmds().subList(0, 2));
cmds.add("call");
cmds.addAll(starter.cmds().subList(2, starter.cmds().size()).stream()
.map(cmd -> cmd.replaceAll("\"\"(.*)\"\"", "\"$1\"")).collect(Collectors.toList()));
if (hasWorkdir) {
cmds.add("call");
cmds.addAll(starter.cmds().subList(2, starter.cmds().size()).stream()
.map(cmd -> cmd.replaceAll("\"\"(.*)\"\"", "\"$1\"")).collect(Collectors.toList()));
} else {
cmds.addAll(starter.cmds().subList(2, 4));
cmds.add("call");
// Skip the duplicate "cmd /c" as we concatenate using && instead
cmds.addAll(starter.cmds().subList(6, starter.cmds().size()).stream()
.map(cmd -> cmd.replaceAll("\"\"(.*)\"\"", "\"$1\"")).collect(Collectors.toList()));

Check warning on line 360 in src/main/java/org/jenkinsci/plugins/docker/workflow/WithContainerStep.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 288-360 are not covered by tests
}
} else {
cmds.addAll(starter.cmds());
}
Expand Down
Loading