web-dev-qa-db-fra.com

Pipeline Jenkins: pas de méthode DSL

Avec ce code, j'ai eu une erreur dans le pipeline Jenkins. Je ne comprends pas pourquoi? Est-ce que je manque quelque chose?

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
}

Erreur:

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
23
Kayden

retirez les crochets supplémentaires autour du sumfunc2 invocation:

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
}

Mise à jour:

En Groovy, si le dernier argument d'une méthode est de type Closure, lors de l'appel de la méthode, la fermeture peut être en dehors des crochets, comme suit:

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
}

La raison pour laquelle l'original jette est parce que le code suivant

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

est le même code que

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

Cela signifie que ce que l'interprète cherchera, c'est

someFunc(String text, Closure c)

et il n'y a pas une telle méthode

23
Gergely Toth