Jenkins 工作流程 : Parallelize step based on tool output

标签 jenkins groovy jenkins-pipeline

我想根据测试工具的输出并行化 Jenkins 阶段。但是,我遇到了一个问题,因为所有并行节点都被同等定义(除了 currently broken 插件中的 循环)。 精简的工作流程脚本示例:

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

虽然我期望结果如下:

node {
  stage name: 'stage one'
  echo 'one'
},
node {
  stage name: 'stage two'
  echo 'two'
},
node {
  stage name: 'stage three'
  echo '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 example不访问任何变量。

最佳答案

尝试def instance_name =实例.get(i)

如果没有它,就好像您只有对 instance_name 的引用,在循环后最终会变成三个。这样它就是同一个对象..

结果:

[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

阅读变量作用域和绑定(bind)机制,即 herehere .

关于 Jenkins 工作流程 : Parallelize step based on tool output,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36631959/

相关文章:

groovy - 如何使用 native groovy 复制包含排除项的文件夹?

jenkins - 如何在 Jenkins 管道中执行托管脚本

docker - 如何在本地机器上查找docker镜像

jenkins - 正确使用 stash\unstash 到不同的目录

Jenkins 全局管道库目录结构

shell - Jenkins:如何在 Windows 从站中执行 shell 脚本

jenkins - 如何让 jenkins API 返回更多构建?

git clone 挂起并超时

groovy - Jenkins 使用 groovy 为作业添加权限

grails - g:formatDate与java.sql.Date类实例有关的问题