Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Mar 9, 2012
1 parent 29ecfaf commit bf15513
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 44 deletions.
Expand Up @@ -49,10 +49,15 @@ public Environment setUp(AbstractBuild build, final Launcher launcher, final Bui
EnvInjectEnvVars envInjectEnvVarsService = new EnvInjectEnvVars(logger);

try {
Map<String, String> previousEnvVars = variableGetter.getEnvVarsPreviousSteps(build, logger);

Map<String, String> previousEnvVars = variableGetter.getEnvVarsPreviousSteps(build, logger);
Map<String, String> injectedEnvVars = new HashMap<String, String>(previousEnvVars);

//Add workspace if not set
if (ws != null) {
injectedEnvVars.put("WORKSPACE", ws.getRemote());
}

//Get result variables
Map<String, String> propertiesEnvVars = envInjectEnvVarsService.getEnvVarsPropertiesProperty(ws, logger, info.getPropertiesFilePath(), info.getPropertiesContentMap(), injectedEnvVars);

Expand Down
Expand Up @@ -55,6 +55,11 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
//Get current envVars
Map<String, String> variables = new HashMap<String, String>(previousEnvVars);

//Add workspace if not set
if (ws != null) {
variables.put("WORKSPACE", ws.getRemote());
}

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

Expand Down
Expand Up @@ -37,6 +37,7 @@ public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, Buil
if (isEnvInjectJobPropertyActive(build)) {
if (!isMatrixRun(build)) {
addBuildWrapper(build, new JobSetupEnvironmentWrapper());
return setUpEnvironmentNonMatrixRun(build, launcher, listener);
} else {
return setUpEnvironmentMatrixRun(build, listener);
}
Expand Down Expand Up @@ -73,23 +74,6 @@ private void addBuildWrapper(AbstractBuild build, BuildWrapper buildWrapper) thr
}
}

private void removeBuildWrapper(AbstractBuild build, BuildWrapper buildWrapper) throws EnvInjectException {
try {
if (buildWrapper != null) {
AbstractProject abstractProject = build.getProject();
if (abstractProject instanceof MatrixProject) {
MatrixProject project = (MatrixProject) abstractProject;
project.getBuildWrappersList().remove(buildWrapper);
} else {
Project project = (Project) abstractProject;
project.getBuildWrappersList().remove(buildWrapper);
}
}
} catch (IOException ioe) {
throw new EnvInjectException(ioe);
}
}

private boolean isEnvInjectJobPropertyActive(AbstractBuild build) {
EnvInjectVariableGetter variableGetter = new EnvInjectVariableGetter();
EnvInjectJobProperty envInjectJobProperty = variableGetter.getEnvInjectJobProperty(build);
Expand All @@ -98,21 +82,8 @@ private boolean isEnvInjectJobPropertyActive(AbstractBuild build) {


public static class JobSetupEnvironmentWrapper extends BuildWrapper {
@Override
public void preCheckout(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
try {
new EnvInjectListener().setUpEnvironmentNonMatrixRun(build, launcher, listener);
} catch (EnvInjectException e) {
throw new IOException(e);
}
}

@Override
public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
return new Environment() {
};
}

@SuppressWarnings("unused")
@Extension
public static class JobSetupEnvironmentWrapperDescriptor extends BuildWrapperDescriptor {

Expand All @@ -133,6 +104,36 @@ public String getDisplayName() {
return null;
}
}

@Override
public void preCheckout(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {

EnvInjectLogger envInjectLogger = new EnvInjectLogger(listener);
EnvInjectVariableGetter variableGetter = new EnvInjectVariableGetter();
EnvInjectJobProperty envInjectJobProperty = variableGetter.getEnvInjectJobProperty(build);

assert envInjectJobProperty != null;

if (envInjectJobProperty.isKeepBuildVariables()) {
try {
//Get previous
Map<String, String> previousEnvVars = variableGetter.getEnvVarsPreviousSteps(build, envInjectLogger);
//Add workspace
FilePath ws = build.getWorkspace();
previousEnvVars.put("WORKSPACE", ws.getRemote());
//Set new env vars
new EnvInjectActionSetter(build.getBuiltOn().getRootPath()).addEnvVarsToEnvInjectBuildAction(build, previousEnvVars);
} catch (EnvInjectException e) {
throw new IOException(e);
}
}
}

@Override
public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
return new Environment() {
};
}
}

private Environment setUpEnvironmentNonMatrixRun(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException, EnvInjectException {
Expand Down Expand Up @@ -464,5 +465,4 @@ private void maskPasswordsIfAny(AbstractBuild build, EnvInjectLogger logger, Map
}
}


}
@@ -1,7 +1,6 @@
package org.jenkinsci.plugins.envinject.service;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.matrix.MatrixRun;
import hudson.model.*;
import hudson.slaves.EnvironmentVariablesNodeProperty;
Expand Down Expand Up @@ -110,10 +109,6 @@ public Map<String, String> getBuildVariables(AbstractBuild build, EnvInjectLogge
Map<String, String> triggerVariable = new BuildCauseRetriever().getTriggeredCause(build);
result.putAll(triggerVariable);

//Add workspace
FilePath ws = build.getWorkspace();
result.put("WORKSPACE", ws.getRemote());

return result;
}

Expand Down Expand Up @@ -171,12 +166,6 @@ public Map<String, String> getEnvVarsPreviousSteps(AbstractBuild build, EnvInjec
if (envInjectDetector.isEnvInjectActivated(build)) {
result.putAll(getCurrentInjectedEnvVars(build));

//Add workspace if not set
FilePath ws = build.getWorkspace();
if (ws != null) {
result.put("WORKSPACE", ws.getRemote());
}

//Add build variables with axis for a MatrixRun
if (build instanceof MatrixRun) {
result.putAll(build.getBuildVariables());
Expand Down

0 comments on commit bf15513

Please sign in to comment.