【发布时间】:2018-01-10 07:02:39
【问题描述】:
问题
我有与 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"
}
}
}
}
}
}
}
【问题讨论】:
-
我建议使用scripted pipeline,这样更灵活。
-
我阅读了这篇文章,但不知道如何在此处应用它。你能举个例子吗?文档真的很少。