Skip to content

Instantly share code, notes, and snippets.

@jorgenpt
Created August 4, 2012 00:16
Show Gist options
  • Save jorgenpt/3252984 to your computer and use it in GitHub Desktop.
Save jorgenpt/3252984 to your computer and use it in GitHub Desktop.
public class EnvironmentScript extends BuildWrapper {
@SuppressWarnings("rawtypes")
@Override
public Environment setUp(AbstractBuild build,
final Launcher launcher,
final BuildListener listener) throws IOException, InterruptedException {
// ... skip ...
return new Environment() {
@Override
public void buildEnvVars(Map<String, String> env) {
// Here we evaluate the variables we parsed above, in order.
// We expand against all the environment variables we have so far.
EnvVars vars = new EnvVars(env);
listener.getLogger().println("Injecting env, before: " + env.toString());
for (Map.Entry<String, String> variable : injectedVariables) {
env.put(variable.getKey(), variable.getValue()); //vars.expand(variable.getValue()));
listener.getLogger().println("Adding " + variable.toString());
}
listener.getLogger().println("Injecting env, after: " + env.toString());
}
};
}
}
public class EnvironmentScriptTest extends HudsonTestCase {
public void testWithDependentVariables () throws Exception {
Project<?, ?> project = createFreeStyleProject();
CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
project.getBuildersList().add(builder);
project.getBuildWrappersList().add(new EnvironmentScript(SCRIPT_DEPENDENT_VARIABLES));
Build<?, ?> b = assertBuildStatusSuccess(job.project.scheduleBuild2(0).get());
System.out.println(b.getLog());
EnvVars vars = builder.getEnvVars();
assertEquals("one", vars.get("var1"));
assertEquals("one two", vars.get("var2"));
assertEquals("yo $var4", vars.get("var3"));
assertEquals("three one two", vars.get("var4"));
}
}
Injecting env, before: {..., USER=jtjerno, ...}
Adding var1=one
Adding var2=$var1 two
Adding var3=yo $var4
Adding var4=three ${var2}
Injecting env, after: {..., USER=jtjerno, var1=one, var2=$var1 two, var3=yo $var4, var4=three ${var2}, ...}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment