Skip to content
Permalink
Browse files

Merge pull request #72 from jimklimov/buildparams

Pass build arguments into evaluated groovy script
  • Loading branch information...
abayer committed Sep 18, 2017
2 parents 3f03365 + bcc9242 commit 3a9e6496456e9e82dbc4dd6cd74202ec0c639526
@@ -19,6 +19,8 @@
import hudson.model.Queue;
import hudson.model.queue.QueueTaskDispatcher;
import hudson.model.queue.CauseOfBlockage;
import hudson.model.ParametersAction;
import hudson.model.ParameterValue;

import java.util.Date;
import java.util.HashMap;
@@ -72,11 +74,37 @@ public CauseOfBlockage canRun(Queue.Item item) {

if (resourceNumber > 0 || !resources.label.isEmpty() || resources.getResourceMatchScript() != null) {
Map<String, Object> params = new HashMap<String, Object>();

// Inject Build Parameters, if possible and applicable to the "item" type
try {
List<ParametersAction> itemparams = item.getActions(ParametersAction.class);
if (itemparams != null) {
for ( ParametersAction actparam : itemparams) {
if (actparam == null) continue;
for ( ParameterValue p : actparam.getParameters() ) {
if (p == null) continue;
params.put(p.getName(), p.getValue());
}
}
}
} catch(Exception ex) {
// Report the error and go on with the build -
// perhaps this item is not a build with args, etc.
// Note this is likely to fail a bit later in such case.
if (LOGGER.isLoggable(Level.WARNING)) {
if (lastLogged.getIfPresent(item.getId()) == null) {
lastLogged.put(item.getId(), new Date());
String itemName = project.getFullName() + " (id=" + item.getId() + ")";
LOGGER.log(Level.WARNING, "Failed to get build params from item " + itemName, ex);
}
}
}

if (item.task instanceof MatrixConfiguration) {
MatrixConfiguration matrix = (MatrixConfiguration) item.task;
params.putAll(matrix.getCombination());
MatrixConfiguration matrix = (MatrixConfiguration) item.task;
params.putAll(matrix.getCombination());
}

final List<LockableResource> selected ;
try {
selected = LockableResourcesManager.get().tryQueue(
@@ -2,7 +2,8 @@
<p>
You can specify a groovy expression to be evaluated each time a resource is checked
to be appropriate for a build. The expression must result into a boolean value. The
following variables are available:
following variables are available, in addition to optional arguments of the currently
evaluated build:
</p>
<dl>
<dt>resourceName</dt>
@@ -20,7 +21,7 @@
<li><code>resourceLabels.contains(axisName)</code></li>
<li><code>resourceName == axisName</code></li>
</ul>

<p>
The script's contents need to pass approval by the <a href="https://plugins.jenkins.io/script-security">Script Security Plugin</a>.
</p>

0 comments on commit 3a9e649

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