0

I basically want to drive a set of choice parameters based on the slave node parameter choice. I tried to use the active choices reactive plugin to point to the node choice like this:

if (Node.contains("name_of_slave_node")) {
  return ["you_chose_slave_node"]
} else {
  return ["master"]
}

Nothing I do seems to work. I can use this type of logic and point to any other type of parameter and it works. Im just a bit stumped as to where to go with this. Could it be a limitation with the plugin or am I missing something with how the groovy is addressing the node parameter. I really appreciate any advice.

2 Answers2

0

It can be hard to get your head around how the plug-in works. Even now I still get surprised by all the cases users apply the plug-in, and the different ways they use it.

You can definitely do what you want. The plug-in executes a Groovy script that must return an array with the values to be displayed on the UI. You also have some helper variables that the plug-in tries to make available for you. One of these variables is the jenkinsProject.

If in your project you assigned a label, then programmatically you can return values based on the node label assigned to your project (or to the node object...).

Here's a very simple example.

assignedNode = jenkinsProject.getAssignedLabelString()
list = []
if (assignedNode == null) {
    // do something
} else if (assignedNode.equals('abc')) {
    // ...
}
return list

Here assignedNode will have the string name of the node, or null if none. If you use instead jenkinsProject.getAssignedLabel(), then you will have not a String, but a Label.

If you need further customization, the best way is to dive into the Java API, then build your Groovy script from the bottom up. Or try finding examples online (there are many for Jenkins and Groovy) and adapt them to your parameter.

Hope that helps, Bruno

0

Working freestyle job solution


I managed to cobble something together yesterday that accomplishes what you want in Jenkin's freestyle jobs. For reference though, it seems like what we want to accomplish is easier done with Jenkin's pipelines (see Active Choices Reactive Reference Parameter in jenkins pipeline)

First, know that the Active choices reactive plugin does not detect any changes in the NodeLabel node parameter choice. You are not going to be able to dynamicially adjust your reactive parameters based on the node/label parameter choice. (perhaps this is because the Node parameter simply prepares a separate job for each node chosen and in that way the Node parameter is more like a build time choice instead of a dynamic one) But I have a workaround.

On the NodeLabel documentation, they specify that the plugin works with the Parameterized Trigger Plugin, see Using the Parameterized Trigger Plugin. This is going to be key.


  1. We will make two new freestyle items. The first will be holding on to your nodes in an active choice parameter, and whatever else you want, then the second job will be a copy of the first one but it will have an additional node parameter alongside the others. Like:
job list of parameters
master - active choice parameter (node)
- foo
- bar
copy - NodeLabel parameter
- active choice parameter (node)
- foo
- bar

This real NodeLabel parameter will take in the active choice parameter from the master job enter image description here to the end user, this node standin active parameter on the master job will react and act just like any other parameter. The node choice in the first box changes the results in the second box. In the configurtation of the master job we then pass our parameters like such, using the "parameterized trigger plugin" from above. enter image description here

  1. We pass our current parameters because the copy/scoped job is going to be doing the real leg work here and will need all our parameters.
  2. Then under "parameter factories" we add a all nodes for label factory and pass the token-expanded form of our active choice parameter's value that represented the node the user chose.
  3. Finally, in our scoped/copy project, we have our node parameter. This parameter's name needs to match the name of node label factory in the previous step (ie. node_list)

enter image description here Just make sure the scoped/copy job is doing the real work and you should be good to go. The master/wrap job's purpose is only to act as a nice visual wrapper to the node parameter and its subsequent choices that takes advantage of the Active choice reactive parameter plugin's capabilities. Finally, it passes its results and responsibilities to the scoped/copy job.

robwill
  • 81
  • 1
  • 4