从 Jenkins 管道并行步骤收集数据 [英] collect data from Jenkins pipeline parallel steps
问题描述
从并行步骤收集数据(例如通过/失败结果)的最佳方式是什么.
What would be the best way to collect data (such as pass/fail results) from parallel steps.
到目前为止我已经达到了什么:
What I've reached so far:
#!groovy
def fspam(name, spam){
spam[name] = "BEEN THERE TOO"
}
// pipeline
node('slave'){
stage("test"){
targets = ["a", "b"]
def tasks = [:]
def spam = [:]
targets.each{ tasks["${it}"] = {
node('slave'){
echo "dry-run ${it}"
spam[it] = "BEEN THERE" <--- works
fspam(it) <--- fails
}
}
}
parallel tasks
print("spam")
print(spam)
}
}
但它失败了:
另外:groovy.lang.MissingPropertyException:没有这样的属性:stam对于类:WorkflowScript groovy.lang.MissingPropertyException:否这样的属性:stam 类:WorkflowScript atorg.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
Also: groovy.lang.MissingPropertyException: No such property: stam for class: WorkflowScript groovy.lang.MissingPropertyException: No such property: stam for class: WorkflowScript at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
- 似乎封闭式可以成功填充地图,但是在使用函数时会引发错误
- 我不确定拥有一张全球地图是最好/最干净的方式
任何建议
推荐答案
targets = ["a", "b"]
tasks = [:]
spam = [:].asSynchronized()
targets.each { target ->
tasks[target] = {
echo "dry-run ${target}"
spam[target] = "BEEN THERE"
fspam(target, spam) // <--- passing spam fixes the issue
}
}
parallel tasks
print("spam")
print(spam)
这保证了对地图的更新是线程安全的.要收集列表,您可以使用 [].asSynchronized()
.链接
This guarantees that updates to the map are thread-safe. For collecting a list, you can use [].asSynchronized()
. Link
这篇关于从 Jenkins 管道并行步骤收集数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!