Skip to content
Permalink
Browse files

Fix JENKINS-16380

  • Loading branch information
gboissinot committed Jan 22, 2013
1 parent 98dcefd commit efb96d79b68d7f6d325bdd77cde3d33a6ae3c69c
@@ -1,11 +1,12 @@
package org.jenkinsci.plugins.envinject;

import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.BuildVariableContributor;
import hudson.model.ParameterValue;
import hudson.model.ParametersAction;
import hudson.model.*;
import org.jenkinsci.lib.envinject.EnvInjectException;
import org.jenkinsci.lib.envinject.EnvInjectLogger;
import org.jenkinsci.plugins.envinject.service.EnvironmentVariablesNodeLoader;

import java.util.HashMap;
import java.util.Map;

/**
@@ -23,12 +24,37 @@ public void buildVariablesFor(AbstractBuild build, Map<String, String> variables
ParametersAction parameters = build.getAction(ParametersAction.class);
//Only for a parameterized job
if (parameters != null) {

//Gather global variables for the current node
EnvironmentVariablesNodeLoader environmentVariablesNodeLoader = new EnvironmentVariablesNodeLoader();
Map<String, String> nodeEnvVars = new HashMap<String, String>();
try {
nodeEnvVars = environmentVariablesNodeLoader.gatherEnvironmentVariablesNode(build, build.getBuiltOn(), new EnvInjectLogger(TaskListener.NULL));
} catch (EnvInjectException e) {
e.printStackTrace();
}

EnvInjectPluginAction envInjectAction = build.getAction(EnvInjectPluginAction.class);
if (envInjectAction != null) {
for (ParameterValue p : parameters) {
String key = p.getName();
Map<String, String> injectedEnvVars = envInjectAction.getEnvMap();
if (injectedEnvVars.containsKey(key)) {

//---INPUTS
//GLOBAL envVars, parameters envVars, injectedEnvVars (with global)


//--CLASSIC USE CASES
//CASE1 : var in global and in parameter
// si non job --> parameter win
// si dans job --> ceux du job

//CASE2: var in global and not in parameter --> nothing


// key in parameter, in job (not in global and already injected)
// --> override
if (injectedEnvVars.containsKey(key) && !nodeEnvVars.containsKey(key)) {
variables.put(key, injectedEnvVars.get(key));
}
}
@@ -94,7 +94,7 @@ public void buildEnvVars(Map<String, String> env) {
}
};
} catch (Throwable throwable) {
logger.error("[EnvInject] - [ERROR] - Problems occurs on injecting env vars as a build wrap: " + throwable.getMessage());
logger.error("[EnvInject] - [ERROR] - Problems occurs on injecting env vars as a build wrap: " + throwable.getCause());
build.setResult(Result.FAILURE);
return null;
}
@@ -8,9 +8,6 @@
import hudson.matrix.MatrixRun;
import hudson.model.*;
import hudson.model.listeners.RunListener;
import hudson.remoting.Callable;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import org.jenkinsci.lib.envinject.EnvInjectException;
@@ -80,52 +77,10 @@ private boolean isEligibleJobType(AbstractBuild build) {

private void loadEnvironmentVariablesNode(AbstractBuild build, Node buildNode, EnvInjectLogger logger) throws EnvInjectException {

logger.info("Loading node environment variables.");

if (buildNode == null) {
return;
}

FilePath nodePath = buildNode.getRootPath();
if (nodePath == null) {
return;
}

EnvironmentVariablesNodeLoader environmentVariablesNodeLoader = new EnvironmentVariablesNodeLoader();
Map<String, String> configNodeEnvVars = environmentVariablesNodeLoader.gatherEnvironmentVariablesNode(build, buildNode, logger);
EnvInjectActionSetter envInjectActionSetter = new EnvInjectActionSetter(buildNode.getRootPath());
try {

//Default node envVars
Map<String, String> configNodeEnvVars = new HashMap<String, String>();

//Get env vars for the current node
Map<String, String> nodeEnvVars = nodePath.act(
new Callable<Map<String, String>, IOException>() {
public Map<String, String> call() throws IOException {
return EnvVars.masterEnvVars;
}
}
);

for (NodeProperty<?> nodeProperty : Hudson.getInstance().getGlobalNodeProperties()) {
if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
EnvironmentVariablesNodeProperty variablesNodeProperty = (EnvironmentVariablesNodeProperty) nodeProperty;
EnvVars envVars = variablesNodeProperty.getEnvVars();
EnvInjectEnvVars envInjectEnvVars = new EnvInjectEnvVars(logger);
configNodeEnvVars.putAll(envVars);
envInjectEnvVars.resolveVars(configNodeEnvVars, nodeEnvVars);
}
}

for (NodeProperty<?> nodeProperty : buildNode.getNodeProperties()) {
if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
EnvironmentVariablesNodeProperty variablesNodeProperty = (EnvironmentVariablesNodeProperty) nodeProperty;
EnvVars envVars = variablesNodeProperty.getEnvVars();
EnvInjectEnvVars envInjectEnvVars = new EnvInjectEnvVars(logger);
configNodeEnvVars.putAll(envVars);
envInjectEnvVars.resolveVars(configNodeEnvVars, nodeEnvVars);
}
}

EnvInjectActionSetter envInjectActionSetter = new EnvInjectActionSetter(nodePath);
envInjectActionSetter.addEnvVarsToEnvInjectBuildAction(build, configNodeEnvVars);

} catch (IOException ioe) {
@@ -53,7 +53,6 @@ public int executeScriptSection(FilePath scriptExecutionRoot,

}


return 0;
}

@@ -0,0 +1,81 @@
package org.jenkinsci.plugins.envinject.service;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.Hudson;
import hudson.model.Node;
import hudson.remoting.Callable;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import org.jenkinsci.lib.envinject.EnvInjectException;
import org.jenkinsci.lib.envinject.EnvInjectLogger;

import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
* @author Gregory Boissinot
*/
public class EnvironmentVariablesNodeLoader implements Serializable {

public Map<String, String> gatherEnvironmentVariablesNode(AbstractBuild build, Node buildNode, EnvInjectLogger logger) throws EnvInjectException {

logger.info("Loading node environment variables.");

if (buildNode == null) {
return Collections.emptyMap();
}

FilePath nodePath = buildNode.getRootPath();
if (nodePath == null) {
return Collections.emptyMap();
}

//Default node envVars
Map<String, String> configNodeEnvVars = new HashMap<String, String>();

try {

//Get env vars for the current node
Map<String, String> nodeEnvVars = nodePath.act(
new Callable<Map<String, String>, IOException>() {
public Map<String, String> call() throws IOException {
return EnvVars.masterEnvVars;
}
}
);

for (NodeProperty<?> nodeProperty : Hudson.getInstance().getGlobalNodeProperties()) {
if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
EnvironmentVariablesNodeProperty variablesNodeProperty = (EnvironmentVariablesNodeProperty) nodeProperty;
EnvVars envVars = variablesNodeProperty.getEnvVars();
EnvInjectEnvVars envInjectEnvVars = new EnvInjectEnvVars(logger);
configNodeEnvVars.putAll(envVars);
envInjectEnvVars.resolveVars(configNodeEnvVars, nodeEnvVars);
}
}

for (NodeProperty<?> nodeProperty : buildNode.getNodeProperties()) {
if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
EnvironmentVariablesNodeProperty variablesNodeProperty = (EnvironmentVariablesNodeProperty) nodeProperty;
EnvVars envVars = variablesNodeProperty.getEnvVars();
EnvInjectEnvVars envInjectEnvVars = new EnvInjectEnvVars(logger);
configNodeEnvVars.putAll(envVars);
envInjectEnvVars.resolveVars(configNodeEnvVars, nodeEnvVars);
}
}

return configNodeEnvVars;

} catch (IOException ioe) {
throw new EnvInjectException(ioe);
} catch (InterruptedException ie) {
throw new EnvInjectException(ie);
}
}

}

0 comments on commit efb96d7

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