Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Oct 1, 2011
1 parent b5a97f7 commit 05dee8c
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 58 deletions.
3 changes: 2 additions & 1 deletion pom.xml
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

Expand Down
@@ -1,6 +1,5 @@
package org.jenkinsci.plugins.envinject;

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
Expand Down Expand Up @@ -38,15 +37,19 @@ public EnvInjectJobPropertyInfo getInfo() {
@Override
public Environment setUp(AbstractBuild build, final Launcher launcher, final BuildListener listener) throws IOException, InterruptedException {

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

//Get current envVars
Map<String, String> variables = envInjectActionSetter.getCurrentEnvVars(build);
//Get previous enVars
Map<String, String> previousEnvVars = envInjectEnvVarsService.getComputerEnvVars();
previousEnvVars.putAll(envInjectActionSetter.getCurrentEnvVars(build));

try {

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

//Always keep build variables (such as parameter variables).
variables.putAll(getAndAddBuildVariables(build));

Expand All @@ -65,11 +68,11 @@ public Environment setUp(AbstractBuild build, final Launcher launcher, final Bui
variables.putAll(triggerVariable);
}

//Resolve vars each other
EnvVars.resolve(variables);
//Resolves vars each other
envInjectEnvVarsService.resolveVars(variables, previousEnvVars);

//Remove unset variables
final Map<String, String> resultVariables = new EnvInjectEnvVarsUnset(logger).removeUnsetVars(variables);
final Map<String, String> resultVariables = envInjectEnvVarsService.removeUnsetVars(variables);

//Add or get the existing action to add new env vars
envInjectActionSetter.addEnvVarsToEnvInjectBuildAction(build, resultVariables);
Expand Down
Expand Up @@ -9,7 +9,7 @@
import hudson.tasks.Builder;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.envinject.service.EnvInjectActionSetter;
import org.jenkinsci.plugins.envinject.service.EnvInjectEnvVarsUnset;
import org.jenkinsci.plugins.envinject.service.EnvInjectEnvVars;
import org.jenkinsci.plugins.envinject.service.PropertiesVariablesRetriever;
import org.kohsuke.stapler.StaplerRequest;

Expand Down Expand Up @@ -40,10 +40,16 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
FilePath ws = build.getWorkspace();
EnvInjectActionSetter envInjectActionSetter = new EnvInjectActionSetter(ws);
EnvInjectLogger logger = new EnvInjectLogger(listener);
EnvInjectEnvVars envInjectEnvVarsService = new EnvInjectEnvVars(logger);

//Get previous enVars
Map<String, String> previousEnvVars = envInjectEnvVarsService.getComputerEnvVars();
previousEnvVars.putAll(envInjectActionSetter.getCurrentEnvVars(build));

try {

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

//Always keep build variables (such as parameter variables).
variables.putAll(getAndAddBuildVariables(build));
Expand All @@ -53,11 +59,12 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
Map<String, String> envMap = ws.act(new PropertiesVariablesRetriever(info, variables, new EnvInjectLogger(listener)));
variables.putAll(envMap);

//Resolve vars each other
EnvVars.resolve(variables);

//Resolves vars each other
envInjectEnvVarsService.resolveVars(variables, previousEnvVars);

//Remove unset variables
final Map<String, String> resultVariables = new EnvInjectEnvVarsUnset(logger).removeUnsetVars(variables);
final Map<String, String> resultVariables = envInjectEnvVarsService.removeUnsetVars(variables);

//Set the new build variables map
build.addAction(new EnvironmentContributingAction() {
Expand Down
@@ -1,6 +1,5 @@
package org.jenkinsci.plugins.envinject;

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
Expand Down Expand Up @@ -32,7 +31,7 @@ public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, Buil
if (isEnvInjectJobPropertyActive(build)) {
try {

Map<String, String> variables = new LinkedHashMap<String, String>();
Map<String, String> infraEnvVars = new LinkedHashMap<String, String>();

EnvInjectJobProperty envInjectJobProperty = getEnvInjectJobProperty(build);
assert envInjectJobProperty != null;
Expand All @@ -41,17 +40,19 @@ public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, Buil

//Add Jenkins System variables
if (envInjectJobProperty.isKeepJenkinsSystemVariables()) {
variables.putAll(build.getEnvironment(new LogTaskListener(LOG, Level.ALL)));
infraEnvVars.putAll(build.getEnvironment(new LogTaskListener(LOG, Level.ALL)));
}

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

final FilePath rootPath = getNodeRootPath();
if (rootPath != null) {

Map<String, String> variables = new LinkedHashMap<String, String>(infraEnvVars);

//Build a properties object with all information
final Map<String, String> envMap = getEnvVarsFromProperties(rootPath, info, variables, launcher, listener);
variables.putAll(envMap);
Expand All @@ -62,11 +63,13 @@ public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, Buil
variables.putAll(triggerVariable);
}

EnvInjectEnvVars envInjectEnvVarsService = new EnvInjectEnvVars(logger);

//Resolves vars each other
EnvVars.resolve(variables);
envInjectEnvVarsService.resolveVars(variables, infraEnvVars);

//Remove unset variables
final Map<String, String> resultVariables = new EnvInjectEnvVarsUnset(logger).removeUnsetVars(variables);
final Map<String, String> resultVariables = envInjectEnvVarsService.removeUnsetVars(variables);

//Add an action
new EnvInjectActionSetter(rootPath).addEnvVarsToEnvInjectBuildAction(build, resultVariables);
Expand Down Expand Up @@ -96,7 +99,6 @@ public void buildEnvVars(Map<String, String> env) {
};
}


private Map<String, String> getEnvVarsFromProperties(FilePath rootPath, final EnvInjectJobPropertyInfo info, final Map<String, String> currentEnvVars, final Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
final Map<String, String> resultMap = new LinkedHashMap<String, String>();
EnvInjectLogger logger = new EnvInjectLogger(listener);
Expand Down
@@ -0,0 +1,66 @@
package org.jenkinsci.plugins.envinject.service;

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

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

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

EnvInjectLogger logger;

public EnvInjectEnvVars(EnvInjectLogger logger) {
this.logger = logger;
}

public Map<String, String> getComputerEnvVars() {
Map<String, String> result = new HashMap<String, String>();
try {
Computer c = Computer.currentComputer();
EnvVars envVars = c.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));
}
EnvVars.resolve(variables);
}

public Map<String, String> removeUnsetVars(Map<String, String> envVars) {
Map<String, String> result = new HashMap<String, String>();
for (Map.Entry<String, String> entry : envVars.entrySet()) {
if (!isUnresolvedVar(entry.getValue())) {
result.put(entry.getKey(), entry.getValue());
} else {
logger.info(String.format("Unset unresolved '%s' variable.", entry.getKey()));
}
}
return result;
}

private boolean isUnresolvedVar(String value) {
if (value == null) {
return false;
}
return value.contains("$");
}

}

This file was deleted.

0 comments on commit 05dee8c

Please sign in to comment.