【问题标题】:How to access all nodes from NodeParameterDefinition in jenkins pipeline?如何从 jenkins 管道中的 NodeParameterDefinition 访问所有节点?
【发布时间】:2019-05-23 15:36:37
【问题描述】:

我正在为 jenkins 编写一个使用 NodeLabel Parameter Plugin 的 Jenkinsfile。在这里,我使用 NodeParameterDefinition 让用户能够选择应该进行构建的节点。我启用了allowMultiSelectionForConcurrentBuilds,但是在访问Jenkinsfile 中的参数值时,我仍然只得到一个只有一个节点名称的字符串。参数值类型也是字符串,如何获取用户为参数选择的所有节点?

参数定义:

 [
     $class: 'NodeParameterDefinition',
     allowedSlaves: ['ALL (no restriction)'],
     defaultSlaves: ['master'],
     description: 'What nodes to run the build on.',
     name: 'BUILD_NODE',
     nodeEligibility: [$class: 'AllNodeEligibility'],
     triggerIfResult: 'allowMultiSelectionForConcurrentBuilds'
 ]

所以如果我在执行的时候选择了多个节点,在访问这个参数值的时候还是只能得到一个节点名。

echo "Will build on $BUILD_NODE";

管道脚本无法启用多节点选择吗?

我如何访问参数值:

echo "Will build on $BUILD_NODE";
node("$BUILD_NODE")
{
   ...
}

【问题讨论】:

  • 进度:我发现我需要以“params.”的形式访问参数。所以我需要使用 params.BUILD_NODE 来获取正确的对象作为 env.BUILD_NODE 和 BUILD_NODE 只是一个字符串表示。但是现在我有一个新问题,那就是参数是一个空对象。

标签: jenkins jenkins-plugins jenkins-pipeline


【解决方案1】:

NodeLabel Parameter Plugin 不能与 Pipeline 和 Blue Ocean 一起顺利运行,因为它不经常更新(请参阅 revision history)。 Jenkins 插件必须遵循 requirements 才能与 Pipeline 兼容。

很遗憾,问题仍未解决(未知何时解决): https://issues.jenkins-ci.org/browse/JENKINS-43720

问题是我无法使用 env.NODE_PARAM 或 NODE_PARAM 来获取 多个节点选择,因为这些只是字符串表示 单个节点。

您可以为这个 jira-task JENKINS-43720 投票(点击“投票给这个问题”),或参与插件开发。

到目前为止,我发现了通过使用另一个参数选项 choice 来模仿插件行为的笨拙方法(但这在 Blue Ocean 中有效!):

properties([
    parameters([
    choice(choices: ["none", "node_1", "node_2"], description: "", name: "NODE_1"),
    choice(choices: ["none", "node_1", "node_2"], description: "", name: "NODE_2")
    ])
])
// here you can write your behaviour 
// e.g. validation of params, e.g. if 'none' is selected, then use the default node_X
node(env.NODE_1) { }
node(env.NODE_2) { } 

或者你可以使用选项string

properties([
    parameters([
       string(defaultValue: "node_1, node_2", description: "", name: "NODE", trim: false)
    ])
])
// parse here the param env.NODE 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2017-01-19
    • 2018-02-09
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多