【问题标题】:Jenkins groovy MissingMethodException No signature of methodJenkins groovy MissingMethodException 没有方法的签名
【发布时间】:2018-12-26 23:33:45
【问题描述】:

我面对

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(...)

  • 没有任何参数
  • 带字符串参数
  • 带有地图参数

没有任何作用。所以我想要一个不接受任何参数的函数启动并运行。但它与整个堆栈跟踪分解如下,

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

【问题讨论】:

  • 在我当地的 Jenkins 上试过。它不允许我使用pipeline 作为脚本名称,这给了我一个“不允许”的例外。当我使用 customPipeline.getThingsDone() 之类的其他东西时,一切都按预期工作。尝试使用不同的名称作为 pipeline 可能会干扰声明性管道的全局 pipeline 步骤。如果它没有帮助你应该更新 Jenkins 和/或你的管道插件。
  • 它确实允许我拥有管道脚本名称。没问题。

标签: jenkins groovy jenkins-shared-libraries


【解决方案1】:

需要从脚本指令访问全局变量

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

【讨论】:

  • 您的示例代码实际上看起来不像您使用的是声明式管道。在脚本化管道中,您不需要任何额外的脚本块。
【解决方案2】:

问题:您配置了一个外部 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 而不是 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 2022-08-06
    • 2017-03-02
    • 2021-04-28
    • 2014-10-16
    • 1970-01-01
    相关资源
    最近更新 更多