Skip to content

Commit

Permalink
[JENKINS-14144] use a WorkspaceListener to avoid injecting a temporar…
Browse files Browse the repository at this point in the history
…y BuildWrapper
  • Loading branch information
ndeloof committed Jul 9, 2013
1 parent 96a5269 commit a9be296
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 127 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.444</version>
<version>1.445</version>
</parent>

<artifactId>envinject</artifactId>
Expand Down
Expand Up @@ -97,46 +97,24 @@ private boolean isEnvInjectJobPropertyActive(AbstractBuild build) {
return envInjectJobProperty != null;
}

public static class JobSetupEnvironmentWrapper extends BuildWrapper {

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

public JobSetupEnvironmentWrapperDescriptor() {
}

public JobSetupEnvironmentWrapperDescriptor(Class<? extends BuildWrapper> clazz) {
super(JobSetupEnvironmentWrapper.class);
}

@Override
public boolean isApplicable(AbstractProject<?, ?> item) {
return false;
}

@Override
public String getDisplayName() {
return null;
}
}
@Extension
public static class JobSetupEnvironmentWorkspaceListener extends WorkspaceListener {

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

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

assert envInjectJobProperty != null;
EnvInjectLogger envInjectLogger = new EnvInjectLogger(listener);

if (envInjectJobProperty.isKeepBuildVariables()) {
try {
//Get previous
Map<String, String> previousEnvVars = variableGetter.getEnvVarsPreviousSteps(build, envInjectLogger);

//Add workspace
FilePath ws = build.getWorkspace();
if (previousEnvVars.get("WORKSPACE") == null) {
previousEnvVars.put("WORKSPACE", ws.getRemote());
}
Expand All @@ -152,16 +130,14 @@ public void preCheckout(AbstractBuild build, Launcher launcher, BuildListener li
new EnvInjectActionSetter(build.getBuiltOn().getRootPath()).addEnvVarsToEnvInjectBuildAction(build, cleanVariables);

} catch (EnvInjectException e) {
throw new IOException(e.getMessage());
throw new RuntimeException(e.getMessage());
} catch (InterruptedException e) {
throw new RuntimeException(e.getMessage());
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}
}

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

private Environment setUpEnvironmentJobPropertyObject(AbstractBuild build, Launcher launcher, BuildListener listener, EnvInjectLogger logger) throws IOException, InterruptedException, EnvInjectException {
Expand Down Expand Up @@ -228,10 +204,6 @@ private Environment setUpEnvironmentJobPropertyObject(AbstractBuild build, Launc
//Add an action to share injected environment variables
new EnvInjectActionSetter(rootPath).addEnvVarsToEnvInjectBuildAction(build, mergedVariables);

//Add a wrapper to intercept the preCheckout event and add specific variables such as WORKSPACE variable
BuildWrapperService wrapperService = new BuildWrapperService();
wrapperService.addBuildWrapper(build, new JobSetupEnvironmentWrapper());


return new Environment() {
@Override
Expand Down Expand Up @@ -324,17 +296,7 @@ public void onCompleted(Run run, TaskListener listener) {
if (!(build instanceof MatrixBuild)) {

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

//Remove technical wrappers
try {
BuildWrapperService wrapperService = new BuildWrapperService();
wrapperService.removeBuildWrappers(build, JobSetupEnvironmentWrapper.class);
} catch (EnvInjectException e) {
logger.error("SEVERE ERROR occurs: " + e.getMessage());
throw new Run.RunnerAbortedException();
}
} else {
if (envInjectAction == null) {
try {
envVars.putAll(build.getEnvironment(listener));
} catch (IOException e) {
Expand Down

This file was deleted.

0 comments on commit a9be296

Please sign in to comment.