-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[JENKINS-53500] Windows support for container step #626
Changes from all commits
81bd573
af5faad
a6cab42
2a0a670
b27b537
6619b89
14f3792
3e09dea
f3d304e
47a77a2
12b7069
15568d7
24c97c2
230a289
217af86
2c4916e
06479de
54771c4
179b9aa
f32db53
b774e48
9238e2d
72ee637
f34e71c
895b317
2d30ad7
0fd290d
ebcfb88
c81198a
a61ba8c
d8992c5
0e2e0be
293f1c3
45bfb1c
dadd5f2
fd851c8
184667b
5e9c181
06ea50a
3220391
24215e2
aa7002d
8bcf318
d3eaab0
80ce964
b94478d
ac0123e
bcd7391
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Runs a build on a Windows pod. | ||
* Tested in EKS: https://docs.aws.amazon.com/eks/latest/userguide/windows-support.html | ||
*/ | ||
podTemplate(yaml: ''' | ||
apiVersion: v1 | ||
kind: Pod | ||
spec: | ||
containers: | ||
- name: jnlp | ||
image: jenkins/jnlp-agent:latest-windows | ||
- name: shell | ||
image: mcr.microsoft.com/powershell:preview-windowsservercore-1809 | ||
command: | ||
- powershell | ||
args: | ||
- Start-Sleep | ||
- 999999 | ||
nodeSelector: | ||
beta.kubernetes.io/os: windows | ||
''') { | ||
node(POD_LABEL) { | ||
container('shell') { | ||
powershell 'Get-ChildItem Env: | Sort Name' | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,7 +80,6 @@ public class ContainerExecDecorator extends LauncherDecorator implements Seriali | |
private static final String COOKIE_VAR = "JENKINS_SERVER_COOKIE"; | ||
|
||
private static final Logger LOGGER = Logger.getLogger(ContainerExecDecorator.class.getName()); | ||
private static final String DEFAULT_SHELL = "sh"; | ||
|
||
/** | ||
* stdin buffer size for commands sent to Kubernetes exec api. A low value will cause slowness in commands executed. | ||
|
@@ -110,7 +109,6 @@ public ContainerExecDecorator(KubernetesClient client, String podName, String co | |
this.containerName = containerName; | ||
this.environmentExpander = environmentExpander; | ||
this.ws = ws; | ||
this.shell = DEFAULT_SHELL; | ||
} | ||
|
||
@Deprecated | ||
|
@@ -224,10 +222,6 @@ public void setWs(FilePath ws) { | |
this.ws = ws; | ||
} | ||
|
||
public String getShell() { | ||
return shell == null? DEFAULT_SHELL:shell; | ||
} | ||
|
||
public void setShell(String shell) { | ||
this.shell = shell; | ||
} | ||
|
@@ -294,7 +288,8 @@ private Proc doLaunch(boolean quiet, String[] cmdEnvs, OutputStream outputForCal | |
} | ||
ByteArrayOutputStream error = new ByteArrayOutputStream(); | ||
|
||
String msg = "Executing shell script inside container [" + containerName + "] of pod [" + getPodName() + "]"; | ||
String sh = shell != null ? shell : launcher.isUnix() ? "sh" : "cmd"; | ||
String msg = "Executing " + sh + " script inside container " + containerName + " of pod " + getPodName(); | ||
LOGGER.log(Level.FINEST, msg); | ||
printStream.println(msg); | ||
|
||
|
@@ -341,7 +336,7 @@ public void onClose(int i, String s) { | |
|
||
ExecWatch watch; | ||
try { | ||
watch = execable.exec(getShell()); | ||
watch = execable.exec(sh); | ||
} catch (KubernetesClientException e) { | ||
if (e.getCause() instanceof InterruptedException) { | ||
throw new IOException( | ||
|
@@ -376,6 +371,7 @@ public void onClose(int i, String s) { | |
|
||
try { | ||
OutputStream stdin = watch.getInput(); | ||
// stdin = new TeeOutputStream(stdin, new LogTaskListener(LOGGER, Level.FINEST).getLogger()); | ||
if (pwd != null) { | ||
// We need to get into the project workspace. | ||
// The workspace is not known in advance, so we have to execute a cd command. | ||
|
@@ -406,7 +402,7 @@ public void onClose(int i, String s) { | |
|
||
LOGGER.log(Level.FINEST, "Launching with env vars: {0}", envVars.toString()); | ||
|
||
this.setupEnvironmentVariable(envVars, stdin); | ||
this.setupEnvironmentVariable(envVars, stdin, sh.equals("cmd")); | ||
|
||
doExec(stdin, printStream, masks, commands); | ||
|
||
|
@@ -431,21 +427,22 @@ public void kill(Map<String, String> modelEnvVars) throws IOException, Interrupt | |
|
||
int exitCode = doLaunch( | ||
true, null, null, null, null, | ||
// TODO Windows | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have not managed to come up with a PowerShell equivalent.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, that seems to be true from an interactive shell, but then from inside the build Get-Process | Format-Table -DisplayError Id, ProcessName, @{Label="stuff"; Expression={$_.StartInfo.EnvironmentVariables["JENKINS_SERVER_COOKIE"]}} works. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or rather, it prints There is also the fact that So for now I think this is just unfixable. |
||
"sh", "-c", "kill \\`grep -l '" + COOKIE_VAR + "=" + cookie +"' /proc/*/environ | cut -d / -f 3 \\`" | ||
).join(); | ||
|
||
getListener().getLogger().println("kill finished with exit code " + exitCode); | ||
} | ||
|
||
private void setupEnvironmentVariable(EnvVars vars, OutputStream out) throws IOException { | ||
private void setupEnvironmentVariable(EnvVars vars, OutputStream out, boolean windows) throws IOException { | ||
for (Map.Entry<String, String> entry : vars.entrySet()) { | ||
//Check that key is bash compliant. | ||
if (entry.getKey().matches("[a-zA-Z_][a-zA-Z0-9_]*")) { | ||
out.write( | ||
String.format( | ||
"export %s='%s'%s", | ||
windows ? "set %s=%s%s" : "export %s='%s'%s", | ||
entry.getKey(), | ||
entry.getValue().replace("'", "'\\''"), | ||
windows ? entry.getValue() : entry.getValue().replace("'", "'\\''"), | ||
NEWLINE | ||
).getBytes(StandardCharsets.UTF_8) | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<?jelly escape-by-default='true'?> | ||
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form"> | ||
<f:entry field="cloud" title="The cloud to use"> | ||
<f:textbox default="kubernetes"/> | ||
<f:entry field="name" title="${%Container name}"> | ||
<f:textbox/> | ||
</f:entry> | ||
<f:entry field="pod" title="The name of the pod to use"> | ||
<f:textbox/> | ||
</f:entry> | ||
<f:entry field="name" title="The name of the container to select"> | ||
<f:textbox/> | ||
</f:entry> | ||
<f:advanced> | ||
<f:entry field="shell" title="${%Shell}"> | ||
<f:textbox/> | ||
</f:entry> | ||
</f:advanced> | ||
</j:jelly> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<p> | ||
Specifies a shell which will run inside the container | ||
and process requests from Jenkins to run executables, | ||
set environment variables, and similar infrastructure. | ||
</p> | ||
<p> | ||
This does <strong>not</strong> affect the shell used to run user code, | ||
such as <code>sh</code> steps. | ||
To run your scripts with a specific shell on Linux, use an interpreter line: | ||
</p> | ||
<pre><code>sh '''#!/bin/bash | ||
for x in {0..9}; do echo x; done | ||
'''</code></pre> | ||
<p> | ||
or just use a subprocess and an externally versioned script: | ||
</p> | ||
<pre><code>sh 'bash ci.sh'</code></pre> | ||
<p> | ||
On Windows, choose between the <code>bat</code> or <code>powershell</code> steps. | ||
</p> | ||
<p> | ||
For a pod running on Linux, defaults to <code>sh</code>, which should be in <code>$PATH</code>; | ||
for a pod running on Windows, defaults to <code>cmd</code>, which should be in <code>%Path%</code>. | ||
Should not generally be overridden. | ||
</p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leftover?