我想通过Jenkins groovy管道寻求帮助,该管道从此处复制: Is it possible to create parallel Jenkins Declarative Pipeline stages in a loop?
我希望在映射下将几个个变量集传递给,在并行运行下几个阶段。但是,只有最后一组(地图底部的方括号)被注册为我的地图。
并行阶段运行时,映射成功进行迭代,但仅使用最后一组(当前为install_Stage(it)
)进行迭代,而忽略其他组。意味着我得到了一个并行显示四个"stage: install ${product}"
阶段的管道,仅此而已。根据以下代码,我希望获得四个阶段(网络设置,还原和安装)的三个并行部分:
#!groovy
@Library('ci_builds')
def products = ["A", "B", "C", "D"]
def parallelStagesMap = products.collectEntries {
switch (it) {
case "A":
static_ip_address = "10.100.100.6"; static_vm_name = "install-vm1"; version = "14.1.60"
break
case "B":
static_ip_address = "10.100.100.7"; static_vm_name = "install-vm2"; version = "15.1"
break
case "C":
static_ip_address = "10.100.100.8"; static_vm_name = "install-vm3"; version = "15.1"
break
case "D":
static_ip_address = "10.100.100.9"; static_vm_name = "install-vm4"; version = "15.2"
break
default:
static_ip_address = "The product name is not on the switch list - please enter an ip address"
version = "The product name is not on the switch list - please enter a version"
break
}
["${it}" : network_reg(it)]
["${it}" : revert_to_snapshot_Stage(it)]
["${it}" : install_Stage(it)]
}
def network_reg(product) {
return {
stage("stage: setup network for ${product}") {
echo "setting network on ${static_vm_name} with ${static_ip_address}."
sh script: "sleep 15"
}
}
}
def revert_to_snapshot_Stage(product) {
return {
stage("stage: revert ${product}") {
echo "reverting ${static_vm_name} for ${product} on ${static_ip_address}."
sh script: "sleep 15"
}
}
}
def install_Stage(product) {
return {
stage("stage: install ${product}") {
echo "installing ${product} on ${static_ip_address}."
sh script: "sleep 15"
}
}
}
pipeline {
agent any
stages {
stage('non-parallel env check') {
steps {
echo 'This stage will be executed first.'
}
}
stage('parallel stage') {
steps {
script {
parallel parallelStagesMap
}
}
}
}
}
network_reg和revert_to_snapshot_Stage将不会运行(除非我将它们放置为最后一组而不是[“ $ {it}”:install_Stage(it)]),在这种情况下,同样,只有并行阶段之一是运行)
我不介意使用多种方法来运行多个映射定义,但其他诸如How to define and iterate over map in Jenkinsfile的方法不允许使用完整的多变量映射(超过键+值对)
任何帮助将不胜感激,谢谢!
答案 0 :(得分:4)
我假设您遇到类似的问题,就像我试图动态构建并行分支以并行执行一样。
两件事非常重要:
制作循环变量的副本(在您的情况下为it
),并仅在并行分支内使用该副本;如果没有,所有分支(闭包)都将引用相同的变量,而该变量当然具有相同的值。这对于闭包而言尤其如此。另请参见:http://groovy-lang.org/closures.html。
请勿使用collectEntries{}
。坚持使用Java风格的循环,因为通常情况下,groovy循环无法正常工作。一些.each{}
构造可能已经可以使用,但是如果有疑问,请切换到Java循环。另请参阅:Impossibility to iterate over a Map using Groovy within Jenkins Pipeline
下面的示例对我有用。我相信您将能够根据需要进行调整。
def products = ["A", "B", "C", "D"]
def parallelStagesMap = [:]
// use java-style loop
for (def product: products) {
// make a copy to ensure that each closure will get it's own variable
def copyOfProduct = product
parallelStagesMap[product] = {echo "install_Stage($copyOfProduct)"}
}
echo parallelStagesMap.toString()
pipeline {
agent any
stages {
stage('parallel stage') {
steps {
script {
parallel parallelStagesMap
}
}
}
}
}
Pipeline: Groovy
插件,因为它们通常可以解决很多问题,这些问题通常在常规中有效,但不会在管道中解决。 您可能想检查以下相关问题,其中也包含一个最小示例: Currying groovy CPS closure for parallel execution