Jenkins工作流程:基于工具输出并行化步骤 [英] Jenkins Workflow: Parallelize step based on tool output

查看:686
本文介绍了Jenkins工作流程:基于工具输出并行化步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据测试工具的输出并行化Jenkins阶段。但是,我遇到了一个问题,因为所有并行节点的定义都是平等的(除了目前在 jenkins-工作流插件)。
修整了工作流脚本示例:



I want to parallelize Jenkins stages based on the output of a test tool. However, I am encountering a problem because all parallel nodes are getting defined equally (in addition to the currently broken loops in the jenkins-workflow plugins). Trimmed down workflow script example:

instances = ["one", "two", "three"]
print "Testing instances: " + instances

test_nodes = [:]
for (int i = 0; i < instances.size(); i++) {
  instance_name = instances.get(i)
  println "Processing instance " + instance_name
  test_nodes["tk-${instance_name}"] = {
    node {
      stage name: ('stage ' + instance_name)
      echo instance_name
    }
  }
}
echo "test_nodes: ${test_nodes}"
parallel test_nodes

虽然我期望得到如下结果:

While I would expect a result as follows:

node {
  stage name: 'stage one'
  echo 'one'
},
node {
  stage name: 'stage two'
  echo 'two'
},
node {
  stage name: 'stage three'
  echo 'three'
} 

我将所有三个节点定义为 3 - 尽可能(注意重复输出 3 ):

I get all three nodes defined as three - as it can be seen in the following output (notice the repeated output three):

[Pipeline] echo
Testing instances: [one, two, three]
[Pipeline] echo
Processing instance one
[Pipeline] echo
Processing instance two
[Pipeline] echo
Processing instance three
[Pipeline] echo
test_nodes: [tk-one:org.jenkinsci.plugins.workflow.cps.CpsClosure2@3febb2f8, tk-two:org.jenkinsci.plugins.workflow.cps.CpsClosure2@b32d891, tk-three:org.jenkinsci.plugins.workflow.cps.CpsClosure2@37281d55]
[Pipeline] Execute in parallel : Start
[Pipeline] [tk-one] parallel { (Branch: tk-one)
[Pipeline] [tk-two] parallel { (Branch: tk-two)
[Pipeline] [tk-three] parallel { (Branch: tk-three)
[Pipeline] [tk-one] Allocate node : Start
[tk-one] Running on master in /var/lib/jenkins/jobs/cookbook-pipeline-cookbook-site-mstypo3org/workspace
[Pipeline] [tk-two] Allocate node : Start
[tk-two] Running on master in /var/lib/jenkins/jobs/cookbook-pipeline-cookbook-site-mstypo3org/workspace@2
[Pipeline] [tk-three] Allocate node : Start
[tk-three] Running on master in /var/lib/jenkins/jobs/cookbook-pipeline-cookbook-site-mstypo3org/workspace@3
[Pipeline] [tk-one] node {
[Pipeline] [tk-two] node {
[Pipeline] [tk-three] node {
[Pipeline] [tk-one] echo
[tk-one] three
[Pipeline] } //node
[Pipeline] [tk-two] echo
[tk-two] three
[Pipeline] } //node
[Pipeline] [tk-three] echo
[tk-three] three
[Pipeline] } //node
[Pipeline] Allocate node : End
[Pipeline] Allocate node : End
[Pipeline] Allocate node : End
[Pipeline] } //parallel
[Pipeline] } //parallel
[Pipeline] } //parallel
[Pipeline] Execute in parallel : End
[Pipeline] End of Pipeline

为什么会发生这种情况?这是 workflow-cps 中的另一个错误,还是我出错了? parallel 示例不会访问任何变量。

Why is this happening? Is this another bug in workflow-cps, or am I getting something wrong? The parallel example does not access any variables.

推荐答案

尝试 def instance_name = instances.get(i )

如果没有它,就像你只有一个对 instance_name 的引用,它最终为。它的同一个对象就是这样。

Without it, its like you just have a references to the instance_name, which ends up as three after the loop. Its the same object that way..

结果:

Result:

[Pipeline] echo
Testing instances: [one, two, three]
[Pipeline] echo
Processing instance one
[Pipeline] echo
Processing instance two
[Pipeline] echo
Processing instance three
[Pipeline] echo
test_nodes: [tk-one:org.jenkinsci.plugins.workflow.cps.CpsClosure2@4cb28325, tk-two:org.jenkinsci.plugins.workflow.cps.CpsClosure2@5bc01979, tk-three:org.jenkinsci.plugins.workflow.cps.CpsClosure2@20c885fe]
[Pipeline] Execute in parallel : Start
[Pipeline] [tk-one] parallel { (Branch: tk-one)
[Pipeline] [tk-two] parallel { (Branch: tk-two)
[Pipeline] [tk-three] parallel { (Branch: tk-three)
[Pipeline] [tk-one] Allocate node : Start
[tk-one] Running on master in /var/lib/jenkins/jobs/pipeline/workspace
[Pipeline] [tk-two] Allocate node : Start
[tk-two] Running on master in /var/lib/jenkins/jobs/pipeline/workspace@2
[Pipeline] [tk-three] Allocate node : Start
[Pipeline] [tk-one] node {
[Pipeline] [tk-two] node {
[tk-three] Running on master in /var/lib/jenkins/jobs/pipeline/workspace
[Pipeline] [tk-one] stage (stage one)
[tk-one] Entering stage stage one
[tk-one] Proceeding
[Pipeline] [tk-one] echo
[tk-one] one
[Pipeline] } //node
[Pipeline] [tk-two] stage (stage two)
[tk-two] Entering stage stage two
[tk-two] Proceeding
[Pipeline] [tk-two] echo
[tk-two] two
[Pipeline] } //node
[Pipeline] Allocate node : End
[Pipeline] [tk-three] node {
[Pipeline] Allocate node : End
[Pipeline] } //parallel
[Pipeline] } //parallel
[Pipeline] [tk-three] stage (stage three)
[tk-three] Entering stage stage three
[tk-three] Proceeding
[Pipeline] [tk-three] echo
[tk-three] three
[Pipeline] } //node
[Pipeline] Allocate node : End
[Pipeline] } //parallel
[Pipeline] Execute in parallel : End
[Pipeline] End of Pipeline
Finished: SUCCESS

阅读变量范围和绑定机制,即这里这里

Read up on variable scope and binding mechanics i.e. here and here.

这篇关于Jenkins工作流程:基于工具输出并行化步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆