Skip to content

Commit 8d822f3

Browse files
authored
Merge pull request #923 from Vlatombe/JENKINS-63847
2 parents a464a6d + 285ee36 commit 8d822f3

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecorator.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ public void onClose(int i, String s) {
464464
if (pwd != null) {
465465
// We need to get into the project workspace.
466466
// The workspace is not known in advance, so we have to execute a cd command.
467-
in.println(String.format("cd \"%s\"", pwd));
467+
in.print(String.format("cd \"%s\"", pwd));
468+
in.print(newLine(!launcher.isUnix()));
468469
}
469470

470471
EnvVars envVars = new EnvVars();
@@ -491,9 +492,9 @@ public void onClose(int i, String s) {
491492

492493
LOGGER.log(Level.FINEST, "Launching with env vars: {0}", envVars.toString());
493494

494-
setupEnvironmentVariable(envVars, in, sh.equals("cmd"));
495+
setupEnvironmentVariable(envVars, in, !launcher.isUnix());
495496

496-
doExec(in, printStream, masks, commands);
497+
doExec(in, !launcher.isUnix(), printStream, masks, commands);
497498

498499
LOGGER.log(Level.INFO, "Created process inside pod: [" + getPodName() + "], container: ["
499500
+ containerName + "]" + "[" + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startMethod) + " ms]");
@@ -527,19 +528,24 @@ private void setupEnvironmentVariable(EnvVars vars, PrintStream out, boolean win
527528
for (Map.Entry<String, String> entry : vars.entrySet()) {
528529
//Check that key is bash compliant.
529530
if (entry.getKey().matches("[a-zA-Z_][a-zA-Z0-9_]*")) {
530-
out.println(
531+
out.print(
531532
String.format(
532533
windows ? "set %s=%s" : "export %s='%s'",
533534
entry.getKey(),
534535
windows ? entry.getValue() : entry.getValue().replace("'", "'\\''")
535536
)
536537
);
538+
out.print(newLine(windows));
537539
}
538540
}
539541
}
540542
};
541543
}
542544

545+
private static String newLine(boolean windows) {
546+
return windows ? "\r\n" : "\n";
547+
}
548+
543549
@Override
544550
public void close() throws IOException {
545551
if (closables == null) return;
@@ -618,7 +624,7 @@ private boolean isSeparator(int b) {
618624
}
619625
}
620626

621-
private static void doExec(PrintStream in, PrintStream out, boolean[] masks, String... statements) {
627+
private static void doExec(PrintStream in, boolean windows, PrintStream out, boolean[] masks, String... statements) {
622628
long start = System.nanoTime();
623629
// For logging
624630
ByteArrayOutputStream loggingOutput = new ByteArrayOutputStream();
@@ -639,10 +645,11 @@ private static void doExec(PrintStream in, PrintStream out, boolean[] masks, Str
639645
.append(statements[i])
640646
.append("\" ");
641647
}
642-
tee.println();
648+
tee.print(newLine(windows));
643649
LOGGER.log(Level.FINEST, loggingOutput.toString(encoding) + "[" + TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - start) + " μs." + "]");
644650
// We need to exit so that we know when the command has finished.
645-
tee.println(EXIT);
651+
tee.print(EXIT);
652+
tee.print(newLine(windows));
646653
tee.flush();
647654
} catch (UnsupportedEncodingException e) {
648655
e.printStackTrace();

0 commit comments

Comments
 (0)