Jenkins 管道抛出 ProxyException "expected to call Device.<init> but wound up catching Device.getIP"

标签 jenkins groovy jenkins-groovy

我像这样在 Jenkins 管道中创建了一个类。

class Device
{
    def ip = null
    def context
    
    def getIP(devName) 
    {
        return "aaa.bbb.ccc.ddd"
    }

    Device(context, devName, devType)
    {
        print("[DEBUG] ctor device")
        ip = getIP(devName)
        this.context = context
        print(ip)
    }
}


ap = new Device(this, "DEV", "TYPE")
print ap.ip
当我在“Groovy Web 控制台”( https://groovyconsole.appspot.com/ )中尝试时,它运行良好
但是当我在 Jenkins 中运行这个脚本时,出现以下错误。
[Pipeline] Start of Pipeline

expected to call Device.<init> but wound up catching Device.getIP; see: https://jenkins.io/redirect/pipeline-cps-method-mismatches/

[Pipeline] End of Pipeline

hudson.remoting.ProxyException: CpsCallableInvocation{methodName=getIP, call=com.cloudbees.groovy.cps.impl.CpsFunction@20fd33e6, receiver=Device@57905ade, arguments=[DEV]}

Finished: FAILURE
脚本有什么问题?

最佳答案

底层 Jenkins 管道引擎将您的 Groovy 代码转换为 groovy-cps兼容的。它有几个限制,其中之一是从非 CPS 转换的方法(构造函数方法)调用 CPS 转换的方法(在您的情况下为 getIP)。
这是documentation page描述了这个限制。

Constructors

Occasionally, users may attempt to use CPS-transformed code such as Pipeline steps inside of a constructor in a Pipeline script. Unfortunately, the construction of objects via the new operator in Groovy is not something that can be CPS-transformed (JENKINS-26313), and so this will not work.


您可以删除对 getIP 的调用。方法来自构造函数,或者您可以注释 getIP方法与 @NonCPS注解。

关于Jenkins 管道抛出 ProxyException "expected to call Device.<init> but wound up catching Device.getIP",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63149361/

相关文章:

jenkins - 在 JenkinsFile 中只声明一次 configFileProvider 并从所有阶段引用

jenkins - Jenkins Pipeline 上的动态参数取决于分支

jenkins - 具有多个用户的多个从站

testing - 从普通的 Groovy 迁移到 Gradle

java - 用于查找不包含某些字符的文本的负正则表达式

java - 错误 : java. io.NotSerializedException : groovy. util.slurpersupport.Attributes

jenkins - 无法从 CMakeCache.txt 检索 CMAKE_MAKE_PROGRAM 值

linq - 类似于 LINQ 的 DefaultIfEmpty 和 FirstOrDefault 的 Groovy 方法

Groovy 加载 .csv 文件

jenkins - 如何使用 Jenkins 管道脚本克隆 github 存储库?