Skip to content

Commit

Permalink
[FIXED JENKINS-16334] Add current node parameter
Browse files Browse the repository at this point in the history
Adds a current node parameter which forces the downstream builds
to build on the same node as the triggering job.
Will override any tie this job to node settings of the downstream job.
Adds a build badge to indicate that a fixed node was used.
  • Loading branch information
cjo9900 committed Jan 16, 2013
1 parent 70aa20b commit 1c15f7b
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/main/java/hudson/plugins/parameterizedtrigger/NodeAction.java
@@ -0,0 +1,70 @@
/*
* The MIT License
*
* Copyright (c) 2013, Chris Johnson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.plugins.parameterizedtrigger;

import hudson.Util;
import hudson.model.Action;
import hudson.model.BuildBadgeAction;
import hudson.model.InvisibleAction;
import hudson.model.Label;
import hudson.model.Queue;
import hudson.model.labels.LabelAssignmentAction;
import hudson.model.queue.SubTask;
import java.util.List;

/**
* {@link Action} that restricts the job to a particular node
* when a project is scheduled
* Will cause a unique build for each different node if a job is already queued.
*
* @author Chris Johnson
*/
public class NodeAction extends InvisibleAction implements LabelAssignmentAction, Queue.QueueAction, BuildBadgeAction {
private final String nodename;

public NodeAction(String nodename) {
this.nodename = nodename;
}

public Label getAssignedLabel(SubTask task) {
return Label.get(nodename);
}

public boolean shouldSchedule(List<Action> actions) {
// see if there is already a matching action with same node
for (NodeAction other:Util.filter(actions, NodeAction.class)) {
if(this.nodename.equals(other.nodename)){
// there is already a task for this node.
return false;
}
}
return true;
}
/**
* @return the tooltip
*/
public String getTooltip() {
return nodename;
}
}
@@ -0,0 +1,43 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hudson.plugins.parameterizedtrigger;

import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.Action;
import hudson.model.Descriptor;
import hudson.model.TaskListener;
import java.io.IOException;
import org.kohsuke.stapler.DataBoundConstructor;

/**
*
* @author Chris johnson
*/
public class NodeParameters extends AbstractBuildParameters{

@DataBoundConstructor
public NodeParameters() {
}

@Override
public Action getAction(AbstractBuild<?, ?> build, TaskListener listener) throws IOException, InterruptedException, DontTriggerException {
String nodename = build.getBuiltOnStr();
// master does not return a node name so add it explicitly.
if(nodename == "") {
nodename = "master";
}
listener.getLogger().println("current node is " + nodename);
return new NodeAction(nodename);
}

@Extension
public static class DescriptorImpl extends Descriptor<AbstractBuildParameters> {
@Override
public String getDisplayName() {
return "Build on the same node";
}
}
}
@@ -0,0 +1,5 @@
<j:jelly xmlns:j="jelly:core">
<img width="16" height="16"
title="${it.tooltip}"
src="${imagesURL}/16x16/computer.png"/>
</j:jelly>
@@ -0,0 +1,3 @@
<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">
<f:entry field="current" />
</j:jelly>
@@ -0,0 +1,4 @@
<div>
Use the same Node for the triggered builds that was used for this build.<br/>
Note: Overrides the Node that the triggered project is tied to.
</div>
@@ -0,0 +1,134 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hudson.plugins.parameterizedtrigger.test;

import hudson.model.AbstractBuild;
import hudson.model.Cause;
import hudson.model.ParametersAction;
import hudson.model.Project;
import hudson.model.StringParameterValue;
import hudson.plugins.parameterizedtrigger.BuildTrigger;
import hudson.plugins.parameterizedtrigger.BuildTriggerConfig;
import hudson.plugins.parameterizedtrigger.NodeParameters;
import hudson.plugins.parameterizedtrigger.ResultCondition;
import hudson.slaves.DumbSlave;
import org.jvnet.hudson.test.CaptureEnvironmentBuilder;
import org.jvnet.hudson.test.HudsonTestCase;

/**
*
* @author cjohnson
*/
public class NodeParametersTest extends HudsonTestCase {

public void test() throws Exception {

DumbSlave slave0 = createOnlineSlave();
DumbSlave slave1 = createOnlineSlave();

Project<?,?> projectA = createFreeStyleProject("projectA");
projectA.getPublishersList().add(
new BuildTrigger(new BuildTriggerConfig("projectB", ResultCondition.SUCCESS, new NodeParameters())));

projectA.setAssignedNode(slave0);

CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
Project projectB = createFreeStyleProject("projectB");
projectB.getBuildersList().add(builder);
projectB.setQuietPeriod(1);
// set to build on slave1
projectB.setAssignedNode(slave1);
hudson.rebuildDependencyGraph();

AbstractBuild buildA = projectA.scheduleBuild2(0).get();
waitUntilNoActivity();
// hudson.getQueue().getItem(projectB).getFuture().get();

assertEquals(slave0, buildA.getBuiltOn());
assertNotNull("builder should record environment", builder.getEnvVars());
// ProjectB will be built on slave 0 regardless of assigned node.
assertEquals("slave0", builder.getEnvVars().get("NODE_NAME"));

}

public void testQueuedJobsCombined() throws Exception {

DumbSlave slave0 = createOnlineSlave();
DumbSlave slave1 = createOnlineSlave();

Project<?,?> projectA = createFreeStyleProject("projectA");
projectA.getPublishersList().add(
new BuildTrigger(new BuildTriggerConfig("projectB", ResultCondition.SUCCESS, new NodeParameters())));

projectA.setAssignedNode(slave0);

CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
Project projectB = createFreeStyleProject("projectB");
projectB.getBuildersList().add(builder);
projectB.setQuietPeriod(10);
// set to build on slave1
projectB.setAssignedNode(slave1);
hudson.rebuildDependencyGraph();

AbstractBuild buildA = projectA.scheduleBuild2(0).get();
AbstractBuild buildA2 = projectA.scheduleBuild2(0).get();
waitUntilNoActivity();
// hudson.getQueue().getItem(projectB).getFuture().get();
assertEquals(2, projectA.getBuilds().size());

assertEquals(slave0, buildA.getBuiltOn());
assertEquals(slave0, buildA2.getBuiltOn());
assertNotNull("builder should record environment", builder.getEnvVars());
// ProjectB will be built on slave 0 regardless of assigned node.
assertEquals("slave0", builder.getEnvVars().get("NODE_NAME"));
// should only be a single build of projectB
assertEquals(1, projectB.getBuilds().size());
}

public void testQueuedJobsNotCombined() throws Exception {

DumbSlave slave0 = createOnlineSlave();
DumbSlave slave1 = createOnlineSlave();
DumbSlave slave2 = createOnlineSlave();

Project<?,?> projectA = createFreeStyleProject("projectA");
projectA.getPublishersList().add(
new BuildTrigger(new BuildTriggerConfig("projectB", ResultCondition.SUCCESS, new NodeParameters())));

projectA.setAssignedNode(slave0);

CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
Project projectB = createFreeStyleProject("projectB");

int firstBuildNumber = projectB.getNextBuildNumber();
projectB.getBuildersList().add(builder);
projectB.setQuietPeriod(5);
// set to build on slave1
projectB.setAssignedNode(slave1);
hudson.rebuildDependencyGraph();

AbstractBuild buildA = projectA.scheduleBuild2(0).get();
// Now trigger on another slave
projectA.setAssignedNode(slave2);
AbstractBuild buildA2 = projectA.scheduleBuild2(0).get();
waitUntilNoActivity();

assertEquals(slave0, buildA.getBuiltOn());
assertEquals(slave2, buildA2.getBuiltOn());

// should have two builds of projectB
assertEquals(2, projectB.getBuilds().size());

AbstractBuild buildB = (AbstractBuild)projectB.getBuildByNumber(firstBuildNumber);
assertNotNull("ProjectB failed to build", buildB);
assertEquals(slave0, buildB.getBuiltOn());

// get the second build of projectB
AbstractBuild buildB2 = (AbstractBuild)buildB.getNextBuild();
assertNotNull("ProjectB failed to build second time", buildB2);
assertEquals(slave2, buildB2.getBuiltOn());

}
}

0 comments on commit 1c15f7b

Please sign in to comment.