简体   繁体   English

尝试使用 readFile() 时 Jenkins Groovy 错误 groovy.lang.MissingMethodException

[英]Jenkins Groovy error groovy.lang.MissingMethodException when trying to use readFile()

I've been getting an odd error in Jenkins when trying to run readFile() within a shared library, so that I can determine if a Dockerfile is pulling from ECR or docker hub.尝试在共享库中运行 readFile() 时,我在 Jenkins 中遇到了一个奇怪的错误,因此我可以确定 Dockerfile 是从 ECR 还是 Z05B6053C41A2130AFD6FC3B158BDAE6 中提取。 The error is:-错误是:-

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: org.myorg.DockerManager.readFile() is applicable for argument types: (java.lang.String) values: [DockerServiceDescription/Dockerfile]
    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)
    at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:20)
    at org.proj.DockerManager.login(/tmp/persistence/jobs/PROJ/jobs/builds/jobs/proj-subproj/branches/PROJ-4190/builds/47/libs/Global/src/org/me/DockerManager.groovy:26)

The method that's triggering the error is:-触发错误的方法是:-

    /**
        Login to docker
     */
    void login() {
        dockerfileHandle = readFile(dockerfile)
        dockerfileLines = dockerfileHandle.readLines()
        def dockerfileFROM = dockerfileLines.find{ dockerfileLine-> dockerfileLine =~ /^FROM / }

        println("FROM: " + dockerfileFROM)
        if (dockerfileFROM =~ /\.ecr\./ ) {
            println("Using ECR")
            ecrlogin()
        } else {
            println("Using DOCKER HUB")
            dockerhublogin()
        }
    }

however that same code worked perfectly well within the pipeline code, it just breaks when moved into a shared library.但是,相同的代码在管道代码中运行良好,只是在移入共享库时会中断。

Any ideas?有任何想法吗?

Access to pipeline steps is done via an instance of CPSScript that represents the running pipeline.对管道步骤的访问是通过代表正在运行的管道的 CPSScript 实例完成的。 To use it in a class of shared library, you have to pass this into your library, either as constructor or method argument:要在共享库的 class 中使用它,您必须将this作为构造函数或方法参数传递到您的库中:

    /**
        Login to docker
     */
    void login(def script) { // actually CPSScript, but that way, no import is needed
        dockerfileHandle = script.readFile(dockerfile)
        //...
    }

This from your actual Jenkins file, you then call it like:这来自您的实际 Jenkins 文件,然后您将其称为:

myInstance.login(this)

Also note that you println calls would result in output in the Jenkins logs, not the build logs, what you are aiming for is script.echo()另请注意,您println调用将导致 Jenkins 日志中的 output ,而不是构建日志,您的目标是script.echo()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jenkins Groovy构建作业错误groovy.lang.MissingMethodException - Jenkins groovy build job error groovy.lang.MissingMethodException groovy.lang.MissingMethodException:方法的无签名:在詹金斯构建流程的groovy中 - groovy.lang.MissingMethodException: No signature of method: in groovy on jenkins buildflow Jenkins groovy.lang.MissingMethodException:没有方法签名:catchError() - Jenkins groovy.lang.MissingMethodException: No signature of method: catchError() groovy.lang.MissingMethodException:没有方法签名:为什么 Jenkins 共享管道库会出现此错误? - groovy.lang.MissingMethodException: No signature of method: Why is this error coming for Jenkins Shared Pipeline library? jenkinspipeline groovy.lang.MissingMethodException:没有方法签名 - jenkinspipeline groovy.lang.MissingMethodException: No signature of method groovy.lang.MissingMethodException:方法的无签名:java.util.ArrayList - groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList hudson.remoting.ProxyException:groovy.lang.MissingMethodException:方法没有签名: - hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: 在 groovy 中,无法将字符串与正则表达式模式匹配。 groovy.lang.MissingMethodException 发生 - In groovy, not able to match a string with regex pattern. groovy.lang.MissingMethodException occurs 如何修复“hudson.remoting.ProxyException:groovy.lang.MissingMethodException:没有方法签名:testFunc.call()” - How to fix 'hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: testFunc.call() ' hudson.remoting.ProxyException: groovy.lang.MissingMethodException: 没有方法签名: java.util.LinkedHashMap.call() 是适用的 - hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.util.LinkedHashMap.call() is applicable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM