我像这样在 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/