-
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-47376: Fix global env variables #245
Changes from 4 commits
7001f17
635b3b7
eb80b6a
68b2e7d
79b6d42
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 |
---|---|---|
@@ -1,24 +1,30 @@ | ||
package org.csanchez.jenkins.plugins.kubernetes.pipeline; | ||
|
||
import static org.csanchez.jenkins.plugins.kubernetes.pipeline.Resources.*; | ||
|
||
import java.io.Closeable; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import hudson.FilePath; | ||
import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl; | ||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import javax.annotation.Nonnull; | ||
|
||
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback; | ||
import org.jenkinsci.plugins.workflow.steps.BodyInvoker; | ||
import org.jenkinsci.plugins.workflow.steps.EnvironmentExpander; | ||
import org.jenkinsci.plugins.workflow.steps.StepContext; | ||
import org.jenkinsci.plugins.workflow.steps.StepExecution; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import hudson.EnvVars; | ||
import hudson.FilePath; | ||
import hudson.LauncherDecorator; | ||
import hudson.slaves.EnvironmentVariablesNodeProperty; | ||
import hudson.slaves.NodeProperty; | ||
import hudson.slaves.NodePropertyDescriptor; | ||
import hudson.util.DescribableList; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import org.jenkinsci.plugins.workflow.steps.StepExecution; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import static org.csanchez.jenkins.plugins.kubernetes.pipeline.Resources.closeQuietly; | ||
import jenkins.model.Jenkins; | ||
|
||
public class ContainerStepExecution extends StepExecution { | ||
|
||
|
@@ -60,7 +66,23 @@ public boolean start() throws Exception { | |
client = nodeContext.connectToCloud(); | ||
|
||
EnvironmentExpander env = getContext().get(EnvironmentExpander.class); | ||
decorator = new ContainerExecDecorator(client, nodeContext.getPodName(), containerName, nodeContext.getNamespace(), env, getContext().get(FilePath.class)); | ||
EnvVars globalVars = null; | ||
Jenkins instance = Jenkins.getInstance(); | ||
DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties = instance | ||
.getGlobalNodeProperties(); | ||
List<EnvironmentVariablesNodeProperty> envVarsNodePropertyList = globalNodeProperties | ||
.getAll(EnvironmentVariablesNodeProperty.class); | ||
if (envVarsNodePropertyList != null && envVarsNodePropertyList.size() != 0) { | ||
globalVars = envVarsNodePropertyList.get(0).getEnvVars(); | ||
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. I assume you need to iterate through 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. You shouldn't unless there are multiple GlobalNodeProperties in a config. |
||
} | ||
decorator = new ContainerExecDecorator(); | ||
decorator.setClient(client); | ||
decorator.setPodName(nodeContext.getPodName()); | ||
decorator.setContainerName(containerName); | ||
decorator.setNamespace(nodeContext.getNamespace()); | ||
decorator.setEnvironmentExpander(env); | ||
decorator.setWs(getContext().get(FilePath.class)); | ||
decorator.setGlobalVars(globalVars); | ||
getContext().newBodyInvoker() | ||
.withContext(BodyInvoker | ||
.mergeLauncherDecorators(getContext().get(LauncherDecorator.class), decorator)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,11 @@ | |
|
||
import com.google.common.collect.ImmutableMap; | ||
|
||
import hudson.EnvVars; | ||
import hudson.slaves.EnvironmentVariablesNodeProperty; | ||
import hudson.slaves.NodeProperty; | ||
import hudson.slaves.NodePropertyDescriptor; | ||
import hudson.util.DescribableList; | ||
import io.fabric8.kubernetes.api.model.Secret; | ||
import io.fabric8.kubernetes.api.model.SecretBuilder; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
|
@@ -61,6 +66,7 @@ public class AbstractKubernetesPipelineTest { | |
protected static final String SECRET_KEY = "password"; | ||
protected static final String CONTAINER_ENV_VAR_FROM_SECRET_VALUE = "container-pa55w0rd"; | ||
protected static final String POD_ENV_VAR_FROM_SECRET_VALUE = "pod-pa55w0rd"; | ||
protected static final String GLOBAL = "GLOBAL"; | ||
|
||
@ClassRule | ||
public static BuildWatcher buildWatcher = new BuildWatcher(); | ||
|
@@ -96,6 +102,15 @@ public void configureCloud() throws Exception { | |
JenkinsLocationConfiguration.get().setUrl(nonLocalhostUrl.toString()); | ||
|
||
r.jenkins.clouds.add(cloud); | ||
|
||
DescribableList<NodeProperty<?>, NodePropertyDescriptor> list = r.jenkins.getGlobalNodeProperties(); | ||
list.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class); | ||
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. unused? 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. Removed. |
||
EnvironmentVariablesNodeProperty newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty(); | ||
list.add(newEnvVarsNodeProperty); | ||
EnvVars envVars = newEnvVarsNodeProperty.getEnvVars(); | ||
envVars.put("GLOBAL", "GLOBAL"); | ||
envVars.put("JAVA_HOME_X", "java-home-x"); | ||
r.jenkins.save(); | ||
} | ||
|
||
private PodTemplate buildBusyboxTemplate(String label) { | ||
|
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.
I don't understand what does it mean if JAVA_HOME is there?
and due to the
break
any variable after JAVA_HOME is ignored, so the order of vars matterThere 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.
Removed that break...The JAVA_HOME comes from not being able to tell if the execProc was dispatched by another plugin, thus necessitating the entire provided environment variables to be injected or from the master, thus overwriting whatever local environment variables there were.
At least, that the best I can understand from the various ways Jenkins envVars are provided.