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

Fix overriding HOME env var and remove duplicated env vars #224

Merged
merged 4 commits into from
Oct 12, 2017
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 @@ -310,24 +310,25 @@ private Container createContainer(KubernetesSlave slave, ContainerTemplate conta
// and `?` for java build tools. So we force HOME to a safe location.
env.put("HOME", containerTemplate.getWorkingDir());

List<EnvVar> envVarsList = new ArrayList<>();
Map<String, EnvVar> envVarsMap = new HashMap<>();

env.entrySet().forEach(item ->
envVarsMap.put(item.getKey(), new EnvVar(item.getKey(), item.getValue(), null))
);

if (globalEnvVars != null) {
envVarsList.addAll(globalEnvVars.stream()
.map(TemplateEnvVar::buildEnvVar)
.collect(Collectors.toList()));
globalEnvVars.forEach(item ->
envVarsMap.put(item.getKey(), item.buildEnvVar())
);
}

if (containerTemplate.getEnvVars() != null) {
envVarsList.addAll(containerTemplate.getEnvVars().stream()
.map(TemplateEnvVar::buildEnvVar)
.collect(Collectors.toList()));
containerTemplate.getEnvVars().forEach(item ->
envVarsMap.put(item.getKey(), item.buildEnvVar())
);
}

List<EnvVar> defaultEnvVars = env.entrySet().stream()
.map(entry -> new EnvVar(entry.getKey(), entry.getValue(), null))
.collect(Collectors.toList());
envVarsList.addAll(defaultEnvVars);
EnvVar[] envVars = envVarsList.stream().toArray(EnvVar[]::new);
EnvVar[] envVars = envVarsMap.values().stream().toArray(EnvVar[]::new);

List<String> arguments = Strings.isNullOrEmpty(containerTemplate.getArgs()) ? Collections.emptyList()
: parseDockerCommand(containerTemplate.getArgs() //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ private void assertEnvVars(JenkinsRuleNonLocalhost r2, WorkflowRun b) throws Exc
r.assertLogContains("OUTSIDE_POD_ENV_VAR_FROM_SECRET = " + POD_ENV_VAR_FROM_SECRET_VALUE + "\n", b);
}

@Test
public void runWithOverriddenEnvVariables() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("runWithOverriddenEnvVars.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("OUTSIDE_CONTAINER_HOME_ENV_VAR = /home/jenkins\n", b);
r.assertLogContains("INSIDE_CONTAINER_HOME_ENV_VAR = /root\n",b);
r.assertLogContains("OUTSIDE_CONTAINER_POD_ENV_VAR = " + POD_ENV_VAR_VALUE + "\n", b);
r.assertLogContains("INSIDE_CONTAINER_POD_ENV_VAR = " + CONTAINER_ENV_VAR_VALUE + "\n",b);
}

@Test
public void runJobWithSpaces() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p with spaces");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
podTemplate(label: 'mypod',
envVars: [
envVar(key: 'POD_ENV_VAR', value: 'pod-env-var-value-first'),
envVar(key: 'POD_ENV_VAR', value: 'pod-env-var-value')
],
containers: [
containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat',
envVars: [
envVar(key: 'HOME', value: '/root'),
envVar(key: 'POD_ENV_VAR', value: 'container-env-var-value-first'),
envVar(key: 'POD_ENV_VAR', value: 'container-env-var-value')
],
),
]) {

node ('mypod') {
sh '''
echo OUTSIDE_CONTAINER_HOME_ENV_VAR = $HOME
echo OUTSIDE_CONTAINER_POD_ENV_VAR = $POD_ENV_VAR
'''
stage('Run busybox') {
container('busybox') {
sh '''
echo inside container
echo INSIDE_CONTAINER_HOME_ENV_VAR = $HOME
echo INSIDE_CONTAINER_POD_ENV_VAR = $POD_ENV_VAR
'''
}
}
}
}