Skip to content

Commit

Permalink
Fixed [JENKINS-11595]
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Nov 7, 2011
1 parent d95e2ee commit 91a47fb
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 142 deletions.
Expand Up @@ -37,26 +37,18 @@ public EnvInjectJobPropertyInfo getInfo() {
@Override
public Environment setUp(AbstractBuild build, final Launcher launcher, final BuildListener listener) throws IOException, InterruptedException {

EnvInjectVariableGetter variableGetter = new EnvInjectVariableGetter();
FilePath ws = build.getWorkspace();
EnvInjectActionSetter envInjectActionSetter = new EnvInjectActionSetter(ws);
EnvInjectLogger logger = new EnvInjectLogger(listener);
EnvInjectEnvVars envInjectEnvVarsService = new EnvInjectEnvVars(logger);

try {

Map<String, String> nodeEnvVars = getNodeEnvVars(envInjectEnvVarsService);
Map<String, String> currentEnvInjectEnvVars = getCurrentEnvInjectEnvVars(envInjectActionSetter, build);
Map<String, String> buildEnvVars = getBuildVariables(build);
Map<String, String> envVarsForFilePath = getEnvVarsForFilePath(envInjectEnvVarsService, nodeEnvVars, currentEnvInjectEnvVars, buildEnvVars);
Map<String, String> previousEnvVars = variableGetter.getPreviousEnvVars(build);
Map<String, String> envVarsForFilePath = getEnvVarsForFilePath(variableGetter, build);
Map<String, String> propertiesEnvVars = retrievePropertiesVars(ws, logger, envVarsForFilePath);

Map<String, String> previousEnvVars = new HashMap<String, String>();
previousEnvVars.putAll(nodeEnvVars);
previousEnvVars.putAll(currentEnvInjectEnvVars);

Map<String, String> injectedEnvVars = new HashMap<String, String>();
injectedEnvVars.putAll(previousEnvVars);
injectedEnvVars.putAll(buildEnvVars);
injectedEnvVars.putAll(propertiesEnvVars);

//Execute script info
Expand Down Expand Up @@ -92,19 +84,17 @@ public void buildEnvVars(Map<String, String> env) {
}
}

private Map<String, String> getNodeEnvVars(EnvInjectEnvVars envInjectEnvVarsService) {
return envInjectEnvVarsService.getCurrentNodeEnvVars();
}

private Map<String, String> getCurrentEnvInjectEnvVars(EnvInjectActionSetter envInjectActionSetter, AbstractBuild build) {
return envInjectActionSetter.getCurrentEnvVars(build);
}
private Map<String, String> getEnvVarsForFilePath(EnvInjectVariableGetter variableGetter, AbstractBuild build) throws IOException, InterruptedException {

private Map<String, String> getEnvVarsForFilePath(EnvInjectEnvVars envInjectEnvVarsService, Map<String, String> nodeVars, Map<String, String> currentEnvInjectEnvVars, Map<String, String> buildVars) {
Map<String, String> buildVarsForFilePath = new HashMap<String, String>();
buildVarsForFilePath.putAll(nodeVars);

Map<String, String> nodeEnvVars = variableGetter.getCurrentNodeEnvVars();
Map<String, String> currentEnvInjectEnvVars = variableGetter.getCurrentInjectedEnvVars(build);
Map<String, String> buildEnvVars = getBuildVariables(build);
buildVarsForFilePath.putAll(nodeEnvVars);
buildVarsForFilePath.putAll(currentEnvInjectEnvVars);
buildVarsForFilePath.putAll(buildVars);
buildVarsForFilePath.putAll(buildEnvVars);

return buildVarsForFilePath;
}

Expand All @@ -115,9 +105,7 @@ private Map<String, String> retrievePropertiesVars(FilePath ws, EnvInjectLogger

private Map<String, String> getBuildVariables(AbstractBuild build) {
Map<String, String> result = new HashMap<String, String>();
//Add build variables such as parameters
result.putAll(build.getBuildVariables());
//Add workspace variable
FilePath ws = build.getWorkspace();
if (ws != null) {
result.put("WORKSPACE", ws.getRemote());
Expand Down
Expand Up @@ -9,6 +9,7 @@
import hudson.tasks.Builder;
import org.jenkinsci.plugins.envinject.service.EnvInjectActionSetter;
import org.jenkinsci.plugins.envinject.service.EnvInjectEnvVars;
import org.jenkinsci.plugins.envinject.service.EnvInjectVariableGetter;
import org.jenkinsci.plugins.envinject.service.PropertiesVariablesRetriever;
import org.kohsuke.stapler.DataBoundConstructor;

Expand Down Expand Up @@ -46,9 +47,8 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
EnvInjectLogger logger = new EnvInjectLogger(listener);
EnvInjectEnvVars envInjectEnvVarsService = new EnvInjectEnvVars(logger);


Map<String, String> previousEnvVars = envInjectEnvVarsService.getCurrentNodeEnvVars();
previousEnvVars.putAll(envInjectActionSetter.getCurrentEnvVars(build));
EnvInjectVariableGetter variableGetter = new EnvInjectVariableGetter();
Map<String, String> previousEnvVars = variableGetter.getPreviousEnvVars(build);

try {

Expand Down Expand Up @@ -102,19 +102,14 @@ public String getUrlName() {

private Map<String, String> getAndAddBuildVariables(AbstractBuild build) {
Map<String, String> result = new HashMap<String, String>();
//Add build variables such as parameters
result.putAll(build.getBuildVariables());
//Add workspace variable
FilePath ws = build.getWorkspace();
if (ws != null) {
result.put("WORKSPACE", ws.getRemote());
}
return result;
}

private void setInfo() {

}

@Extension
@SuppressWarnings("unused")
Expand Down
Expand Up @@ -10,7 +10,6 @@

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
Expand All @@ -28,11 +27,13 @@ public class EnvInjectListener extends RunListener<Run> implements Serializable
@Override
public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {

EnvInjectVariableGetter variableGetter = new EnvInjectVariableGetter();

EnvInjectLogger logger = new EnvInjectLogger(listener);
if (isEnvInjectJobPropertyActive(build)) {
if (variableGetter.isEnvInjectJobPropertyActive(build)) {
try {

EnvInjectJobProperty envInjectJobProperty = getEnvInjectJobProperty(build);
EnvInjectJobProperty envInjectJobProperty = variableGetter.getEnvInjectJobProperty(build);
assert envInjectJobProperty != null;
EnvInjectJobPropertyInfo info = envInjectJobProperty.getInfo();
assert envInjectJobProperty != null && envInjectJobProperty.isOn();
Expand All @@ -42,14 +43,14 @@ public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, Buil

//Add Jenkins System variables
if (envInjectJobProperty.isKeepJenkinsSystemVariables()) {
infraEnvVarsNode.putAll(getJenkinsSystemVariablesCurrentNode(build));
infraEnvVarsNode.putAll(variableGetter.getJenkinsSystemVariablesCurrentNode(build));
infraEnvVarsMaster.putAll(getJenkinsSystemVariablesMaster(build));
}

//Add build variables (such as parameter variables).
if (envInjectJobProperty.isKeepBuildVariables()) {
infraEnvVarsNode.putAll(getBuildVariables(build));
infraEnvVarsMaster.putAll(getBuildVariables(build));
infraEnvVarsNode.putAll(variableGetter.getBuildVariables(build));
infraEnvVarsMaster.putAll(variableGetter.getBuildVariables(build));
}

final FilePath rootPath = getNodeRootPath();
Expand Down Expand Up @@ -102,37 +103,6 @@ public void buildEnvVars(Map<String, String> env) {
};
}

private Map<String, String> getJenkinsSystemVariablesCurrentNode(AbstractBuild build) throws IOException, InterruptedException {

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

result.putAll(build.getEnvironment(new LogTaskListener(LOG, Level.ALL)));

//Global properties
for (NodeProperty<?> nodeProperty : Hudson.getInstance().getGlobalNodeProperties()) {
if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
EnvironmentVariablesNodeProperty environmentVariablesNodeProperty = (EnvironmentVariablesNodeProperty) nodeProperty;
result.putAll(environmentVariablesNodeProperty.getEnvVars());
}
}

//Node properties
Computer computer = Computer.currentComputer();
if (computer != null) {
Node node = computer.getNode();
if (node != null) {
for (NodeProperty<?> nodeProperty : node.getNodeProperties()) {
if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
EnvironmentVariablesNodeProperty environmentVariablesNodeProperty = (EnvironmentVariablesNodeProperty) nodeProperty;
result.putAll(environmentVariablesNodeProperty.getEnvVars());
}
}
}
}

return result;
}

private Map<String, String> getJenkinsSystemVariablesMaster(AbstractBuild build) throws IOException, InterruptedException {

Map<String, String> result = new TreeMap<String, String>();
Expand Down Expand Up @@ -235,35 +205,4 @@ private FilePath getNodeRootPath() {
return null;
}

private Map<String, String> getBuildVariables(AbstractBuild build) {
Map<String, String> result = new HashMap<String, String>();

//Add build process variables
result.putAll(build.getCharacteristicEnvVars());

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

//Add workspace variable
FilePath ws = build.getWorkspace();
if (ws != null) {
result.put("WORKSPACE", ws.getRemote());
}
return result;
}

private boolean isEnvInjectJobPropertyActive(Run run) {
EnvInjectJobProperty envInjectJobProperty = getEnvInjectJobProperty(run);
if (envInjectJobProperty != null) {
EnvInjectJobPropertyInfo info = envInjectJobProperty.getInfo();
if (info != null && envInjectJobProperty.isOn()) {
return true;
}
}
return false;
}

private EnvInjectJobProperty getEnvInjectJobProperty(Run run) {
return (EnvInjectJobProperty) run.getParent().getProperty(EnvInjectJobProperty.class);
}
}
Expand Up @@ -10,7 +10,6 @@
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


Expand Down Expand Up @@ -41,19 +40,4 @@ public Map<String, String> call() throws EnvInjectException {
build.addAction(envInjectAction);
}
}

/**
* Get a new map with the current envMap
*/
public Map<String, String> getCurrentEnvVars(AbstractBuild<?, ?> build) {
EnvInjectAction envInjectAction = build.getAction(EnvInjectAction.class);
Map<String, String> result = new LinkedHashMap<String, String>();
if (envInjectAction != null) {
Map envMap = envInjectAction.getEnvMap();
result.putAll(envMap);
return result;
} else {
return result;
}
}
}
Expand Up @@ -2,11 +2,8 @@

import hudson.EnvVars;
import hudson.Util;
import hudson.model.Computer;
import hudson.model.Hudson;
import org.jenkinsci.plugins.envinject.EnvInjectLogger;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -22,30 +19,6 @@ public EnvInjectEnvVars(EnvInjectLogger logger) {
this.logger = logger;
}

public Map<String, String> getMasterEnvVars() {
return getEnvVars(Hudson.getInstance().toComputer());
}

public Map<String, String> getCurrentNodeEnvVars() {
return getEnvVars(Computer.currentComputer());
}

private Map<String, String> getEnvVars(Computer computer) {
Map<String, String> result = new HashMap<String, String>();
try {
EnvVars envVars = computer.getEnvironment();
for (Map.Entry<String, String> entry : envVars.entrySet()) {
result.put(entry.getKey(), entry.getValue());
}
} catch (IOException ioe) {
logger.error(ioe.getMessage());
} catch (InterruptedException ie) {
logger.error(ie.getMessage());
}
return result;
}


public void resolveVars(Map<String, String> variables, Map<String, String> env) {
for (Map.Entry<String, String> entry : variables.entrySet()) {
entry.setValue(Util.replaceMacro(entry.getValue(), env));
Expand Down

0 comments on commit 91a47fb

Please sign in to comment.