Jenkins管道:没有这样的DSL方法

时间:2016-08-11 11:47:30

标签: jenkins groovy jenkins-pipeline

使用此代码我在Jenkins管道中出错。我不明白为什么? 我错过了什么吗?

node {
   stage 'test'
   def whatThe = someFunc('textToFunc')
   {def whatThe2 = someFunc2('textToFunc2')}
}


def someFunc(String text){
    echo text
    text
}
def someFunc2(String text2){
    echo text2
    text2
}

错误:

java.lang.NoSuchMethodError: **No such DSL method 'someFunc'** found among [archive, bat, build, catchError, checkout, deleteDir, dir, echo, emailext, emailextrecipients, error, fileExists, git, input, isUnix, load, mail, node, parallel, properties, pwd, readFile, readTrusted, retry, sh, sleep, stage, stash, step, svn, timeout, timestamps, tool, unarchive, unstash, waitUntil, withCredentials, withEnv, wrap, writeFile, ws]
        at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:124)
        at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:117)
        at groovy.lang.MetaClassImpl.invokeMethodOnGroovyObject(MetaClassImpl.java:1280)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1174)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
        at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
        at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:15)
        at WorkflowScript.run(WorkflowScript:4)
        at ___cps.transform___(Native Method)
        at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:55)
        at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:106)
        at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:79)
        at sun.reflect.GeneratedMethodAccessor878.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
        at com.cloudbees.groovy.cps.impl.ClosureBlock.eval(ClosureBlock.java:40)
        at com.cloudbees.groovy.cps.Next.step(Next.java:58)
        at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)
        at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:164)
        at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:360)
        at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:80)
        at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:236)
        at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:226)
        at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:47)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
        at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Finished: FAILURE

3 个答案:

答案 0 :(得分:22)

sumfunc2调用周围删除额外的括号:

node {
   stage 'test'
   def whatThe = someFunc('textToFunc')
   def whatThe2 = someFunc2('textToFunc2')
}

def someFunc(String text){
    echo text
    text
}
def someFunc2(String text2){
    echo text2
    text2
}

<强>更新

在Groovy中,如果方法的最后一个参数是Closure类型,那么在调用方法时,闭包可以在括号之外,如:

def foo(whatever, Closure c) {}

// Can be invoked as
foo(whatever, {
    // This is the second argument of foo of type Closure
})

// It is also the same as writing
foo(whatever) {
    // This is the second argument of foo of type Closure
}

原始抛出的原因是因为以下代码

def whatThe = someFunc('textToFunc')
{def whatThe2 = someFunc2('textToFunc2')}

的代码相同
def whatThe = someFunc('textToFunc') {
    def whatThe2 = someFunc2('textToFunc2')
}

这意味着解释器将要寻找的是

someFunc(String text, Closure c)

并且没有这样的方法

答案 1 :(得分:12)

由于此答案是我在查找“无此类DSL方法”消息时发现的第一个答案,因此我想补充一点,它也可能是不匹配的接口。在我的情况下,第一个参数是一个列表,但是我尝试使用数组调用该方法。因此,请检查您的界面是否符合您的期望,并正确输入参数。

答案 2 :(得分:0)

就我而言,发生的事情是我正在使用:${} 引用一个变量,该变量正在扩展为空字符串:

env.VAR1=${VAR2}

就我而言,var2 并不真正存在,所以我实际上必须做的是:

env.VAR1=env.VAR2

这是我最后的愚蠢错误。