从并行步骤收集数据(例如通过/失败结果)的最佳方法是什么。
到目前为止我已经达到的目标:
#!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位于 org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
任何建议
答案 0 :(得分:1)
好吧,错过了一个显而易见的解决方案:
#!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"
fspam(it, spam) <--- passing spam fixes the issue
}
}
}
parallel tasks
print("spam")
print(spam)
}
}
一个问题仍然存在:有更好/更清洁的方法吗?(线程安全性/ jenkins管道本机等)
答案 1 :(得分:1)
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()
。 Link