Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JENKINS-10088 add new paramtype to param-triggerpl
- Loading branch information
Showing
5 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...vnet/jenkins/plugins/nodelabelparameter/parameterizedtrigger/NodeLabelBuildParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...kins/plugins/nodelabelparameter/parameterizedtrigger/NodeLabelBuildParameter/config.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
5 changes: 5 additions & 0 deletions
5
...ns/plugins/nodelabelparameter/parameterizedtrigger/NodeLabelBuildParameter/help-name.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
85 changes: 85 additions & 0 deletions
85
.../jenkins/plugins/nodelabelparameter/parameterizedtrigger/NodeLabelBuildParameterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} | ||
} |