管道并行中的动态步骤未按预期工作

时间:2019-05-29 19:49:35

标签: jenkins-pipeline jenkins-declarative-pipeline

我尝试从集合中在Jenkins管道中动态创建并行步骤。

当我运行 sh 代码块而未分配变量时,一切正常。 当我将 sh 代码分配给变量,然后尝试执行该代码时-对于每个并行步骤代码,请使用collection中的最后一个元素。

CLUSTERS="192.168.0.1,192.168.0.2|127.0.0.1,127.0.0.2"

按预期工作:

stage("Run Repair") {
  environment { CLUSTERS = findClusters() }
  steps {
    script {
      def splitted_clusters = CLUSTERS.split('\\|')
      def stepsForParallel = splitted_clusters.collectEntries {
        echo "Non parallel ${it}"
        ["Repair ${it}" : {sh(script: """ echo "parallel: Found cluster with IPs: ${it}" """)}]
      }
      echo "Start parallel:"
      parallel stepsForParallel
    }
  }
}

输出:

Non parallel 192.168.0.1,192.168.0.2
Non parallel 127.0.0.1,127.0.0.2
Start parallel:
[Repair 192.168.0.1,192.168.0.2] Running shell script
[Repair 192.168.0.1,192.168.0.2] Found cluster with IPs: 192.168.0.1,192.168.0.2
[Repair 127.0.0.1,127.0.0.2] Running shell script
[Repair 127.0.0.1,127.0.0.2] Found cluster with IPs: 127.0.0.1,127.0.0.2

不起作用:

stage("Run Repair") {
  environment { CLUSTERS = findClusters() }
  steps {
    script {
      def splitted_clusters = CLUSTERS.split('\\|')
      def stepsForParallel = splitted_clusters.collectEntries {
        command = """ echo "Found cluster with IPs: ${it}" """
        echo "Non parallel ${it}"
        ["Repair ${it}" : {sh(script: command)}]
      }
      echo "Start parallel:"
      parallel stepsForParallel
    }
  }
}

错误的输出:

Non parallel 192.168.0.1,192.168.0.2
Non parallel 127.0.0.1,127.0.0.2
Start parallel:
[Repair 192.168.0.1,192.168.0.2] Running shell script
[Repair 192.168.0.1,192.168.0.2] Found cluster with IPs: 127.0.0.1,127.0.0.2
[Repair 127.0.0.1,127.0.0.2] Running shell script
[Repair 127.0.0.1,127.0.0.2] Found cluster with IPs: 127.0.0.1,127.0.0.2

那么,我试图了解这种行为背后隐藏着什么?

0 个答案:

没有答案