Groovy代码有效,然后在Jenkins中无效。如何进行这项工作?

时间:2019-07-09 18:30:58

标签: jenkins groovy

我正在尝试增加“加载”管线步进功能,但不断出现错误。我发现它是基于堆栈跟踪执行的代码,但是我无法一辈子弄清楚为什么它不只是按编写的方式调用代码。

我已经写了很多Java代码,所以我知道它要做什么。我只是不明白为什么它要这样做或如何说服它停止! groovy示例非常完美!顺便说一句:如果在groovy / jenkins中有一种惯用的方法可以做到这一点,那么我就全神贯注了。

詹金斯版本:2.17.61 Groovy插件:2.2

test.groovy

def someFunction(def params){
    println("someFunction ${params}")
}
def someFunction2(def params){
    println("someFunction2 ${params}")
}
def mainFunc(def stuff){

}
def somemainThingrunFunmain(){

}
def ___cps___21685(){

}
def ___cps___21688(){

}
this

main.groovy

def loaded = evaluate('test.groovy' as File)

def toAugment = loaded.class.declaredMethods*.name.findAll { !(it =~ '^(main|run)$|^[$]|^(___cps___)') }
def proxy = new Script(this.binding) {
    @Override
    Object run() {
        monad.run()
    }
}
toAugment.each {
    proxy.metaClass."${it}" = { "logging ${it}".tap { println(it)} } >> loaded.&"${it}"
}

proxy.someFunction('hello world1')
proxy.someFunction2('hello world2')

输出:

called
someFunction hello world1
called
someFunction2 hello world2

现在是詹金斯:

Jenkinsfile:

library 'common-libraries@chb0'

node('java') {

    stage('SCM') {
        checkout scm
    }

    def loaded = load('test.groovy')
    stage('experiment') {
         loaded.someFunction('hello world1')
         loaded.someFunction2('hello world2')
    }
}

适应的库(在common-library:vars / load.groovy中):

def call(String path) {

    def loaded = steps.load(path)
    def proxy = new Script(this.getBinding()) { // error here
        @Override
        Object run() {
            loaded.run()
        }
    }
    // remove groovy and jenkins generated functions. Don't touch those
    def toAugment = loaded.class.declaredMethods*.name.findAll { !(it =~ '^(main|run)$|^[$]|^(___cps___)') }
    toAugment.each {
        proxy.metaClass."${it}" = { "logging ${it}".tap { println(it) } } >> loaded.&"${it}"
    }
}

例外:

groovy.lang.GroovyRuntimeException: Could not find matching constructor for: load$1(load, Script1, groovy.lang.Binding)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1732)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1532)
at org.codehaus.groovy.runtime.callsite.MetaClassConstructorSite.callConstructor(MetaClassConstructorSite.java:49)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.constructorCall(DefaultInvoker.java:25)

0 个答案:

没有答案