jenkins管道:无法将构建参数传递给共享库vars [英] jenkins pipeline: can't pass build parameters to shared library vars
问题描述
基本上,没有多余的废话,我无法将构建属性传递给Library var调用.
Basically I can't pass build properties to Library var call without extra nonsense.
jenkinsfile相关的块:
jenkinsfile relevant chunk:
tc_test{
repo = 'test1'
folder = 'test2'
submodules = true
refs = params.GitCheckout
}
这会导致错误
java.lang.NullPointerException:无法获取属性'GitCheckout' 空对象
java.lang.NullPointerException: Cannot get property 'GitCheckout' on null object
这有效,
def a1 = params.GitCheckout
tc_test{
repo = 'test1'
folder = 'test2'
submodules = true
refs = a1
}
共享库中vars/tc_test.groovy的内容:
The contents of the vars/tc_test.groovy in shared library :
def call ( body ) {
def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
try {
body()
} catch(e) {
currentBuild.result = "FAILURE";
throw e;
} finally {
config.each{ k, v -> println "${k}:${v}" }
}
}
我对groovy不太满意,所以可能很明显.
I'm not really good with groovy, so it might be something obvious.
推荐答案
Small workaround is using maps instead of closures:
tc_test ([
repo: 'test1',
folder: 'test2',
submodules: true,
refs = params.GitCheckout
])
可能有缺点,但对我来说效果很好.
May have drawbacks, but for me that worked perfectly.
仍然必须将参数作为参数传递才能访问它们,但是至少现在代码更有意义了.
Still have to transfer params as argument to have access to them, but at least the code makes more sense now.
这篇关于jenkins管道:无法将构建参数传递给共享库vars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!