Skip to content

Commit beee95a

Browse files
committed
Add support for overrides.
1 parent 891c3b2 commit beee95a

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/main/java/com/lookout/jenkins/EnvironmentScript.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package com.lookout.jenkins;
2+
import hudson.EnvVars;
23
import hudson.Extension;
34
import hudson.FilePath;
45
import hudson.Launcher;
@@ -78,6 +79,7 @@ public Environment setUp(AbstractBuild build,
7879
// Then we parse the variables out of it. We could use java.util.Properties, but it doesn't order the properties, so expanding variables with previous variables (like a shell script expects) doesn't work.
7980
String[] lines = commandOutput.toString().split("(\n|\r\n)");
8081
final Map<String, String> envAdditions = new HashMap<String, String>(lines.length);
82+
final Map<String, String> envOverrides = new HashMap<String, String>();
8183
for (String line : lines)
8284
{
8385
if (line.trim().isEmpty()) {
@@ -89,14 +91,25 @@ public Environment setUp(AbstractBuild build,
8991
listener.error("[environment-script] Invalid line encountered, ignoring: " + line);
9092
} else {
9193
listener.getLogger().println("[environment-script] Adding variable '" + keyAndValue[0] + "' with value '" + keyAndValue[1] + "'");
92-
envAdditions.put(keyAndValue[0], keyAndValue[1]);
94+
95+
// We sort overrides and additions into two different buckets, because they have to be processed in sequence.
96+
// See hudson.EnvVars.override for how this logic works.
97+
if (keyAndValue[0].indexOf('+') > 0)
98+
envOverrides.put(keyAndValue[0], keyAndValue[1]);
99+
else
100+
envAdditions.put(keyAndValue[0], keyAndValue[1]);
93101
}
94102
}
95103

96104
return new Environment() {
97105
@Override
98106
public void buildEnvVars(Map<String, String> env) {
99-
env.putAll(envAdditions);
107+
// A little roundabout, but allows us to do overrides per
108+
// how EnvVars#override works (PATH+unique=/foo/bar)
109+
EnvVars envVars = new EnvVars(env);
110+
envVars.putAll(envAdditions);
111+
envVars.overrideAll(envOverrides);
112+
env.putAll(envVars);
100113
}
101114
};
102115
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
EnvironmentScriptWrapper.UnableToProduceScript=Unable to produce a script file
22
EnvironmentScriptWrapper.UnableToExecuteScript=Unable to execute script, return code {0}
3-
EnvironmentScriptWrapper.UnableToParseScriptOutput=Unable to parse variables from script: {0}

src/test/java/com/lookout/jenkins/EnvironmentScriptTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ public void testWithDependentVariables () throws Exception {
5757
assertEquals("three one two", vars.get("var4"));
5858
}
5959

60+
final String SCRIPT_OVERRIDDEN_VARIABLES =
61+
"echo var1=one\n"
62+
+ "echo var1+something='not one'\n"
63+
+ "echo var2+something='two'";
64+
public void testWithOverridenVariables () throws Exception {
65+
TestJob job = new TestJob(SCRIPT_OVERRIDDEN_VARIABLES);
66+
Build<?, ?> b = assertBuildStatusSuccess(job.project.scheduleBuild2(0).get());
67+
System.out.println(b.getLog());
68+
69+
EnvVars vars = job.builder.getEnvVars();
70+
assertEquals("not one:one", vars.get("var1"));
71+
assertEquals("two", vars.get("var2"));
72+
}
73+
6074
public void testReadingFileFromSCM () throws Exception {
6175
TestJob job = new TestJob("cat envs");
6276
job.project.setScm(new SingleFileSCM("envs", "foo_var=bar"));

0 commit comments

Comments
 (0)