Skip to content

Commit

Permalink
JENKINS-22489 - Add the ability to specify where each script executes…
Browse files Browse the repository at this point in the history
… instead of all of the scripts when added to a matrix job.
  • Loading branch information
Daniel Heid committed Feb 11, 2018
1 parent 4575c24 commit 1f7ec73
Show file tree
Hide file tree
Showing 26 changed files with 626 additions and 204 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -93,7 +93,11 @@ changes without needing to run to `package` phase.

This version introduces the ability to run Groovy scripts in a sandbox when activated using the checkbox in the configuration view.

Now you can also select for each script whether it should be executed on each axe or on the whole matrix.

* JENKINS-48014 - Allow sandboxing for Groovy scripts
* JENKINS-22489 - Add the ability to specify where each script executes instead of all of the scripts when
added to a matrix job.

### Version 2.4.0

Expand Down
Expand Up @@ -5,35 +5,33 @@
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixRun;
import hudson.model.BuildListener;
import org.jenkinsci.plugins.postbuildscript.processor.Processor;
import org.jenkinsci.plugins.postbuildscript.processor.ProcessorFactory;

import java.io.IOException;

public class ConfigurableMatrixAggregator extends MatrixAggregator {

private final Processor processor;

private final ExecuteOn executeOn;

public ConfigurableMatrixAggregator(
MatrixBuild build,
Launcher launcher,
BuildListener listener,
ProcessorFactory processorFactory,
ExecuteOn executeOn
ProcessorFactory processorFactory
) {
super(build, launcher, listener);
processor = processorFactory.create(build, launcher, listener);
this.executeOn = executeOn;
processor = processorFactory.createMatrixProcessor(build, launcher, listener);
}

@Override
public boolean endRun(MatrixRun run) throws InterruptedException, IOException {
listener.getLogger().println(run);
listener.getLogger().println();
return super.endRun(run);
}

@Override
public boolean endBuild() throws InterruptedException, IOException {
return !executeOn.matrix() || processor.process();
public boolean endBuild() {
return processor.process(true);
}
}
Expand Up @@ -2,18 +2,13 @@

/**
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
* @deprecated Still here for downwards compatibility. Please use {@link org.jenkinsci.plugins.postbuildscript.model.ExecuteOn} instead
*/
public enum ExecuteOn {
@Deprecated
public enum ExecuteOn {

MATRIX,
AXES,
BOTH;
BOTH

public boolean matrix() {
return this == MATRIX || this == BOTH;
}

public boolean axes() {
return this == AXES || this == BOTH;
}
}
Expand Up @@ -10,41 +10,50 @@
import hudson.model.BuildListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Publisher;
import org.jenkinsci.plugins.postbuildscript.model.PostBuildItem;
import org.jenkinsci.plugins.postbuildscript.model.PostBuildStep;
import org.jenkinsci.plugins.postbuildscript.model.Script;
import org.jenkinsci.plugins.postbuildscript.model.ScriptFile;
import org.jenkinsci.plugins.postbuildscript.processor.Processor;
import org.jenkinsci.plugins.postbuildscript.processor.ProcessorFactory;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Collection;

import static org.jenkinsci.plugins.postbuildscript.ExecuteOn.BOTH;
import java.util.List;

public class MatrixPostBuildScript extends PostBuildScript implements MatrixAggregatable {

/**
* Can now be selected individually, see
* @return
*/
@Deprecated
private ExecuteOn executeOn;

@DataBoundConstructor
public MatrixPostBuildScript(Collection<ScriptFile> genericScriptFiles, Collection<ScriptFile> groovyScriptFiles, Collection<Script> groovyScripts, Collection<PostBuildStep> buildSteps, boolean markBuildUnstable, ExecuteOn executeOn) {
public MatrixPostBuildScript(
Collection<ScriptFile> genericScriptFiles,
Collection<ScriptFile> groovyScriptFiles,
Collection<Script> groovyScripts,
Collection<PostBuildStep> buildSteps,
boolean markBuildUnstable
) {
super(genericScriptFiles, groovyScriptFiles, groovyScripts, buildSteps, markBuildUnstable);

this.executeOn = executeOn;
}

@Override
public hudson.matrix.MatrixAggregator createAggregator(
MatrixBuild matrixBuild,
MatrixBuild build,
Launcher launcher,
BuildListener buildListener
BuildListener listener
) {
ProcessorFactory processorFactory = createProcessorFactory();
return new ConfigurableMatrixAggregator(
matrixBuild,
build,
launcher,
buildListener,
processorFactory,
executeOn
listener,
processorFactory
);
}

Expand All @@ -53,29 +62,34 @@ public boolean perform(
AbstractBuild<?, ?> build,
Launcher launcher,
BuildListener listener
) throws InterruptedException, IOException {

Processor processor = createProcessor(build, launcher, listener);

if (executeOn.axes()) {
return processor.process();
}

return true;
) {
ProcessorFactory processorFactory = createProcessorFactory();
Processor processor = processorFactory.createMatrixProcessor(build, launcher, listener);
return processor.process();
}

public ExecuteOn getExecuteOn() {
return executeOn;
}

public Object readResolve() {
super.readResolve();
if (executeOn == null) {
executeOn = BOTH;
if (executeOn != null) {
applyExecuteOn(getGenericScriptFiles());
applyExecuteOn(getGroovyScriptFiles());
applyExecuteOn(getGroovyScripts());
applyExecuteOn(getBuildSteps());
}
return this;
}

private void applyExecuteOn(Iterable<? extends PostBuildItem> items) {
for (PostBuildItem item : items) {
item.setExecuteOn(translate(executeOn));
}
}

private static org.jenkinsci.plugins.postbuildscript.model.ExecuteOn translate(ExecuteOn executeOn) {
return org.jenkinsci.plugins.postbuildscript.model.ExecuteOn.valueOf(executeOn.name());
}

@Extension(optional = true)
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {

Expand All @@ -95,10 +109,6 @@ public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return MatrixProject.class.isAssignableFrom(jobType);
}

public boolean isMatrixProject(Object job) {
return job instanceof MatrixProject;
}

}


Expand Down
Expand Up @@ -17,10 +17,11 @@
import org.jenkinsci.plugins.postbuildscript.model.Script;
import org.jenkinsci.plugins.postbuildscript.model.ScriptFile;
import org.jenkinsci.plugins.postbuildscript.model.ScriptType;
import org.jenkinsci.plugins.postbuildscript.processor.Processor;
import org.jenkinsci.plugins.postbuildscript.processor.ProcessorFactory;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -108,16 +109,14 @@ public boolean perform(
AbstractBuild<?, ?> build,
Launcher launcher,
BuildListener listener
) throws InterruptedException, IOException {

) {
Processor processor = createProcessor(build, launcher, listener);
return processor.process();

}

Processor createProcessor(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) {
private Processor createProcessor(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) {
ProcessorFactory processorFactory = createProcessorFactory();
return processorFactory.create(build, launcher, listener);
return processorFactory.createDefaultProcessor(build, launcher, listener);
}

ProcessorFactory createProcessorFactory() {
Expand Down

This file was deleted.

@@ -0,0 +1,20 @@
package org.jenkinsci.plugins.postbuildscript.model;

/**
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
*/
public enum ExecuteOn {

BOTH,
MATRIX,
AXES;

public boolean matrix() {
return this == MATRIX || this == BOTH;
}

public boolean axes() {
return this == AXES || this == BOTH;
}

}
Expand Up @@ -15,6 +15,8 @@ public class PostBuildItem {

private Role role = Role.BOTH;

private ExecuteOn executeOn;

public PostBuildItem(@Nullable Collection<String> results) {
if (results != null) {
this.results.addAll(results);
Expand Down Expand Up @@ -54,13 +56,25 @@ public Role getRole() {
return role;
}

public ExecuteOn getExecuteOn() {
return executeOn;
}

@DataBoundSetter
public void setExecuteOn(ExecuteOn executeOn) {
this.executeOn = executeOn;
}

public Object readResolve() {
if (results == null) {
results = new HashSet<>();
}
if (role == null) {
role = Role.BOTH;
}
if (executeOn == null) {
executeOn = ExecuteOn.BOTH;
}
return this;
}

Expand Down

0 comments on commit 1f7ec73

Please sign in to comment.