diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecorator.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecorator.java index e4d404c97c..432b06f6c7 100755 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecorator.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecorator.java @@ -82,34 +82,35 @@ public class ContainerExecDecorator extends LauncherDecorator implements Seriali private final String namespace; private final String containerName; private final EnvironmentExpander environmentExpander; + private final EnvVars globalVars; - public ContainerExecDecorator(KubernetesClient client, String podName, String containerName, String namespace, EnvironmentExpander environmentExpander) { + public ContainerExecDecorator(KubernetesClient client, String podName, String containerName, String namespace, EnvironmentExpander environmentExpander, EnvVars globalVars) { this.client = client; this.podName = podName; this.namespace = namespace; this.containerName = containerName; this.environmentExpander = environmentExpander; + this.globalVars = globalVars; } public ContainerExecDecorator(KubernetesClient client, String podName, String containerName, String namespace) { - this(client, podName, containerName, namespace, null); + this(client, podName, containerName, namespace, null, null); } @Deprecated public ContainerExecDecorator(KubernetesClient client, String podName, String containerName, AtomicBoolean alive, CountDownLatch started, CountDownLatch finished, String namespace) { - this(client, podName, containerName, namespace, null); + this(client, podName, containerName, namespace, null, null); } @Deprecated public ContainerExecDecorator(KubernetesClient client, String podName, String containerName, AtomicBoolean alive, CountDownLatch started, CountDownLatch finished) { - this(client, podName, containerName, null, null); + this(client, podName, containerName, null); } @Deprecated public ContainerExecDecorator(KubernetesClient client, String podName, String containerName, String path, AtomicBoolean alive, CountDownLatch started, CountDownLatch finished) { - this(client, podName, containerName, null, null); + this(client, podName, containerName, null); } - @Override public Launcher decorate(final Launcher launcher, final Node node) { return new Launcher.DecoratedLauncher(launcher) { @@ -223,6 +224,10 @@ public void onClose(int i, String s) { String.format("cd \"%s\"%s", pwd, NEWLINE).getBytes(StandardCharsets.UTF_8)); } + //get global vars here, run the export first as they'll get overwritten. + if (globalVars != null) { + this.setupEnvironmentVariable(globalVars, watch); + } EnvVars envVars = new EnvVars(); if (environmentExpander != null) { diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerStepExecution.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerStepExecution.java index 81e4ff4ad7..81f8c87621 100755 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerStepExecution.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerStepExecution.java @@ -12,9 +12,15 @@ import hudson.EnvVars; 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 java.util.List; import javax.annotation.Nonnull; +import jenkins.model.Jenkins; import static org.csanchez.jenkins.plugins.kubernetes.pipeline.Resources.closeQuietly; @@ -43,7 +49,14 @@ public boolean start() throws Exception { client = nodeContext.connectToCloud(); EnvironmentExpander env = getContext().get(EnvironmentExpander.class); - decorator = new ContainerExecDecorator(client, nodeContext.getPodName(), containerName, nodeContext.getNamespace(), env); + EnvVars globalVars = null; + Jenkins instance = Jenkins.getInstance(); + DescribableList, NodePropertyDescriptor>globalNodeProperties = instance.getGlobalNodeProperties(); + List envVarsNodePropertyList = globalNodeProperties.getAll(EnvironmentVariablesNodeProperty.class); + if ( envVarsNodePropertyList != null && envVarsNodePropertyList.size() != 0 ) { + globalVars = envVarsNodePropertyList.get(0).getEnvVars(); + } + decorator = new ContainerExecDecorator(client, nodeContext.getPodName(), containerName, nodeContext.getNamespace(), env, globalVars); getContext().newBodyInvoker() .withContext(BodyInvoker .mergeLauncherDecorators(getContext().get(LauncherDecorator.class), decorator)) diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/AbstractKubernetesPipelineTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/AbstractKubernetesPipelineTest.java index 25f70d9d6e..731c676ad3 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/AbstractKubernetesPipelineTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/AbstractKubernetesPipelineTest.java @@ -49,6 +49,11 @@ import org.jvnet.hudson.test.LoggerRule; 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; @@ -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,14 @@ public void configureCloud() throws Exception { JenkinsLocationConfiguration.get().setUrl(nonLocalhostUrl.toString()); r.jenkins.clouds.add(cloud); + + DescribableList, NodePropertyDescriptor> list = r.jenkins.getGlobalNodeProperties(); + list.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class); + EnvironmentVariablesNodeProperty newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty(); + list.add(newEnvVarsNodeProperty); + EnvVars envVars = newEnvVarsNodeProperty.getEnvVars(); + envVars.put("GLOBAL", "GLOBAL"); + r.jenkins.save(); } private PodTemplate buildBusyboxTemplate(String label) { diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineTest.java index 5bfbfcfe2b..0b3c8a2ed4 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineTest.java @@ -155,12 +155,14 @@ private void assertEnvVars(JenkinsRuleNonLocalhost r2, WorkflowRun b) throws Exc r.assertLogContains("INSIDE_CONTAINER_ENV_VAR_FROM_SECRET = " + CONTAINER_ENV_VAR_FROM_SECRET_VALUE + "\n", b); r.assertLogContains("INSIDE_POD_ENV_VAR = " + POD_ENV_VAR_VALUE + "\n", b); r.assertLogContains("INSIDE_POD_ENV_VAR_FROM_SECRET = " + POD_ENV_VAR_FROM_SECRET_VALUE + "\n", b); + r.assertLogContains("INSIDE_GLOBAL = " + GLOBAL + "\n", b); + r.assertLogContains("OUTSIDE_CONTAINER_ENV_VAR =\n", b); r.assertLogContains("OUTSIDE_CONTAINER_ENV_VAR_LEGACY =\n", b); r.assertLogContains("OUTSIDE_CONTAINER_ENV_VAR_FROM_SECRET =\n", b); r.assertLogContains("OUTSIDE_POD_ENV_VAR = " + POD_ENV_VAR_VALUE + "\n", b); - r.assertLogContains("OUTSIDE_POD_ENV_VAR_FROM_SECRET = " + POD_ENV_VAR_FROM_SECRET_VALUE + "\n", b); + r.assertLogContains("OUTSIDE_GLOBAL = " + GLOBAL + "\n", b); } @Test diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithExistingTemplate.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithExistingTemplate.groovy index 8d1f758ed7..b47b3cc513 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithExistingTemplate.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithExistingTemplate.groovy @@ -7,6 +7,7 @@ node ('busybox') { echo OUTSIDE_CONTAINER_ENV_VAR_FROM_SECRET = \$CONTAINER_ENV_VAR_FROM_SECRET echo OUTSIDE_POD_ENV_VAR = \$POD_ENV_VAR echo OUTSIDE_POD_ENV_VAR_FROM_SECRET = \$POD_ENV_VAR_FROM_SECRET + echo OUTSIDE_GLOBAL = \$GLOBAL """ stage('Run busybox') { @@ -18,6 +19,7 @@ node ('busybox') { echo INSIDE_CONTAINER_ENV_VAR_FROM_SECRET = \$CONTAINER_ENV_VAR_FROM_SECRET echo INSIDE_POD_ENV_VAR = \$POD_ENV_VAR echo INSIDE_POD_ENV_VAR_FROM_SECRET = \$POD_ENV_VAR_FROM_SECRET + echo INSIDE_GLOBAL = \$GLOBAL """ } } diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithEnvVars.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithEnvVars.groovy index 11b3d47796..d57796e7cf 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithEnvVars.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithEnvVars.groovy @@ -20,6 +20,7 @@ podTemplate(label: 'mypod', echo OUTSIDE_CONTAINER_ENV_VAR_FROM_SECRET = \$CONTAINER_ENV_VAR_FROM_SECRET echo OUTSIDE_POD_ENV_VAR = \$POD_ENV_VAR echo OUTSIDE_POD_ENV_VAR_FROM_SECRET = \$POD_ENV_VAR_FROM_SECRET + echo OUTSIDE_GLOBAL = \$GLOBAL """ stage('Run busybox') { container('busybox') { @@ -30,6 +31,7 @@ podTemplate(label: 'mypod', echo INSIDE_CONTAINER_ENV_VAR_FROM_SECRET = \$CONTAINER_ENV_VAR_FROM_SECRET echo INSIDE_POD_ENV_VAR = \$POD_ENV_VAR echo INSIDE_POD_ENV_VAR_FROM_SECRET = \$POD_ENV_VAR_FROM_SECRET + echo INSIDE_GLOBAL = \$GLOBAL """ } }