Skip to content
Browse files

Merge pull request #12 from dawidmalina/multi

JENKINS-36697 Must show "Run Only on Parent" for
  • Loading branch information
dawidmalina committed Jul 30, 2016
2 parents 3dae55b + 799792f commit a872dd6bc689d7740cfce27bcbc6e15b1e62d54d
17 pom.xml
@@ -6,7 +6,7 @@
<!-- Which version of Jenkins is this plugin built against? -->

@@ -79,7 +79,20 @@
<!-- Test Dependencies -->
@@ -35,6 +35,8 @@
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import com.github.mjdetullio.jenkins.plugins.multibranch.MatrixMultiBranchProject;

import com.lookout.jenkins.commands.Commands;
import com.lookout.jenkins.commands.PowerShell;
import com.lookout.jenkins.commands.Shebangs;
@@ -228,7 +230,7 @@ public void buildEnvVars(Map<String, String> env) {

* Create an aggregator that will calculate the environment once iff onlyRunOnParent is true.
* Create an aggregator that will calculate the environment once if onlyRunOnParent is true.
* The aggregator we return is called on the parent job for matrix jobs. In it we generate the environment once and
* persist it in an Action (of type {@link PersistedEnvironment}) if the job has onlyRunOnParent enabled. The
@@ -287,7 +289,11 @@ public boolean isApplicable(AbstractProject<?, ?> project) {

public boolean isMatrix(StaplerRequest request) {
return (request.findAncestorObject(AbstractProject.class) instanceof MatrixProject);
if (request.findAncestorObject(MatrixMultiBranchProject.class) != null ||
request.findAncestorObject(MatrixProject.class) != null) {
return Boolean.TRUE;
return Boolean.FALSE;
@@ -1,3 +1,4 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">

@@ -1,3 +1,4 @@
<?jelly escape-by-default='true'?>
This view is used to render the installed plugins page.
@@ -0,0 +1,105 @@
package com.lookout.jenkins;

import static org.junit.Assert.*;

import java.nio.charset.Charset;

import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;


import hudson.FilePath;
import hudson.matrix.Axis;
import hudson.matrix.AxisList;
import hudson.matrix.MatrixRun;
import hudson.matrix.DefaultMatrixExecutionStrategyImpl;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
import hudson.model.Result;
import hudson.model.TaskListener;
import hudson.util.StreamTaskListener;

public class EnvironmentScriptMultiMatrixTest {

public JenkinsRule jenkins = new JenkinsRule();

class MultiMatrixTestJob {
public MatrixProject project;
public CountBuilder countBuilder;
public MatrixBuild build;
public TaskListener listener;

public MultiMatrixTestJob(String script, boolean onlyRunOnParent) throws Exception {
listener = new StreamTaskListener(System.err, Charset.defaultCharset());
project = jenkins.createMatrixProject();

// This forces it to run the builds sequentially, to prevent any
// race conditions when concurrently updating the 'counter' file.
project.setExecutionStrategy(new DefaultMatrixExecutionStrategyImpl(true, null, null, null));

project.setAxes(new AxisList(new Axis("axis", "value1", "value2")));
project.getBuildWrappersList().add(new EnvironmentScript(script, scriptType, onlyRunOnParent, hideGeneratedValue));
countBuilder = new CountBuilder();
build = jenkins.buildAndAssertSuccess(project);

final static String SCRIPT_COUNTER =
+ "if [ -f $file ]; then\n"
+ " let i=$(cat $file)+1\n"
+ "else\n"
+ " i=1\n"
+ "fi\n"
+ "echo 1 >was_run\n"
+ "echo $i >$file\n"
+ "echo seen=yes";

// Generate a random directory that we pass to the shell script.
File tempDir = Files.createTempDir();
String script = String.format(SCRIPT_COUNTER, tempDir.getPath());
String scriptType = "unixScript";
boolean hideGeneratedValue = Boolean.TRUE;

public void testWithParentOnly() throws Exception {
MultiMatrixTestJob job = new MultiMatrixTestJob(script, true);

// We ensure that this was only run once (on the parent)
assertEquals("1", new FilePath(tempDir).child("counter").readToString().trim());

// Then make sure that it was in fact in the parent's WS that we ran.
for (MatrixRun run :

public void testWithEachChild() throws Exception {
MultiMatrixTestJob job = new MultiMatrixTestJob(script, false);

// We ensure that this was only run twice - once for each axis combination - but not on the parent.
assertEquals("2", new FilePath(tempDir).child("counter").readToString().trim());

// Then make sure that it was in fact in the combination jobs' workspace.
for (MatrixRun run :

private void buildAndAssert(MultiMatrixTestJob job) throws Exception {

// Make sure that the environment variables set in the script are properly propagated.
// Make sure that the builder was executed twice, once for each axis value.
assertEquals(2, job.countBuilder.getCount());
@@ -3,8 +3,6 @@
import static org.junit.Assert.*;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import hudson.EnvVars;
@@ -19,8 +17,6 @@
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.SingleFileSCM;

import com.kenai.jffi.Array;

public class EnvironmentScriptTest {


0 comments on commit a872dd6

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