diff --git a/core/src/main/java/hudson/Launcher.java b/core/src/main/java/hudson/Launcher.java index 382e850bbd43d26f8ed0f4e762a0b3e24e4ee568..bb5748827d63a0e42ec16bda8e3c5eeb1f3042bd 100644 --- a/core/src/main/java/hudson/Launcher.java +++ b/core/src/main/java/hudson/Launcher.java @@ -1087,9 +1087,10 @@ public abstract class Launcher { */ private static EnvVars inherit(Map overrides) { EnvVars m = new EnvVars(EnvVars.masterEnvVars); - // first add all values and then eventually expand them as values can refer other newly added values (see JENKINS-19488) - for (Map.Entry o : overrides.entrySet()) - m.override(o.getKey(),o.getValue()); + // first add all new values and then eventually expand them as values can refer other newly added values (see JENKINS-19488) + for (Map.Entry o : overrides.entrySet()) + if(m.get(o.getKey()) == null) + m.put(o.getKey(), o.getValue()); for (Map.Entry o : overrides.entrySet()) m.override(o.getKey(),m.expand(o.getValue())); return m; diff --git a/test/src/test/java/hudson/LauncherTest.java b/test/src/test/java/hudson/LauncherTest.java index c98f7e20bae56d59fbc451d80a2ca265dc2d1a5b..f20227d8a60c265df5898b69424aebe39e3fb063 100644 --- a/test/src/test/java/hudson/LauncherTest.java +++ b/test/src/test/java/hudson/LauncherTest.java @@ -25,19 +25,22 @@ package hudson; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; - -import java.io.IOException; - import hudson.model.FreeStyleBuild; import hudson.model.FreeStyleProject; import hudson.model.ParametersDefinitionProperty; +import hudson.model.Slave; import hudson.model.StringParameterDefinition; -import hudson.tasks.CommandInterpreter; import hudson.tasks.BatchFile; +import hudson.tasks.CommandInterpreter; import hudson.tasks.Shell; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + import org.junit.Rule; import org.junit.Test; +import org.jvnet.hudson.test.Bug; import org.jvnet.hudson.test.JenkinsRule; public class LauncherTest { @@ -45,6 +48,7 @@ public class LauncherTest { @Rule public JenkinsRule rule = new JenkinsRule(); + @Bug(19488) @Test public void correctlyExpandEnvVars() throws Exception { FreeStyleProject project = rule.createFreeStyleProject(); @@ -63,6 +67,27 @@ public class LauncherTest { assertThat(log(build), containsString("aaa aaaccc ccc")); } + + @Bug(19926) + @Test + public void overwriteSystemEnvVars() throws Exception { + Map env = new HashMap(); + env.put("jenkins_19926", "original value"); + Slave slave = rule.createSlave(new EnvVars(env)); + + FreeStyleProject project = rule.createFreeStyleProject(); + project.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("jenkins_19926", "${jenkins_19926} and new value"))); + final CommandInterpreter script = Functions.isWindows() + ? new BatchFile("echo %jenkins_19926") + : new Shell("echo ${jenkins_19926}") + ; + project.getBuildersList().add(script); + project.setAssignedNode(slave.getComputer().getNode()); + + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + assertThat(log(build), containsString("original value and new value")); + } @SuppressWarnings("deprecation") private String log(FreeStyleBuild build) throws IOException {