Skip to content
Browse files
Merge pull request #5 from ssogabe/JENKINS-9260.
Jenkins 9260 promote plugin should provide ability to select slave node to run
  • Loading branch information
ssogabe committed May 4, 2011
2 parents 47e71b8 + 0250946 commit 2abeca2ff733b3a29227fddc433912570ef691e4
@@ -1,10 +1,12 @@
package hudson.plugins.promoted_builds;

import antlr.ANTLRException;
import hudson.Extension;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.AutoCompletionCandidates;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.model.Failure;
@@ -15,6 +17,7 @@
import hudson.model.Job;
import hudson.model.JobProperty;
import hudson.model.JobPropertyDescriptor;
import hudson.model.Label;
import hudson.tasks.BuildStep;
import hudson.util.FormValidation;
import net.sf.json.JSONArray;
@@ -29,6 +32,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import org.kohsuke.stapler.QueryParameter;

@@ -326,6 +330,78 @@ public FormValidation doCheckName(@QueryParameter String name) {

return FormValidation.ok();

public FormValidation doCheckLabelString(@QueryParameter String value) {
if (Util.fixEmpty(value)==null)
return FormValidation.ok(); // nothing typed yet
try {
} catch (ANTLRException e) {
return FormValidation.error(e,
// TODO: if there's an atom in the expression that is empty, report it
if (Hudson.getInstance().getLabel(value).isEmpty())
return FormValidation.warning(Messages.JobPropertyImpl_LabelString_NoMatch());
return FormValidation.ok();

public AutoCompletionCandidates doAutoCompleteAssignedLabelString(@QueryParameter String value) {
AutoCompletionCandidates c = new AutoCompletionCandidates();
Set<Label> labels = Hudson.getInstance().getLabels();
List<String> queries = new AutoCompleteSeeder(value).getSeeds();

for (String term : queries) {
for (Label l : labels) {
if (l.getName().startsWith(term)) {
return c;

* Utility class for taking the current input value and computing a list
* of potential terms to match against the list of defined labels.
static class AutoCompleteSeeder {

private String source;
private Pattern quoteMatcher = Pattern.compile("(\\\"?)(.+?)(\\\"?+)(\\s*)");

AutoCompleteSeeder(String source) {
this.source = source;

List<String> getSeeds() {
ArrayList<String> terms = new ArrayList();
boolean trailingQuote = source.endsWith("\"");
boolean leadingQuote = source.startsWith("\"");
boolean trailingSpace = source.endsWith(" ");

if (trailingQuote || (trailingSpace && !leadingQuote)) {
} else {
if (leadingQuote) {
int quote = source.lastIndexOf('"');
if (quote == 0) {
} else {
} else {
int space = source.lastIndexOf(' ');
if (space > -1) {
terms.add(source.substring(space + 1));
} else {

return terms;
@@ -1,5 +1,7 @@
package hudson.plugins.promoted_builds;

import antlr.ANTLRException;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
@@ -17,6 +19,8 @@
import hudson.model.Queue.Item;
import hudson.model.Run;
import hudson.model.Saveable;
import hudson.model.labels.LabelAtom;
import hudson.model.labels.LabelExpression;
import hudson.tasks.BuildStep;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
@@ -48,7 +52,12 @@ public final class PromotionProcess extends AbstractProject<PromotionProcess,Pro
* and ${rootURL}/plugin/promoted-builds/icons/32x32/, e.g. <code>"star-gold"</code>.
public String icon;

* The label that promotion process can be run on.
public String assignedLabel;

private List<BuildStep> buildSteps = new ArrayList<BuildStep>();

/*package*/ PromotionProcess(JobPropertyImpl property, String name) {
@@ -62,6 +71,12 @@ public final class PromotionProcess extends AbstractProject<PromotionProcess,Pro
buildSteps = (List)Descriptor.newInstancesFromHeteroList(
req, c, "buildStep", (List) PromotionProcess.getAll());
icon = c.getString("icon");
if (c.has("hasAssignedLabel")) {
JSONObject j = c.getJSONObject("hasAssignedLabel");
assignedLabel = Util.fixEmptyAndTrim(j.getString("labelString"));
} else {
assignedLabel = null;

@@ -100,7 +115,7 @@ public PromotionCondition getPromotionCondition(String promotionClassName) {

return null;

public DescribableList<Publisher, Descriptor<Publisher>> getPublishersList() {
// TODO: extract from the buildsSteps field? Or should I separate builders and publishers?
return new DescribableList<Publisher,Descriptor<Publisher>>(this);
@@ -114,10 +129,27 @@ public List<BuildStep> getBuildSteps() {
return buildSteps;

* Gets the textual representation of the assigned label as it was entered by the user.
public String getAssignedLabelString() {
if (assignedLabel == null) return null;
try {
return assignedLabel;
} catch (ANTLRException e) {
// must be old label or host name that includes whitespace or other unsafe chars
return LabelAtom.escape(assignedLabel);

@Override public Label getAssignedLabel() {
// Really would like to run on the exact node that the promoted build ran on,
// not just the same label.. but at least this works if job is tied to one node:
return getOwner().getAssignedLabel();
if (assignedLabel == null) return getOwner().getAssignedLabel();

return Hudson.getInstance().getLabel(assignedLabel);

@Override public JDK getJDK() {
@@ -18,6 +18,15 @@
<f:option selected="${s.icon=='star-red'}" value="star-red">Red star</f:option>

<f:optionalBlock name="hasAssignedLabel" title="${%Restrict where this promotiom process can be run}"
<f:entry title="${%Label Expression}" field="labelString"
description="If not set, the label of the promoted build will be used.">
<f:textbox autoCompleteDelimChar=" " value="${s.assignedLabelString}" />

<f:section title="Criteria">
<table style="width:100%">
@@ -0,0 +1,53 @@
If you want to always run this promotion process on a specific node/slave, just specify its name.
If not specified, the same label which the promoted build used can be used.
This works well when you have a small number of nodes.

As the size of the cluster grows, it becomes useful not to tie projects to specific slaves,
as it hurts resource utilization when slaves may come and go. For such situation, assign labels
to slaves to classify their capabilities and characteristics, and specify a boolean expression
over those labels to decide where to run.

<h3>Valid Operators</h3>
The following operators are supported, in the order of precedence.




<dt>a -> b</dt>
"implies" operator. Equivalent to <tt>!a|b</tt>.
For example, <tt>windows->x64</tt> could be thought of as "if run on a Windows slave,
that slave must be 64bit." It still allows Jenkins to run this build on linux.

<dt>a &lt;-> b</dt>
"if and only if" operator. Equivalent to <tt>a&amp;&amp;b || !a&amp;&amp;!b</tt>.
For example, <tt>windows->sfbay</tt> could be thought of as "if run on a Windows slave,
that slave must be in the SF bay area, but if not on Windows, it must not be in the bay area."
All operators are left-associative (i.e., a->b->c &lt;-> (a->b)->c )
An expression can contain whitespace for better readability, and it'll be ignored.

Label names or slave names can be quoted if they contain unsafe characters. For example,
<tt>"jenkins-solaris (Solaris)" || "Windows 2008"</tt>
@@ -22,7 +22,12 @@

PromotionProcess.PermalinkDisplayName=Latest promotion:{0}
Invalid boolean expression: {0}
There's no slave/cloud that matches this assignment
KeepBuildForEverAction.descriptor.displayName=Keep Build Forever
KeepBuildForEverAction.console.notPromotion=This build is not a promotion, how did we get here? Not keeping build.
KeepBuildForEverAction.console.promotionNotGoodEnough=Promotion build result [{0}] is not good enough. Not keeping build.
KeepBuildForEverAction.console.keepingBuild=Marking build to keep forever.

0 comments on commit 2abeca2

Please sign in to comment.