Skip to content
Permalink
Browse files

Fix JENKINS-12704 and JENKINS-12841

  • Loading branch information...
gboissinot committed Feb 22, 2012
1 parent 173ae9e commit b2c132e077b2bb052df175660a8906533d681d37
@@ -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) {
@@ -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.");
@@ -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();

@@ -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;
@@ -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;
@@ -75,12 +76,6 @@
//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);
@@ -113,32 +108,6 @@
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) {
@@ -172,6 +141,11 @@ public EnvInjectJobProperty getEnvInjectJobProperty(Job project) {
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));

0 comments on commit b2c132e

Please sign in to comment.
You can’t perform that action at this time.