Jenkins Groovy MissingMethodException方法没有签名

时间:2018-07-18 23:31:48

标签: jenkins groovy jenkins-shared-libraries

我正面临

  

getThingsDone()适用于参数类型:()值:[]

以下代码错误

in shared library ....
vars/pipeline.groovy

def getThingsDone(m = null){
   echo "done"
}

in some project that includes shared library

@Library(shared_pipeline@branch_name) _
pipeline.getThingsDone()

我尝试定义getThingsDone(...)

  • 不带任何参数
  • 带有字符串参数
  • 带有Map参数

没有任何效果。所以我想获得一个不接受任何参数并开始运行的函数。但这会破坏整个堆栈跟踪,如下所示,

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.getThingsDone() is applicable for argument types: () values: []
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:64)
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)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:155)
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:133)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:120)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:153)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:157)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
at WorkflowScript.run(WorkflowScript:7)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixName(FunctionCallBlock.java:77)
at sun.reflect.GeneratedMethodAccessor208.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:19)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:35)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:32)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:32)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:331)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:82)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:243)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:231)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
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(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Finished: FAILURE

2 个答案:

答案 0 :(得分:1)

需要从脚本指令访问

全局变量

在上述情况下,getThingsDone()是全局函数。并且根据https://issues.jenkins-ci.org/browse/JENKINS-42360,不允许在脚本指令之外使用全局变量/函数。

答案 1 :(得分:0)

问题:您配置了一个外部 Jenkins 共享库,其中包含您想在 jenkins 管道中使用的一些函数。但是,您的 Jenkins 无法识别方法签名并引发异常。

问题

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: 
No signature of method: java.lang.Class.someMethodName()

解决方案

to 之间的关系如下:

<shared library> contains: <some function> -> jenkins imports this function and can use it. 
  1. 验证函数名称没有错字或缺失字符,验证输入参数的顺序是否正确。

  2. 您需要验证库是否正确导入

    @Library('my-shared-library') _
    
    /* Using a version specifier, such as branch, tag, etc */
    @Library('my-shared-library@1.0') _
    
    /*Accessing multiple libraries with one statement */
    @Library(['my-shared-library', 'otherlib@abc1234']) _
    
  3. 全局函数应该包含在脚本指令中,例如文件名:startServer.groovy

    #!/usr/bin/env groovy
    
    def call() {
        script{
            sh """#!/bin/bash -le
                cd ${workspace}/app
                npm start
            """
        }
    }
    

[关于詹金斯管道:]

 //pipeline code...
  startServer()
  1. 如果您在 groovy 类中声明一个 静态函数,请确保该类以小写字母开头。例如使用:nodeUtils.groovy 和 NOT Nodeutils.groovy(由于某种原因无法识别)

     #!/usr/bin/env groovy
    
        def startServer() {
            script{
                sh """#!/bin/bash -le
                    cd ${workspace}/app
                    npm start
                """
            }
        }
    
        def installPackages() {
            script{
                sh """#!/bin/bash -le
                    cd ${workspace}/app
                    npm install --save-dev
                """
            }
        }
    

[关于詹金斯管道:]

//pipeline code...
nodeUtils.installPackages()