问题
我有简单的并行管道(见代码),我和Jenkins 2.89.2一起使用 . 另外,我使用参数,现在希望能够通过在作业执行之前提供参数来自动减少deployVM A..Z阶段的数量 .
如何通过提供参数动态构建我的管道?
到目前为止研究:
-
Jenkins pipeline script created dynamically - 不能使用我的Jenkins版本
-
Can I create dynamically stages in a Jenkins pipeline? - 也没有工作
代码
我想要的伪代码 - 动态生成:
pipeline {
agent any
parameters {
string(name: 'countTotal', defaultValue: '3')
}
stages {
stage('deployVM') {
def list = [:]
for(int i = 0; i < countTotal.toInteger; i++) {
list += stage("deployVM ${i}") {
steps {
script {
sh "echo p1; sleep 12s; echo phase${i}"
}
}
}
}
failFast true
parallel list
}
}
}
我到目前为止的代码 - 执行并行但是静态的:
pipeline {
agent any
stages {
stage('deployVM') {
failFast true
parallel {
stage('deployVM A') {
steps {
script {
sh "echo p1; sleep 12s; echo phase1"
}
}
}
stage('deployVM B') {
steps {
script {
sh "echo p1; sleep 20s; echo phase2"
}
}
}
}
}
}
}
1 回答
虽然这个问题假设使用声明性管道,但我建议使用scripted pipeline,因为它更灵活 .
您的任务可以通过这种方式完成
另请参阅snippet generator,它允许您生成一些脚本化的管道代码 .