Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Nov 24, 2011
1 parent 0886664 commit 8dd92f5
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 93 deletions.
Expand Up @@ -45,27 +45,21 @@ public Environment setUp(AbstractBuild build, final Launcher launcher, final Bui

try {
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> injectedEnvVars = new HashMap<String, String>();
injectedEnvVars.putAll(previousEnvVars);
injectedEnvVars.putAll(propertiesEnvVars);

//Execute script info
EnvInjectScriptExecutorService scriptExecutorService = new EnvInjectScriptExecutorService(info, envVarsForFilePath, injectedEnvVars, ws, launcher, logger);
EnvInjectScriptExecutorService scriptExecutorService = new EnvInjectScriptExecutorService(info, ws, previousEnvVars, previousEnvVars, launcher, logger);
scriptExecutorService.executeScriptFromInfoObject();

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

// Retrieve triggered cause
if (info.isPopulateTriggerCause()) {
Map<String, String> triggerVariable = new BuildCauseRetriever().getTriggeredCause(build);
injectedEnvVars.putAll(triggerVariable);
}

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

//Remove unset variables
final Map<String, String> resultVariables = envInjectEnvVarsService.removeUnsetVars(injectedEnvVars);
final Map<String, String> resultVariables = envInjectEnvVarsService.getEnvVarsInfo(ws, info, injectedEnvVars);

//Add or get the existing action to add new env vars
envInjectActionSetter.addEnvVarsToEnvInjectBuildAction(build, resultVariables);
Expand All @@ -84,35 +78,6 @@ public void buildEnvVars(Map<String, String> env) {
}
}

private Map<String, String> getEnvVarsForFilePath(EnvInjectVariableGetter variableGetter, AbstractBuild build) throws IOException, InterruptedException {

Map<String, String> buildVarsForFilePath = new HashMap<String, String>();

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(buildEnvVars);

return buildVarsForFilePath;
}

private Map<String, String> retrievePropertiesVars(FilePath ws, EnvInjectLogger logger, Map<String, String> buildVarsForFilePath) throws IOException, InterruptedException {
Map<String, String> envMap = ws.act(new PropertiesVariablesRetriever(info, buildVarsForFilePath, logger));
return envMap;
}

private Map<String, String> getBuildVariables(AbstractBuild build) {
Map<String, String> result = new HashMap<String, String>();
result.putAll(build.getBuildVariables());
FilePath ws = build.getWorkspace();
if (ws != null) {
result.put("WORKSPACE", ws.getRemote());
}
return result;
}

@Extension
@SuppressWarnings("unused")
public static final class DescriptorImpl extends BuildWrapperDescriptor {
Expand Down
Expand Up @@ -10,7 +10,6 @@
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;

import java.io.IOException;
Expand All @@ -35,10 +34,6 @@ public EnvInjectInfo getInfo() {
return info;
}

public void setInfo(EnvInjectInfo info) {
this.info = info;
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {

Expand All @@ -60,14 +55,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

//Get env vars from properties info.
//File information path can be relative to the workspace
Map<String, String> envMap = ws.act(new PropertiesVariablesRetriever(info, variables, new EnvInjectLogger(listener)));
variables.putAll(envMap);

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

//Remove unset variables
final Map<String, String> resultVariables = envInjectEnvVarsService.removeUnsetVars(variables);
final Map<String, String> resultVariables = envInjectEnvVarsService.getEnvVarsInfo(ws, info, variables);

//Set the new build variables map
build.addAction(new EnvironmentContributingAction() {
Expand Down Expand Up @@ -129,6 +117,5 @@ public String getHelpFile() {
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}

}
}
Expand Up @@ -60,10 +60,10 @@ public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, Buil
if (rootPath != null) {

EnvInjectEnvVars envInjectEnvVarsService = new EnvInjectEnvVars(logger);
final Map<String, String> resultVariables = envInjectEnvVarsService.processEnvVars(rootPath, info, infraEnvVarsMaster, infraEnvVarsNode);
final Map<String, String> resultVariables = envInjectEnvVarsService.getEnvVarsJobPropertyInfo(rootPath, info, infraEnvVarsMaster, infraEnvVarsNode);

//Execute script
envInjectEnvVarsService.executeScript(rootPath, info, infraEnvVarsMaster, infraEnvVarsNode, resultVariables, launcher, listener);
envInjectEnvVarsService.executeScript(info, rootPath, infraEnvVarsMaster, infraEnvVarsNode, resultVariables, launcher, listener);

// Retrieve triggered cause
if (info.isPopulateTriggerCause()) {
Expand Down
@@ -1,12 +1,12 @@
package org.jenkinsci.plugins.envinject.service;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.BuildListener;
import hudson.model.Hudson;
import org.jenkinsci.plugins.envinject.EnvInjectException;
import org.jenkinsci.plugins.envinject.EnvInjectInfo;
import org.jenkinsci.plugins.envinject.EnvInjectJobPropertyInfo;
import org.jenkinsci.plugins.envinject.EnvInjectLogger;

Expand All @@ -27,53 +27,83 @@ public EnvInjectEnvVars(EnvInjectLogger logger) {
this.logger = logger;
}

public Map<String, String> processEnvVars(
public Map<String, String> getEnvVarsJobPropertyInfo(
FilePath rootPath,

EnvInjectJobPropertyInfo info,
Map<String, String> infraEnvVarsMaster,
Map<String, String> infraEnvVarsNode) throws IOException, InterruptedException {

final Map<String, String> envMap = getEnvVarsFromProperties(rootPath, logger, info, infraEnvVarsMaster, infraEnvVarsNode);
Map<String, String> variables = new LinkedHashMap<String, String>(infraEnvVarsNode);
variables.putAll(envMap);
return filterEnvVars(logger, infraEnvVarsNode, variables);
return filterEnvVars(infraEnvVarsNode, variables);
}

public Map<String, String> getEnvVarsInfo(
FilePath rootPath,
EnvInjectInfo info,
Map<String, String> currentEnvVars) throws IOException, InterruptedException {

final Map<String, String> propertiesEnv = getEnvVarsFromProperties(rootPath, logger, info, currentEnvVars);
Map<String, String> variables = new LinkedHashMap<String, String>(currentEnvVars);
variables.putAll(propertiesEnv);
return filterEnvVars(currentEnvVars, variables);
}

public void executeScript(FilePath rootPath,
final EnvInjectJobPropertyInfo info,
final Map<String, String> infraEnvVarsMaster,
final Map<String, String> infraEnvVarsNode,
Map<String, String> scriptContentEnvVars,
public void executeScript(final EnvInjectJobPropertyInfo info,
FilePath scriptExecutionRoot,
Map<String, String> infraEnvVarsMaster,
Map<String, String> infraEnvVarsNode,
Map<String, String> computedEnvVars,
final Launcher launcher,
BuildListener listener) throws EnvInjectException {
EnvInjectLogger logger = new EnvInjectLogger(listener);
EnvInjectScriptExecutorService scriptExecutorService;
if (info.isLoadFilesFromMaster()) {
scriptExecutorService = new EnvInjectScriptExecutorService(info, infraEnvVarsMaster, scriptContentEnvVars, rootPath, launcher, logger);
scriptExecutorService = new EnvInjectScriptExecutorService(info, scriptExecutionRoot, infraEnvVarsMaster, computedEnvVars, launcher, logger);
} else {
scriptExecutorService = new EnvInjectScriptExecutorService(info, infraEnvVarsNode, scriptContentEnvVars, rootPath, launcher, logger);
scriptExecutorService = new EnvInjectScriptExecutorService(info, scriptExecutionRoot, infraEnvVarsNode, computedEnvVars, launcher, logger);
}
scriptExecutorService.executeScriptFromInfoObject();
}

private Map<String, String> filterEnvVars(EnvInjectLogger logger, Map<String, String> infraEnvVarsNode, Map<String, String> variables) {
private Map<String, String> filterEnvVars(Map<String, String> previousEnvVars, Map<String, String> variables) {

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

//Remove unset variables
return removeUnsetVars(variables);
}

public void resolveVars(Map<String, String> variables, Map<String, String> env) {
private void resolveVars(Map<String, String> variables, Map<String, String> env) {

//Resolve variables against env
for (Map.Entry<String, String> entry : variables.entrySet()) {
entry.setValue(Util.replaceMacro(entry.getValue(), env));
}
EnvVars.resolve(variables);

//Resolve variables against variables itself
boolean stopToResolveVars = false;
int nbUnresolvedVar = 0;

while (!stopToResolveVars) {
int previousNbUnresolvedVar = nbUnresolvedVar;
nbUnresolvedVar = 0;
for (Map.Entry<String, String> entry : variables.entrySet()) {
String value = Util.replaceMacro(entry.getValue(), variables);
entry.setValue(value);
if (isUnresolvedVar(value)) {
nbUnresolvedVar++;
}
}
if (previousNbUnresolvedVar == nbUnresolvedVar) {
stopToResolveVars = true;
}
}
}

public Map<String, String> removeUnsetVars(Map<String, String> envVars) {
private 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())) {
Expand All @@ -86,10 +116,7 @@ public Map<String, String> removeUnsetVars(Map<String, String> envVars) {
}

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

private Map<String, String> getEnvVarsFromProperties(FilePath rootPath,
Expand All @@ -106,5 +133,12 @@ private Map<String, String> getEnvVarsFromProperties(FilePath rootPath,
return resultMap;
}


private Map<String, String> getEnvVarsFromProperties(FilePath rootPath,
EnvInjectLogger logger,
final EnvInjectInfo info,
final Map<String, String> infraEnvVarsNode) throws IOException, InterruptedException {
final Map<String, String> resultMap = new LinkedHashMap<String, String>();
resultMap.putAll(rootPath.act(new PropertiesVariablesRetriever(info, infraEnvVarsNode, logger)));
return resultMap;
}
}
Expand Up @@ -21,21 +21,25 @@ public class EnvInjectScriptExecutorService {

private EnvInjectJobPropertyInfo info;

private Map<String, String> scriptPathEnvVars;
private FilePath scriptExecutionRoot;

private Map<String, String> scriptContentEnvVars;
private Map<String, String> scriptPathExecutionEnvVars;

private FilePath rootScriptExecutionPath;
private Map<String, String> scriptExecutionEnvVars;

private Launcher launcher;

private EnvInjectLogger logger;

public EnvInjectScriptExecutorService(EnvInjectJobPropertyInfo info, Map<String, String> scriptPathEnvVars, Map<String, String> scriptContentEnvVars, FilePath rootScriptExecutionPath, Launcher launcher, EnvInjectLogger logger) {
public EnvInjectScriptExecutorService(EnvInjectJobPropertyInfo info,
FilePath scriptExecutionRoot,
Map<String, String> scriptPathExecutionEnvVars,
Map<String, String> scriptExecutionEnvVars,
Launcher launcher, EnvInjectLogger logger) {
this.info = info;
this.scriptPathEnvVars = scriptPathEnvVars;
this.scriptContentEnvVars = scriptContentEnvVars;
this.rootScriptExecutionPath = rootScriptExecutionPath;
this.scriptExecutionRoot = scriptExecutionRoot;
this.scriptPathExecutionEnvVars = scriptPathExecutionEnvVars;
this.scriptExecutionEnvVars = scriptExecutionEnvVars;
this.launcher = launcher;
this.logger = logger;
}
Expand All @@ -44,25 +48,24 @@ public void executeScriptFromInfoObject() throws EnvInjectException {

//Process the script file path
if (info.getScriptFilePath() != null) {
String scriptFilePathResolved = Util.replaceMacro(info.getScriptFilePath(), scriptPathEnvVars);
String scriptFilePathResolved = Util.replaceMacro(info.getScriptFilePath(), scriptPathExecutionEnvVars);
String scriptFilePathNormalized = scriptFilePathResolved.replace("\\", "/");
executeScriptPath(scriptFilePathNormalized);
}

//Process the script content
if (info.getScriptContent() != null) {
String scriptResolved = Util.replaceMacro(info.getScriptContent(), scriptContentEnvVars);
executeScriptContent(scriptResolved);
executeScriptContent(info.getScriptContent());
}
}


private void executeScriptPath(String scriptFilePath) throws EnvInjectException {
try {
FilePath f = new FilePath(rootScriptExecutionPath, scriptFilePath);
FilePath f = new FilePath(scriptExecutionRoot, scriptFilePath);
if (f.exists()) {
launcher.getListener().getLogger().println(String.format("Executing '%s' script.", scriptFilePath));
int cmdCode = launcher.launch().cmds(new File(scriptFilePath)).stdout(launcher.getListener()).envs(scriptContentEnvVars).pwd(rootScriptExecutionPath).join();
int cmdCode = launcher.launch().cmds(new File(scriptFilePath)).stdout(launcher.getListener()).envs(scriptPathExecutionEnvVars).pwd(scriptExecutionRoot).join();
if (cmdCode != 0) {
logger.info(String.format("The exit code is '%s'. Fail the build.", cmdCode));
}
Expand All @@ -87,9 +90,9 @@ private void executeScriptContent(String scriptContent) throws EnvInjectExceptio
batchRunner = new BatchFile(scriptContent);
}

FilePath tmpFile = batchRunner.createScriptFile(rootScriptExecutionPath);
FilePath tmpFile = batchRunner.createScriptFile(scriptExecutionRoot);
logger.info(String.format("Executing the script: \n %s", scriptContent));
int cmdCode = launcher.launch().cmds(batchRunner.buildCommandLine(tmpFile)).stdout(launcher.getListener()).pwd(rootScriptExecutionPath).join();
int cmdCode = launcher.launch().cmds(batchRunner.buildCommandLine(tmpFile)).stdout(launcher.getListener()).envs(scriptExecutionEnvVars).pwd(scriptExecutionRoot).join();
if (cmdCode != 0) {
String message = String.format("The exit code is '%s'. Fail the build.", cmdCode);
logger.error(message);
Expand Down
Expand Up @@ -75,5 +75,4 @@ private File getFile(File base, String scriptFilePath) {
return file.exists() ? file : null;
}


}
Expand Up @@ -3,6 +3,6 @@
Execute a script file aimed at setting an environment such a create folders, copying files, and so on.<br/>
Give the script file content. <br/>
You can use the above properties variables. <br/>
However, adding or overriding environment variables in the script has no impact in the build job.
However, adding or overriding environment variables in the script doesn't have any impacts in the build job.
</p>
</div>
Expand Up @@ -4,6 +4,6 @@
Give the script file path. <br/>
You must give an absolute path (the process is executed before a SCM checkout).
You can use the above properties variables. <br/>
However, adding or overriding environment variables in the script has no impact in the build job.
However, adding or overriding environment variables in the script doesn't have any impacts in the build job.
</p>
</div>

0 comments on commit 8dd92f5

Please sign in to comment.