Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Florence.Chabanois committed Jan 26, 2012
1 parent 2031408 commit 2b8dcaa
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 58 deletions.
135 changes: 78 additions & 57 deletions src/main/java/org/jenkinsci/plugins/envinject/EnvInjectListener.java
Expand Up @@ -35,84 +35,106 @@ public class EnvInjectListener extends RunListener<Run> implements Serializable
public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {

EnvInjectVariableGetter variableGetter = new EnvInjectVariableGetter();
if (!isMatrixRun(build) && variableGetter.isEnvInjectJobPropertyActive(build.getParent())) {
EnvInjectLogger logger = new EnvInjectLogger(listener);
try {

EnvInjectLogger logger = new EnvInjectLogger(listener);
logger.info("Preparing an environment for the job.");
try {
if (!isMatrixRun(build)) {

EnvInjectJobProperty envInjectJobProperty = variableGetter.getEnvInjectJobProperty(build.getParent());
assert envInjectJobProperty != null;
EnvInjectJobPropertyInfo info = envInjectJobProperty.getInfo();
assert envInjectJobProperty != null && envInjectJobProperty.isOn();
if (variableGetter.isEnvInjectJobPropertyActive(build)) {
logger.info("Preparing an environment for the job.");
EnvInjectJobProperty envInjectJobProperty = variableGetter.getEnvInjectJobProperty(build.getParent());
assert envInjectJobProperty != null;
EnvInjectJobPropertyInfo info = envInjectJobProperty.getInfo();
assert envInjectJobProperty != null && envInjectJobProperty.isOn();

Map<String, String> infraEnvVarsNode = new LinkedHashMap<String, String>();
Map<String, String> infraEnvVarsMaster = new LinkedHashMap<String, String>();
Map<String, String> infraEnvVarsNode = new LinkedHashMap<String, String>();
Map<String, String> infraEnvVarsMaster = new LinkedHashMap<String, String>();

//Add Jenkins System variables
if (envInjectJobProperty.isKeepJenkinsSystemVariables()) {
logger.info("Jenkins system variables are kept.");
infraEnvVarsNode.putAll(variableGetter.getJenkinsSystemVariablesCurrentNode(build));
infraEnvVarsMaster.putAll(getJenkinsSystemVariablesMaster(build));
}
//Add Jenkins System variables
if (envInjectJobProperty.isKeepJenkinsSystemVariables()) {
logger.info("Jenkins system variables are kept.");
infraEnvVarsNode.putAll(variableGetter.getJenkinsSystemVariablesCurrentNode(build));
infraEnvVarsMaster.putAll(getJenkinsSystemVariablesMaster(build));
}

//Add build variables
if (envInjectJobProperty.isKeepBuildVariables()) {
logger.info("Jenkins build variables are kept.");
Map<String, String> buildVariables = variableGetter.getBuildVariables(build, logger);
infraEnvVarsNode.putAll(buildVariables);
infraEnvVarsMaster.putAll(buildVariables);
}
//Add build variables
if (envInjectJobProperty.isKeepBuildVariables()) {
logger.info("Jenkins build variables are kept.");
Map<String, String> buildVariables = variableGetter.getBuildVariables(build, logger);
infraEnvVarsNode.putAll(buildVariables);
infraEnvVarsMaster.putAll(buildVariables);
}

//Add build parameters (or override)
Map<String, String> parametersVariables = variableGetter.getParametersVariables(build);
infraEnvVarsNode.putAll(parametersVariables);
//Add build parameters (or override)
Map<String, String> parametersVariables = variableGetter.getParametersVariables(build);
infraEnvVarsNode.putAll(parametersVariables);

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

EnvInjectEnvVars envInjectEnvVarsService = new EnvInjectEnvVars(logger);
EnvInjectEnvVars envInjectEnvVarsService = new EnvInjectEnvVars(logger);

//Execute script
int resultCode = envInjectEnvVarsService.executeScript(info.isLoadFilesFromMaster(),
info.getScriptContent(),
rootPath, info.getScriptFilePath(), infraEnvVarsMaster, infraEnvVarsNode, launcher, listener);
if (resultCode != 0) {
build.setResult(Result.FAILURE);
throw new Run.RunnerAbortedException();
}
//Execute script
int resultCode = envInjectEnvVarsService.executeScript(info.isLoadFilesFromMaster(),
info.getScriptContent(),
rootPath, info.getScriptFilePath(), infraEnvVarsMaster, infraEnvVarsNode, launcher, listener);
if (resultCode != 0) {
build.setResult(Result.FAILURE);
throw new Run.RunnerAbortedException();
}

final Map<String, String> propertiesVariables = envInjectEnvVarsService.getEnvVarsPropertiesJobProperty(rootPath,
logger, info.isLoadFilesFromMaster(),
info.getPropertiesFilePath(), info.getPropertiesContent(),
infraEnvVarsMaster, infraEnvVarsNode);

final Map<String, String> propertiesVariables = envInjectEnvVarsService.getEnvVarsPropertiesJobProperty(rootPath,
logger, info.isLoadFilesFromMaster(),
info.getPropertiesFilePath(), info.getPropertiesContent(),
infraEnvVarsMaster, infraEnvVarsNode);
//Get variables get by contribution
Map<String, String> contributionVariables = getEnvVarsByContribution(envInjectJobProperty, listener);

//Get variables get by contribution
Map<String, String> contributionVariables = getEnvVarsByContribution(envInjectJobProperty, listener);
final Map<String, String> resultVariables = envInjectEnvVarsService.getMergedVariables(infraEnvVarsNode, propertiesVariables, contributionVariables);

final Map<String, String> resultVariables = envInjectEnvVarsService.getMergedVariables(infraEnvVarsNode, propertiesVariables, contributionVariables);
//Add an action
new EnvInjectActionSetter(rootPath).addEnvVarsToEnvInjectBuildAction(build, resultVariables);

//Add an action
new EnvInjectActionSetter(rootPath).addEnvVarsToEnvInjectBuildAction(build, resultVariables);
return new Environment() {
@Override
public void buildEnvVars(Map<String, String> env) {
env.putAll(resultVariables);
}
};
}
}

} else {

if (variableGetter.isEnvInjectJobPropertyActive(build)) {

logger.info("Using environment variables injected by the matrix job.");
final Map<String, String> resultVariables = variableGetter.getPreviousEnvVars(build, logger);
final FilePath rootPath = getNodeRootPath();

if (rootPath != null) {
//Add an action
new EnvInjectActionSetter(rootPath).addEnvVarsToEnvInjectBuildAction(build, resultVariables);
}
return new Environment() {
@Override
public void buildEnvVars(Map<String, String> env) {
env.putAll(resultVariables);
}
};
}

} catch (EnvInjectException envEx) {
logger.error("SEVERE ERROR occurs: " + envEx.getMessage());
throw new Run.RunnerAbortedException();
} catch (Run.RunnerAbortedException rre) {
logger.info("Fail the build.");
throw new Run.RunnerAbortedException();
} catch (Throwable throwable) {
logger.error("SEVERE ERROR occurs: " + throwable.getMessage());
throw new Run.RunnerAbortedException();
}

} catch (EnvInjectException envEx) {
logger.error("SEVERE ERROR occurs: " + envEx.getMessage());
throw new Run.RunnerAbortedException();
} catch (Run.RunnerAbortedException rre) {
logger.info("Fail the build.");
throw new Run.RunnerAbortedException();
} catch (Throwable throwable) {
logger.error("SEVERE ERROR occurs: " + throwable.getMessage());
throw new Run.RunnerAbortedException();
}

return new Environment() {
Expand All @@ -123,7 +145,6 @@ private boolean isMatrixRun(AbstractBuild build) {
return build instanceof MatrixRun;
}


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

Map<String, String> result = new TreeMap<String, String>();
Expand Down
@@ -1,6 +1,7 @@
package org.jenkinsci.plugins.envinject.service;

import hudson.FilePath;
import hudson.matrix.MatrixRun;
import hudson.model.*;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
Expand Down Expand Up @@ -132,7 +133,19 @@ private FilePath decideWorkspace(AbstractBuild build, TopLevelItem item, Node n,
return n.getWorkspaceFor(item);
}

public boolean isEnvInjectJobPropertyActive(Job job) {
public boolean isEnvInjectJobPropertyActive(AbstractBuild build) {

if (build == null) {
throw new IllegalArgumentException("A build object must be set.");
}

Job job;
if (build instanceof MatrixRun) {
job = ((MatrixRun) build).getParentBuild().getParent();
} else {
job = build.getParent();
}

EnvInjectJobProperty envInjectJobProperty = getEnvInjectJobProperty(job);
if (envInjectJobProperty != null) {
EnvInjectJobPropertyInfo info = envInjectJobProperty.getInfo();
Expand Down

0 comments on commit 2b8dcaa

Please sign in to comment.