Skip to content
Permalink
Browse files

Fix JENKINS-13041

  • Loading branch information...
gboissinot committed Mar 10, 2012
1 parent 268126e commit 9fe6112e892c11143ccfd180218ca4cc44fb1dc1
@@ -1,8 +1,8 @@
package org.jenkinsci.plugins.envinject;

import hudson.*;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
import hudson.matrix.MatrixRun;
import hudson.model.*;
import hudson.model.listeners.RunListener;
import hudson.slaves.EnvironmentVariablesNodeProperty;
@@ -29,34 +29,28 @@
@Extension
public class EnvInjectListener extends RunListener<Run> implements Serializable {


@Override
public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
EnvInjectLogger logger = new EnvInjectLogger(listener);
try {
if (isEnvInjectJobPropertyActive(build)) {
if (!isMatrixRun(build)) {
addBuildWrapper(build, new JobSetupEnvironmentWrapper());
return setUpEnvironmentNonMatrixRun(build, launcher, listener);
} else {
return setUpEnvironmentMatrixRun(build, listener);
if (!(build instanceof MatrixBuild)) {
EnvInjectLogger logger = new EnvInjectLogger(listener);
try {
if (isEnvInjectJobPropertyActive(build)) {
return setUpEnvironmentRun(build, launcher, listener);
}
} 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() {
};

}

@SuppressWarnings("unchecked")
private void addBuildWrapper(AbstractBuild build, BuildWrapper buildWrapper) throws EnvInjectException {
try {
if (buildWrapper != null) {
@@ -80,7 +74,6 @@ private boolean isEnvInjectJobPropertyActive(AbstractBuild build) {
return envInjectJobProperty != null;
}


public static class JobSetupEnvironmentWrapper extends BuildWrapper {

@SuppressWarnings("unused")
@@ -136,7 +129,7 @@ public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener l
}
}

private Environment setUpEnvironmentNonMatrixRun(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException, EnvInjectException {
private Environment setUpEnvironmentRun(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException, EnvInjectException {

EnvInjectVariableGetter variableGetter = new EnvInjectVariableGetter();
EnvInjectJobProperty envInjectJobProperty = variableGetter.getEnvInjectJobProperty(build);
@@ -236,28 +229,6 @@ private void injectPasswords(AbstractBuild build, EnvInjectJobProperty envInject

}

private Environment setUpEnvironmentMatrixRun(AbstractBuild build, BuildListener listener) throws IOException, InterruptedException, EnvInjectException {
EnvInjectVariableGetter variableGetter = new EnvInjectVariableGetter();
EnvInjectLogger logger = new EnvInjectLogger(listener);
logger.info("Using environment variables injected by the parent matrix job.");
final Map<String, String> resultVariables = variableGetter.getEnvVarsPreviousSteps(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);
}
};
}

private boolean isMatrixRun(AbstractBuild build) {
return build instanceof MatrixRun;
}

private Map<String, String> getJenkinsSystemVariables(boolean onMaster) throws IOException, InterruptedException {

Map<String, String> result = new TreeMap<String, String>();
@@ -361,26 +332,46 @@ private boolean isEnvInjectAction(EnvironmentContributingAction a) {
@Override
public void onCompleted(Run run, TaskListener listener) {

EnvVars envVars = new EnvVars();
EnvInjectLogger logger = new EnvInjectLogger(listener);
AbstractBuild build = (AbstractBuild) run;
if (!(build instanceof MatrixBuild)) {
EnvVars envVars = new EnvVars();
EnvInjectLogger logger = new EnvInjectLogger(listener);

EnvInjectPluginAction envInjectAction = run.getAction(EnvInjectPluginAction.class);
if (envInjectAction != null) {

//Remove technical wrappers
try {
removeTechnicalWrappers(build, JobSetupEnvironmentWrapper.class, EnvInjectPasswordWrapper.class);
} catch (EnvInjectException e) {
logger.error("SEVERE ERROR occurs: " + e.getMessage());
throw new Run.RunnerAbortedException();
}

} else {
//Keep classic injected env vars
AbstractBuild abstractBuild = (AbstractBuild) run;
try {
envVars.putAll(abstractBuild.getEnvironment(listener));
} catch (IOException e) {
logger.error("SEVERE ERROR occurs: " + e.getMessage());
throw new Run.RunnerAbortedException();
} catch (InterruptedException e) {
logger.error("SEVERE ERROR occurs: " + e.getMessage());
throw new Run.RunnerAbortedException();
}
}

EnvInjectPluginAction envInjectAction = run.getAction(EnvInjectPluginAction.class);
if (envInjectAction != null) {
//Mask passwords
maskPasswordsIfAny(build, logger, envVars);

//Remove technical wrappers
//Add or override EnvInject Action
EnvInjectActionSetter envInjectActionSetter = new EnvInjectActionSetter(getNodeRootPath());
try {
removeTechnicalWrappers(build, JobSetupEnvironmentWrapper.class, EnvInjectPasswordWrapper.class);
envInjectActionSetter.addEnvVarsToEnvInjectBuildAction((AbstractBuild<?, ?>) run, envVars);
} catch (EnvInjectException e) {
logger.error("SEVERE ERROR occurs: " + e.getMessage());
throw new Run.RunnerAbortedException();
}

} else {
//Keep classic injected env vars
AbstractBuild abstractBuild = (AbstractBuild) run;
try {
envVars.putAll(abstractBuild.getEnvironment(listener));
} catch (IOException e) {
logger.error("SEVERE ERROR occurs: " + e.getMessage());
throw new Run.RunnerAbortedException();
@@ -389,26 +380,9 @@ public void onCompleted(Run run, TaskListener listener) {
throw new Run.RunnerAbortedException();
}
}

//Mask passwords
maskPasswordsIfAny(build, logger, envVars);

//Add or override EnvInject Action
EnvInjectActionSetter envInjectActionSetter = new EnvInjectActionSetter(getNodeRootPath());
try {
envInjectActionSetter.addEnvVarsToEnvInjectBuildAction((AbstractBuild<?, ?>) run, envVars);
} catch (EnvInjectException e) {
logger.error("SEVERE ERROR occurs: " + e.getMessage());
throw new Run.RunnerAbortedException();
} catch (IOException e) {
logger.error("SEVERE ERROR occurs: " + e.getMessage());
throw new Run.RunnerAbortedException();
} catch (InterruptedException e) {
logger.error("SEVERE ERROR occurs: " + e.getMessage());
throw new Run.RunnerAbortedException();
}
}

@SuppressWarnings("unchecked")
private void removeTechnicalWrappers(AbstractBuild build, Class<JobSetupEnvironmentWrapper> jobSetupEnvironmentWrapperClass, Class<EnvInjectPasswordWrapper> envInjectPasswordWrapperClass) throws EnvInjectException {

AbstractProject abstractProject = build.getProject();
@@ -1,7 +1,7 @@
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:u="/util">

<f:optionalBlock name="on"
title="${%Prepare an environment for the job}"
title="${%Prepare an environment for the run}"
checked="${instance.on}"
help="/plugin/envinject/help.html">

@@ -1,7 +1,7 @@
<div>
<p>
If this option is checked, Jenkins makes it possible to set an environment
for the Job by defining environment variables and execute a script (a setup script).<br/>
for the build job (or for each run on a matrix project) by defining environment variables and execute a script (a setup script).<br/>
All these actions will be executed before a SCM checkout.<br/>
By default, after the set up, only injected variables (with Jenkins variables) will be available in the build
scripts, in the Jenkins post-actions, and so on. <br/>

0 comments on commit 9fe6112

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