Skip to content
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-50437] Env vars using PATH+SOMETHING syntax clear the previous env var #301

Merged
merged 2 commits into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
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 @@ -45,14 +45,4 @@ public String toString() {
return String.format("KubernetesComputer name: %s slave: %s", getName(), getNode());
}

/*
Return empty EnvVars since we don't want to inject jnlp agent system variables;
*/
@Override
public EnvVars getEnvironment() throws IOException, InterruptedException {
return new EnvVars();
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import hudson.Launcher;
import hudson.LauncherDecorator;
import hudson.Proc;
import hudson.model.Computer;
import hudson.model.Node;
import io.fabric8.kubernetes.api.model.ContainerStatus;
import io.fabric8.kubernetes.api.model.Pod;
Expand Down Expand Up @@ -212,28 +213,28 @@ public Launcher decorate(final Launcher launcher, final Node node) {
@Override
public Proc launch(ProcStarter starter) throws IOException {
LOGGER.log(Level.FINEST, "Launch proc with environment: {0}", Arrays.toString(starter.envs()));
boolean quiet = starter.quiet();
FilePath pwd = starter.pwd();
List<String> procStarter = Arrays.asList(starter.envs());
List<String> cmdEnvs = new ArrayList<String>();
// One issue that cropped up was that when executing sh commands, we would get the jnlp agent's injected
// environment variables as well, causing obvious problems such as JAVA_HOME being overwritten. The
// unsatisfying answer was to check for the presence of JENKINS_HOME in the cmdenvs and skip if present.
// check if the cmd is sourced from Jenkins, rather than another plugin;
// Currently, build level properties will be provided by the Run Context anyways.
boolean javaHome_detected = false;
for (String env : procStarter) {
if (env.equalsIgnoreCase("JAVA_HOME")) {
LOGGER.log(Level.FINEST, "Detected JAVA_HOME in {0}", env);
javaHome_detected = true;
break;
String[] envVars = starter.envs();
if (node != null) { // It seems this is possible despite the method javadoc saying it is non-null
final Computer computer = node.toComputer();
if (computer != null) {
List<String> resultEnvVar = new ArrayList<>();
try {
EnvVars environment = computer.getEnvironment();
String[] envs = starter.envs();
for (String keyValue : envs) {
String[] split = keyValue.split("=", 2);
if (!split[1].equals(environment.get(split[0]))) {
// Only keep environment variables that differ from Computer's environment
resultEnvVar.add(keyValue);
}
}
envVars = resultEnvVar.toArray(new String[resultEnvVar.size()]);
} catch (InterruptedException e) {
throw new IOException("Unable to retrieve environment variables", e);
}
}
}
if (!javaHome_detected) {
cmdEnvs = procStarter;
}
String[] commands = getCommands(starter);
return doLaunch(quiet, cmdEnvs.toArray(new String[cmdEnvs.size()]), starter.stdout(), pwd, commands);
return doLaunch(starter.quiet(), envVars, starter.stdout(), starter.pwd(), getCommands(starter));
}

private Proc doLaunch(boolean quiet, String [] cmdEnvs, OutputStream outputForCaller, FilePath pwd, String... commands) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ public void runWithEnvVariablesInContext() throws Exception {
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("The initial value of POD_ENV_VAR is pod-env-var-value", b);
r.assertLogContains("The value of POD_ENV_VAR outside container is /bin/mvn:pod-env-var-value", b);
r.assertLogContains("The value of FROM_ENV_DEFINITION is ABC", b);
r.assertLogContains("The value of FROM_WITHENV_DEFINITION is DEF", b);
r.assertLogContains("The value of WITH_QUOTE is \"WITH_QUOTE", b);
Expand All @@ -160,6 +162,7 @@ public void runWithEnvVariablesInContext() throws Exception {
r.assertLogContains("The value of AFTER_ESCAPED_QUOTE is AFTER_ESCAPED_QUOTE\\\"", b);
r.assertLogContains("The value of SINGLE_QUOTE is BEFORE'AFTER", b);
r.assertLogContains("The value of WITH_NEWLINE is before newline\nafter newline", b);
r.assertLogContains("The value of POD_ENV_VAR is /bin/mvn:pod-env-var-value", b);
r.assertLogContains("The value of WILL.NOT is ", b);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//noinspection GrPackage
podTemplate(label: 'mypod',
envVars: [
envVar(key: 'POD_ENV_VAR', value: 'pod-env-var-value'),
],
containers: [
containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'),
]) {
Expand All @@ -8,6 +11,11 @@ podTemplate(label: 'mypod',
env.FROM_ENV_DEFINITION = "ABC"
node ('mypod') {
stage('Run busybox') {
sh 'echo before withEnv'
sh '''
echo "The initial value of POD_ENV_VAR is $POD_ENV_VAR"
'''

withEnv([
'FROM_WITHENV_DEFINITION=DEF',
'WITH_QUOTE="WITH_QUOTE',
Expand All @@ -16,8 +24,13 @@ podTemplate(label: 'mypod',
"SINGLE_QUOTE=BEFORE'AFTER",
'AFTER_ESCAPED_QUOTE=AFTER_ESCAPED_QUOTE\\"',
'WITH_NEWLINE=before newline\nafter newline',
'POD_ENV_VAR+MAVEN=/bin/mvn',
'WILL.NOT=BEUSED'
]) {
sh 'echo outside container'
sh '''
echo "The value of POD_ENV_VAR outside container is $POD_ENV_VAR"
'''
container('busybox') {
sh 'echo inside container'
sh '''
Expand All @@ -30,6 +43,7 @@ podTemplate(label: 'mypod',
echo "The value of SINGLE_QUOTE is $SINGLE_QUOTE"
echo "The value of WITH_NEWLINE is $WITH_NEWLINE"
echo "The value of WILL.NOT is $WILL.NOT"
echo "The value of POD_ENV_VAR is $POD_ENV_VAR"
'''
}
}
Expand Down