我对Jenkins/Groovy还比较陌生,请多包容。
我正在使用管道Groovy脚本中的DSL。DSL实例化了一个自定义类,该类尝试使用Jenkins插件。我一直在收到错误,似乎系统试图将插件作为类的直接成员访问...?
Jenkins任务:管道脚本
@Library('lib-jenkins-util@branchname') _ // contains dsl.groovy
dsl {
x = 'value'
}
文件: dsl.groovy
def call(body) {
def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()
node('node-name') {
stage('Stage 1') {
def f = new Foo()
}
}
文件名: Foo.groovy
class Foo implements Serializable {
Foo() {
// fails below
sh "curl http://our-jenkins-server/xxx/api/json -user username:apitoken -o output.json"
json = readJSON file: output.json
}
}
错误:
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: 没有找到方法的签名:com.xxx.Foo.sh() 可适用于参数类型:(org.codehaus.groovy.runtime.GStringImpl) 值:[curl http://our-jenkins-server/xxx/api/json -user username:apitoken -o output.json]
请问有人能帮我理解我错过了什么吗?我们不能直接从自定义类中调用插件吗?