【发布时间】:2018-08-06 14:49:36
【问题描述】:
我想就 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 不允许完整的多变量映射(超过键+值对)
任何帮助将不胜感激,谢谢!
【问题讨论】:
标签: jenkins groovy jenkins-pipeline jenkins-groovy