Skip to content
Permalink
Browse files

Fixed JENKINS-12027

  • Loading branch information...
gboissinot committed Dec 8, 2011
1 parent 6c1fb85 commit 8fdee0a1a42b310b5a1bbfb00f38414fbb001b21
@@ -47,7 +47,7 @@ public Environment setUp(AbstractBuild build, final Launcher launcher, final Bui
EnvInjectEnvVars envInjectEnvVarsService = new EnvInjectEnvVars(logger);

try {
Map<String, String> previousEnvVars = variableGetter.getPreviousEnvVars(build);
Map<String, String> previousEnvVars = variableGetter.getPreviousEnvVars(build, logger);

Map<String, String> injectedEnvVars = new HashMap<String, String>(previousEnvVars);

@@ -42,11 +42,11 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
EnvInjectLogger logger = new EnvInjectLogger(listener);
EnvInjectEnvVars envInjectEnvVarsService = new EnvInjectEnvVars(logger);

EnvInjectVariableGetter variableGetter = new EnvInjectVariableGetter();
Map<String, String> previousEnvVars = variableGetter.getPreviousEnvVars(build);

try {

EnvInjectVariableGetter variableGetter = new EnvInjectVariableGetter();
Map<String, String> previousEnvVars = variableGetter.getPreviousEnvVars(build, logger);

//Get current envVars
Map<String, String> variables = new HashMap<String, String>(previousEnvVars);

@@ -54,8 +54,9 @@ public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, Buil
//Add build variables (such as parameter variables).
if (envInjectJobProperty.isKeepBuildVariables()) {
TopLevelItem topLevelItem = (TopLevelItem) build.getParent();
infraEnvVarsNode.putAll(variableGetter.getBuildVariables(build, topLevelItem));
infraEnvVarsMaster.putAll(variableGetter.getBuildVariables(build, topLevelItem));
Map<String, String> buildVariables = variableGetter.getBuildVariables(build, topLevelItem, logger);
infraEnvVarsNode.putAll(buildVariables);
infraEnvVarsMaster.putAll(buildVariables);
}

final FilePath rootPath = getNodeRootPath();
@@ -15,6 +15,10 @@ public EnvInjectLogger(TaskListener listener) {
this.listener = listener;
}

public TaskListener getListener() {
return listener;
}

public void info(String message) {
listener.getLogger().println("[EnvInject] - " + message);
}
@@ -6,9 +6,7 @@
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import hudson.util.LogTaskListener;
import org.jenkinsci.plugins.envinject.EnvInjectAction;
import org.jenkinsci.plugins.envinject.EnvInjectJobProperty;
import org.jenkinsci.plugins.envinject.EnvInjectJobPropertyInfo;
import org.jenkinsci.plugins.envinject.*;

import java.io.IOException;
import java.util.HashMap;
@@ -58,7 +56,7 @@
}


public Map<String, String> getBuildVariables(AbstractBuild build, TopLevelItem topLevelItem) {
public Map<String, String> getBuildVariables(AbstractBuild build, TopLevelItem topLevelItem, EnvInjectLogger logger) throws EnvInjectException {
Map<String, String> result = new HashMap<String, String>();

//Add build process variables
@@ -68,26 +66,38 @@
result.putAll(build.getBuildVariables());

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

return result;
}

private String getOrCreateWorkspace(AbstractBuild build, TopLevelItem item) {
FilePath ws = build.getWorkspace();
if (ws != null) {
return ws.getRemote();
} else {
Node node = Computer.currentComputer().getNode();
private String getOrCreateWorkspace(AbstractBuild build, TopLevelItem item, EnvInjectLogger logger) throws EnvInjectException {
try {
Node node = build.getBuiltOn();
if (node != null) {
FilePath wFilePath = node.getWorkspaceFor(item);
return wFilePath.getRemote();
FilePath workspace = decideWorkspace(build, item, node, logger.getListener());
workspace.mkdirs();
return workspace.getRemote();
}
return null;
} catch (InterruptedException ie) {
throw new EnvInjectException(ie);
} catch (IOException ie) {
throw new EnvInjectException(ie);
}
}

private FilePath decideWorkspace(AbstractBuild build, TopLevelItem item, Node n, TaskListener listener) throws InterruptedException, IOException {
if (item instanceof AbstractProject) {
String customWorkspace = ((AbstractProject) item).getCustomWorkspace();
if (customWorkspace != null) {
return n.getRootPath().child(build.getEnvironment(listener).expand(customWorkspace));
}
}
return n.getWorkspaceFor(item);
}

public boolean isEnvInjectJobPropertyActive(Job job) {
@@ -105,14 +115,14 @@ public EnvInjectJobProperty getEnvInjectJobProperty(Job project) {
return (EnvInjectJobProperty) project.getProperty(EnvInjectJobProperty.class);
}

public Map<String, String> getPreviousEnvVars(AbstractBuild build) throws IOException, InterruptedException {
public Map<String, String> getPreviousEnvVars(AbstractBuild build, EnvInjectLogger logger) throws IOException, InterruptedException, EnvInjectException {
Map<String, String> result = new HashMap<String, String>();
EnvInjectJobProperty jobProperty = getEnvInjectJobProperty(build.getParent());
if (jobProperty != null) {
result.putAll(getCurrentInjectedEnvVars(build));
} else {
result.putAll(getJenkinsSystemVariablesCurrentNode(build));
result.putAll(getBuildVariables(build, (TopLevelItem) build.getParent()));
result.putAll(getBuildVariables(build, (TopLevelItem) build.getParent(), logger));
}
return result;
}

0 comments on commit 8fdee0a

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