简体   繁体   English

如何从 Jenkins 的活动选择反应参数中引用节点参数

[英]How to reference the node paramater from the active choices reactive parameter in Jenkins

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.这可能是插件的限制,还是我遗漏了 groovy 如何解决节点参数的问题。 I really appreciate any advice.我真的很感激任何建议。

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. 该插件执行Groovy脚本,该脚本必须返回一个数组,其中包含要在UI上显示的值。 You also have some helper variables that the plug-in tries to make available for you. 您还可以使用一些帮助程序变量,插件会尝试将这些变量提供给您。 One of these variables is the jenkinsProject . 这些变量之一是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. 在这里, signedNode将具有节点的字符串名称;如果没有,则为null。 If you use instead jenkinsProject.getAssignedLabel() , then you will have not a String, but a Label . 如果改为使用jenkinsProject.getAssignedLabel() ,则您将没有String,而是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. 如果需要进一步的自定义,最好的方法是深入Java API,然后从下至上构建Groovy脚本。 Or try finding examples online (there are many for Jenkins and Groovy) and adapt them to your parameter. 或尝试在线查找示例(Jenkins和Groovy有很多示例),并根据您的参数进行调整。

Hope that helps, Bruno 希望能有所帮助,布鲁诺

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 )不过作为参考,我们想要完成的工作似乎更容易使用 Jenkin 的管道完成(请参阅jenkins 管道中的 Active Choices Reactive Reference Parameter

First, know that the Active choices reactive plugin does not detect any changes in the NodeLabel node parameter choice.首先,要知道 Active choice 反应式插件不会检测 NodeLabel 节点参数选择中的任何更改。 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. (也许这是因为 Node 参数只是为每个选择的节点准备了一个单独的作业,这样 Node 参数更像是一个构建时间选择而不是动态选择)但我有一个解决方法。

On the NodeLabel documentation, they specify that the plugin works with the Parameterized Trigger Plugin, see Using the Parameterized Trigger Plugin .在 NodeLabel 文档中,他们指定插件与参数化触发器插件一起使用,请参阅使用参数化触发器插件 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这个真正的 NodeLabel 参数将从主作业中获取活动选择参数在此处输入图片说明 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.在主作业的配置中,我们然后像这样传递我们的参数,使用上面的“参数化触发器插件”。 在此处输入图片说明

  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)该参数的名称需要与上一步中节点标签工厂的名称匹配(即node_list)

在此处输入图片说明 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.最后,它将其结果和职责传递给作用域/复制作业。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jenkins 管道中的主动选择反应参考参数 - Active Choices Reactive Reference Parameter in jenkins pipeline Jenkins 声明式管道:如何从“主动选择反应引用参数的 Groovy 脚本”调用函数? - Jenkins Declarative Pipeline: How to call functions from "Active choices reactive reference parameter's Groovy script"? 如何在 Jenkins 中为 Active Choices Reactive Parameter 插件设置默认值? - How to set default value for Active Choices Reactive Parameter plugin in Jenkins? Jenkins Groovy 和 Active Choices 反应参数 - Jenkins Groovy and Active Choices Reactive Parameter Jenkins Active Choices Reactive Parameter中的OutputStreamWriter替代方法 - OutputStreamWriter alternative in Jenkins Active Choices Reactive Parameter Jenkins Active Choices反应参考参数不适用于格式化的HTML输入文本框 - Jenkins Active Choices Reactive Reference Parameter does not work with Formatted HTML Input-Text-Box Jenkins - 如何处理主动选择反应参考参数中的复选框 - Jenkins - How to handle checkbox in Active choice reactive reference parameter 从父作业设置参考参数时,不会选择活动选择反应参数 - Active Choices Reactive Parameter does not gets selected when the Reference parameter is set from a parent job 查询 AWS CLI 以填充 Jenkins “Active Choices Reactive Parameter” (Linux) - Query AWS CLI to populate Jenkins “Active Choices Reactive Parameter” (Linux) Jenkins - 主动选择反应参考参数 - Jenkins - Active choice reactive reference parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM