Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Mar 3, 2012
1 parent 1be2cee commit b4ea67d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
Expand Up @@ -9,7 +9,6 @@
import hudson.slaves.NodeProperty;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import hudson.util.LogTaskListener;
import org.jenkinsci.lib.envinject.EnvInjectException;
import org.jenkinsci.lib.envinject.EnvInjectLogger;
import org.jenkinsci.plugins.envinject.model.EnvInjectJobPropertyContributor;
Expand All @@ -24,7 +23,6 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
Expand Down Expand Up @@ -132,8 +130,8 @@ private Environment setUpEnvironmentNonMatrixRun(AbstractBuild build, Launcher l
//Add Jenkins System variables
if (envInjectJobProperty.isKeepJenkinsSystemVariables()) {
logger.info("Jenkins system variables are kept.");
infraEnvVarsMaster.putAll(getJenkinsSystemVariables(build, true));
infraEnvVarsNode.putAll(getJenkinsSystemVariables(build, false));
infraEnvVarsMaster.putAll(getJenkinsSystemVariables(true));
infraEnvVarsNode.putAll(getJenkinsSystemVariables(false));
}

//Add build variables
Expand Down Expand Up @@ -208,7 +206,7 @@ private boolean isMatrixRun(AbstractBuild build) {
return build instanceof MatrixRun;
}

private Map<String, String> getJenkinsSystemVariables(AbstractBuild build, boolean onMaster) throws IOException, InterruptedException {
private Map<String, String> getJenkinsSystemVariables(boolean onMaster) throws IOException, InterruptedException {

Map<String, String> result = new TreeMap<String, String>();

Expand All @@ -226,29 +224,16 @@ private Map<String, String> getJenkinsSystemVariables(AbstractBuild build, boole
if (n != null)
result.put("NODE_NAME", computer.getName());
result.put("NODE_LABELS", Util.join(n.getAssignedLabels(), " "));

Executor e = build.getExecutor();
if (e != null) {
result.put("EXECUTOR_NUMBER", String.valueOf(e.getNumber()));
}
}

String rootUrl = Hudson.getInstance().getRootUrl();
if (rootUrl != null) {
result.put("JENKINS_URL", rootUrl);
result.put("HUDSON_URL", rootUrl); // Legacy compatibility
result.put("BUILD_URL", rootUrl + build.getUrl());
result.put("JOB_URL", rootUrl + build.getParent().getUrl());
}
result.put("JENKINS_HOME", Hudson.getInstance().getRootDir().getPath());
result.put("HUDSON_HOME", Hudson.getInstance().getRootDir().getPath()); // legacy compatibility

EnvVars envVars = new EnvVars();
for (EnvironmentContributor ec : EnvironmentContributor.all()) {
ec.buildEnvironmentFor(build, envVars, new LogTaskListener(LOG, Level.ALL));
result.putAll(envVars);
}

//Global properties
for (NodeProperty<?> nodeProperty : Hudson.getInstance().getGlobalNodeProperties()) {
if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
Expand Down
@@ -1,5 +1,6 @@
package org.jenkinsci.plugins.envinject.service;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.matrix.MatrixRun;
import hudson.model.*;
Expand Down Expand Up @@ -70,6 +71,38 @@ public Map<String, String> getBuildVariables(AbstractBuild build, EnvInjectLogge
//Add build process variables
result.putAll(build.getCharacteristicEnvVars());

try {
EnvVars envVars = new EnvVars();
for (EnvironmentContributor ec : EnvironmentContributor.all()) {
ec.buildEnvironmentFor(build, envVars, new LogTaskListener(LOG, Level.ALL));
result.putAll(envVars);
}

JDK jdk = build.getProject().getJDK();
if (jdk != null) {
Node node = build.getBuiltOn();
if (node != null) {
jdk = jdk.forNode(node, logger.getListener());
}
jdk.buildEnvVars(result);
}
} catch (IOException ioe) {
throw new EnvInjectException(ioe);
} catch (InterruptedException ie) {
throw new EnvInjectException(ie);
}

Executor e = build.getExecutor();
if (e != null) {
result.put("EXECUTOR_NUMBER", String.valueOf(e.getNumber()));
}

String rootUrl = Hudson.getInstance().getRootUrl();
if (rootUrl != null) {
result.put("BUILD_URL", rootUrl + build.getUrl());
result.put("JOB_URL", rootUrl + build.getParent().getUrl());
}

//Add build variables such as parameters, plugins contributions, ...
result.putAll(build.getBuildVariables());

Expand Down

0 comments on commit b4ea67d

Please sign in to comment.