Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Feb 22, 2012
1 parent 173ae9e commit b2c132e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 37 deletions.
Expand Up @@ -39,11 +39,11 @@ public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, Buil
try {
if (!isMatrixRun(build)) {
if (variableGetter.isEnvInjectJobPropertyActive(build)) {
return setUpEnvironmentNonMatrixProject(build, launcher, listener);
return setUpEnvironmentNonMatrixRun(build, launcher, listener);
}
} else {
if (variableGetter.isEnvInjectJobPropertyActive(build)) {
return setUpEnvironmentMatrixProject(build, listener);
return setUpEnvironmentMatrixRun(build, listener);
}
}
} catch (EnvInjectException envEx) {
Expand All @@ -60,7 +60,7 @@ public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, Buil
};
}

private Environment setUpEnvironmentNonMatrixProject(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException, EnvInjectException {
private Environment setUpEnvironmentNonMatrixRun(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException, EnvInjectException {
EnvInjectVariableGetter variableGetter = new EnvInjectVariableGetter();
EnvInjectLogger logger = new EnvInjectLogger(listener);
logger.info("Preparing an environment for the job.");
Expand Down Expand Up @@ -129,10 +129,10 @@ public void buildEnvVars(Map<String, String> env) {
};
}

private Environment setUpEnvironmentMatrixProject(AbstractBuild build, BuildListener listener) throws IOException, InterruptedException, EnvInjectException {
private Environment setUpEnvironmentMatrixRun(AbstractBuild build, BuildListener listener) throws IOException, InterruptedException, EnvInjectException {
EnvInjectVariableGetter variableGetter = new EnvInjectVariableGetter();
EnvInjectLogger logger = new EnvInjectLogger(listener);
logger.info("Using environment variables injected by the matrix job.");
logger.info("Using environment variables injected by the parent matrix job.");
final Map<String, String> resultVariables = variableGetter.getEnvVarsPreviousSteps(build, logger);
final FilePath rootPath = getNodeRootPath();

Expand Down Expand Up @@ -267,12 +267,21 @@ public void onCompleted(Run run, TaskListener listener) {

EnvInjectPluginAction envInjectAction = run.getAction(EnvInjectPluginAction.class);
if (envInjectAction != null) {

//Add other plugins env vars contribution variables (exclude builder action and parameter actions already populated)
for (EnvironmentContributingAction a : Util.filter(run.getActions(), EnvironmentContributingAction.class)) {
if (!parameter2exclude(a)) {
a.buildEnvVars((AbstractBuild<?, ?>) run, envVars);
}
}

//Add workspace if not set
AbstractBuild build = (AbstractBuild) run;
FilePath ws = build.getWorkspace();
if (ws != null) {
envVars.put("WORKSPACE", ws.getRemote());
}

} else {
//Keep classic injected env vars
AbstractBuild abstractBuild = (AbstractBuild) run;
Expand Down
Expand Up @@ -2,6 +2,7 @@

import hudson.FilePath;
import hudson.Util;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixRun;
import hudson.model.*;
import hudson.slaves.EnvironmentVariablesNodeProperty;
Expand Down Expand Up @@ -75,12 +76,6 @@ public Map<String, String> getBuildVariables(AbstractBuild build, EnvInjectLogge
//Add build variables such as parameters, plugins contributions, ...
result.putAll(build.getBuildVariables());

//Add workspace variable
String workspace = getWorkspaceWithCreation(build, logger, result);
if (workspace != null) {
result.put("WORKSPACE", workspace);
}

//Retrieve triggered cause
Map<String, String> triggerVariable = new BuildCauseRetriever().getTriggeredCause(build);
result.putAll(triggerVariable);
Expand Down Expand Up @@ -113,32 +108,6 @@ public Map<String, String> overrideParametersVariablesWithSecret(AbstractBuild b
return result;
}

private String getWorkspaceWithCreation(AbstractBuild build, EnvInjectLogger logger, Map<String, String> currentEnvVars) throws EnvInjectException {
Node node = build.getBuiltOn();
Job job = build.getParent();
try {
final WorkspaceList.Lease lease = decideWorkspace((TopLevelItem) job, node, currentEnvVars, Computer.currentComputer().getWorkspaceList());
FilePath workspace = lease.path;
workspace.mkdirs();
return workspace.getRemote();
} catch (InterruptedException e) {
throw new EnvInjectException(e);
} catch (IOException e) {
throw new EnvInjectException(e);
}
}

private WorkspaceList.Lease decideWorkspace(TopLevelItem item, Node n, Map<String, String> currentEnVars, WorkspaceList wsl) throws InterruptedException, IOException {
if (item instanceof AbstractProject) {
String customWorkspace = ((AbstractProject) item).getCustomWorkspace();
if (customWorkspace != null) {
String customWorkspaceResolved = Util.replaceMacro(customWorkspace, currentEnVars);
return WorkspaceList.Lease.createDummyLease(n.getRootPath().child(customWorkspaceResolved));
}
}
return wsl.allocate(n.getWorkspaceFor(item));
}

public boolean isEnvInjectJobPropertyActive(AbstractBuild build) {

if (build == null) {
Expand Down Expand Up @@ -172,6 +141,11 @@ public Map<String, String> getEnvVarsPreviousSteps(AbstractBuild build, EnvInjec
EnvInjectDetector envInjectDetector = new EnvInjectDetector();
if (envInjectDetector.isEnvInjectActivated(build)) {
result.putAll(getCurrentInjectedEnvVars(build));
//Add workspace if not set
FilePath ws = build.getWorkspace();
if (ws != null) {
result.put("WORKSPACE", ws.getRemote());
}
} else {
result.putAll(getJenkinsSystemVariablesCurrentNode(build));
result.putAll(getBuildVariables(build, logger));
Expand Down

0 comments on commit b2c132e

Please sign in to comment.