本文介绍了Jenkins中的WorkflowScript.with的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jenkins共享库中有一个类,该类存储来自jenkins管道脚本的WorkflowScript类的实例,如下所示.

I have a class in a jenkins shared library that stores the instance of a WorkflowScript class from a jenkins pipeline script as shown below.

def myTools = new my.org.MyTools(this)

MyTools的构造函数只是存储WorkflowScript的实例,就像这样...

the constructore of MyTools just stores the instance of WorkflowScript like this...

MyTools(script){
    this.script = script
}

然后我有一种方法尝试在script上使用groovy的.with但失败了...

then I have a method that tries to use groovy's .with on script but fails...

myMethod(){
    script.with{
        node{
            echo "I want to be able to use pipeline syntax here"
            sh 'echo "without using script. in front of each command"'
        }
    }
}

但是当我运行它时,出现以下错误...

But when I run this i get the following error...

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: org.jenkinsci.plugins.workflow.cps.CpsClosure2.node() is applicable for argument types: (org.jenkinsci.plugins.workflow.cps.CpsClosure2) values: [org.jenkinsci.plugins.workflow.cps.CpsClosure2@855f14e]
Possible solutions: clone(), use([Ljava.lang.Object;), notify(), wait(), call(), run()
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:54)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite
.java:113)

我希望script.with闭包的内部可以访问与script.相同的所有内容,但是看起来它只能访问java.lang.Object的方法.能否使script.with闭包在与script.相同的上下文中起作用?

I would have expected the inside of the script.with closure to have access to all of the same stuff as script., but it looks like it only has access to methods of java.lang.Object. Is it possible to get the script.with closure to function with the same context of script.?

推荐答案

用于groovy封闭可以指定该封包用于解析方法和属性的策略

for groovy closure possible to specify strategy which the closure uses to resolve methods and properties

有以下选项:DELEGATE_FIRST,DELEGATE_ONLY,OWNER_FIRST(默认),OWNER_ONLY,TO_SELF

there are options: DELEGATE_FIRST, DELEGATE_ONLY, OWNER_FIRST (default), OWNER_ONLY, TO_SELF

我认为管道脚本具有特殊的策略,并且在您的类中有一个默认策略.

i think the pipeline script has a special strategy, and in your class there is a default one.

这篇关于Jenkins中的WorkflowScript.with的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 22:34