Skip to content

Commit

Permalink
JENKINS-10088 add new paramtype to param-triggerpl
Browse files Browse the repository at this point in the history
  • Loading branch information
imod committed Jul 10, 2011
1 parent 2a92600 commit f9a09c2
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Expand Up @@ -27,4 +27,12 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>parameterized-trigger</artifactId>
<version>2.8</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,70 @@
package org.jvnet.jenkins.plugins.nodelabelparameter.parameterizedtrigger;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.Action;
import hudson.model.TaskListener;
import hudson.model.AbstractBuild;
import hudson.model.Descriptor;
import hudson.model.ParametersAction;
import hudson.plugins.parameterizedtrigger.AbstractBuildParameters;

import java.io.IOException;

import org.jvnet.jenkins.plugins.nodelabelparameter.LabelParameterValue;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* As this plugin is build against Jenkins1.398 and dynamic nodelabel assignment
* was only introduced with Jenkins1.417, this extension is marked as optional!
*
* @author domi
*
*/
public class NodeLabelBuildParameter extends AbstractBuildParameters {

public final String name;
public final String nodeLabel;

@DataBoundConstructor
public NodeLabelBuildParameter(String name, String nodeLabel) {
this.name = name;
this.nodeLabel = nodeLabel;
}

public Action getAction(AbstractBuild<?, ?> build, TaskListener listener)
throws IOException, InterruptedException {
EnvVars env = build.getEnvironment(listener);
String labelExpanded = env.expand(nodeLabel);

LabelParameterValue parameterValue = new LabelParameterValue(name,
labelExpanded);
listener.getLogger().println("define: " + parameterValue);

return new ParametersAction(parameterValue);
}

@Extension(optional = true)
public static class DescriptorImpl extends
Descriptor<AbstractBuildParameters> {

// force loading of dependent class to disable extension early
// static {
// try {
// @SuppressWarnings("unused")
// Class<?> c = LabelParameterValue.class;
// } catch (Throwable e) {
// throw new NoClassDefFoundError(
// "'LabelParameterValue' - nodelabelparameter-plugin not installed, disable NodeLabelBuildParameter for parameterized-trigger-plugin");
// }
// }



@Override
public String getDisplayName() {
return "NodeLabel parameter";
}
}

}
@@ -0,0 +1,10 @@
<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="name" title="Name">
<f:textbox />
</f:entry>
<f:entry field="nodeLabel" title="Node">
<f:textbox />
</f:entry>

</j:jelly>
@@ -0,0 +1,5 @@
<div>
This parameter type defines where the target job should be executed, the value must match either a label or a node name - otherwise the job will just stay in the queue.
The NodeLabel parameter passed to the target job, does not have to exist on the target job (but if the target has one defined, it should match the name).
This way it is possible to trigger jobs on different nodes then they are actually configured.
</div>
@@ -0,0 +1,85 @@
/*
* The MIT License
*
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
*
* 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 org.jvnet.jenkins.plugins.nodelabelparameter.parameterizedtrigger;

import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Project;
import hudson.model.labels.LabelAtom;
import hudson.plugins.parameterizedtrigger.BuildTrigger;
import hudson.plugins.parameterizedtrigger.BuildTriggerConfig;
import hudson.plugins.parameterizedtrigger.ResultCondition;

import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.jenkins.plugins.nodelabelparameter.LabelParameterDefinition;

public class NodeLabelBuildParameterTest extends HudsonTestCase {

/**
* Tests whether a job A is able to trigger job B to be executed on a
* specific node/slave. If it does not work, the timeout will stop/fail the
* test after 60 seconds.
*
* @throws Exception
*/
public void test() throws Exception {

final String paramName = "node";
final String nodeName = "someNode";

// create a slave with a given label to execute projectB on
createOnlineSlave(new LabelAtom(nodeName));

// create projectA, which triggers projectB with a given label parameter
Project<?, ?> projectA = createFreeStyleProject("projectA");
projectA.getPublishersList().add(
new BuildTrigger(new BuildTriggerConfig("projectB",
ResultCondition.SUCCESS, new NodeLabelBuildParameter(
paramName, nodeName))));

// create projectB, with a predefined parameter (same name as used in
// projectA!)
FreeStyleProject projectB = createFreeStyleProject("projectB");
ParametersDefinitionProperty pdp = new ParametersDefinitionProperty(
new LabelParameterDefinition(paramName, "some desc",
"wrongNodeName"));
projectB.addProperty(pdp);
// CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
// projectB.getBuildersList().add(builder);
projectB.setQuietPeriod(1);
hudson.rebuildDependencyGraph();

// projectA should trigger projectB just after execution, therefore we
// never trigger projectB explicitly
projectA.scheduleBuild2(0).get();
hudson.getQueue().getItem(projectB).getFuture().get();

FreeStyleBuild build = projectB.getLastCompletedBuild();
String foundNodeName = build.getBuildVariables().get(paramName);
assertNotNull("project should run on a specific node", foundNodeName);
assertEquals(nodeName, foundNodeName);

}
}

0 comments on commit f9a09c2

Please sign in to comment.