jenkins - 詹金斯管道中动态生成的并行块
问题描述
我使用 Jenkins 管道使用“并行”构造同时执行一些构建/部署任务:
stage('tasks')
parallel('task1': {someFunction(arg=1)},
'task2': {someFunction(arg=2)},
'task3': {someFunction(arg=3)}
)
}
有时我想在运行时动态构建这个结构。可能吗 ?
示例:要从 10 个列表中为选定目标复制的文件。
当用户选择 4 个项目时,我想创建一个包含 4 个项目的“并行”结构,然后执行它。
解决方案
基本上,您可以创建一个函数,该函数返回保存项目任务的 groovy 闭包映射。
请记住,您需要关闭 groovy 沙盒才能运行它。
#!/usr/bin/env groovy
def getSomeFunction = { arg ->
// returns the closure with your task, function, run with the chosen parameter
return {
println(arg)
}
}
def getParalellBlock = { number ->
def myList = ['a', 'b', 'c', 'd'] // your list of items
def blockMap = [:]
1.upto(number, {
// Make sure to handle 'index out of range' kind of problems
blockMap.put(it.toString(), getSomeFunction(myList[it-1]))
})
return blockMap
}
node() {
stage('tasks') {
// you can supply the parameter from job parameters
parallel(getParalellBlock(3))
}
}
推荐阅读
- python - 如何停止循环耗尽内存?
- kotlin - 是否有用于查找向量中元素索引的 Kotlin 函数?
- asp.net-core - 如何在 IHostingStartup 实现中访问配置?
- css - stroke-dasharray 和 stroke-dashoffset 填充进度
- python - 使用您选择的给定分隔符将字符和数字段与给定字符串分开
- laravel - 一般错误:3730 无法删除由外键约束引用的表“问卷”(SQL:如果存在“问卷”,则删除表) Laravel
- java - 如何在 Windows 终端上使用 Log4j2 启用 ANSI 颜色?
- python - 使用 fill_between 和 min/max 来表示不等式
- css - React 更改 antd 选择背景颜色
- .net - 如何在 C# 中为 ProtoBuf 重复字段添加值