EnvVars env;
AbstractBuild b = (AbstractBuild) p.getLastBuild();
if (b != null) {
Node lastBuiltOn = b.getBuiltOn();
if (lastBuiltOn != null) {
env = lastBuiltOn.toComputer().getEnvironment().overrideAll(b.getCharacteristicEnvVars());
} else {
env = new EnvVars(System.getenv());
}
String rootUrl = Hudson.getInstance().getRootUrl();
if (rootUrl != null) {
env.put("HUDSON_URL", rootUrl);
env.put("BUILD_URL", rootUrl + b.getUrl());
env.put("JOB_URL", rootUrl + p.getUrl());
}
if (!env.containsKey("HUDSON_HOME")) {
env.put("HUDSON_HOME", Hudson.getInstance().getRootDir().getPath());
}
if (ws != null) {
env.put("WORKSPACE", ws.getRemote());
}
p.getScm().buildEnvVars(b, env);
StreamBuildListener buildListener = new StreamBuildListener((OutputStream) listener.getLogger());
for (NodeProperty nodeProperty : Hudson.getInstance().getGlobalNodeProperties()) {
Environment environment = nodeProperty.setUp(b, launcher, (BuildListener) buildListener);
if (environment != null) {
environment.buildEnvVars(env);
}
}
if (lastBuiltOn != null) {
for (NodeProperty nodeProperty : lastBuiltOn.getNodeProperties()) {
Environment environment = nodeProperty.setUp(b, launcher, buildListener);
if (environment != null) {
environment.buildEnvVars(env);
}
}