Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Mar 10, 2012
1 parent 268126e commit 9fe6112
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 77 deletions.
124 changes: 49 additions & 75 deletions src/main/java/org/jenkinsci/plugins/envinject/EnvInjectListener.java
@@ -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;
Expand All @@ -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) {
Expand All @@ -80,7 +74,6 @@ private boolean isEnvInjectJobPropertyActive(AbstractBuild build) {
return envInjectJobProperty != null;
}


public static class JobSetupEnvironmentWrapper extends BuildWrapper {

@SuppressWarnings("unused")
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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>();
Expand Down Expand Up @@ -361,26 +332,46 @@ private Map<String, String> getEnvVarsByContribution(AbstractBuild build, EnvInj
@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();
Expand All @@ -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();
Expand Down
@@ -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">

Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/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/>
Expand Down

0 comments on commit 9fe6112

Please sign in to comment.