我正在使用 Jenkinsfile 编写一个简单的管道来测试 Groovy 中的类和接口(interface),但我经常遇到 Jenkins 返回没有任何意义的错误的情况。例如,下面的代码实现了一个简单的构建器模式:
class Configuration implements Serializable {
ArrayList credentials
}
class ConfigurationBuilder implements Serializable {
Configuration configuration
ConfigurationBuilder() {
configuration = new Configuration()
}
ConfigurationBuilder withCredentials(ArrayList credentials) {
configuration.credentials = credentials
return this
}
Configuration build() {
return configuration
}
}
node("commons") {
ConfigurationBuilder builder = new ConfigurationBuilder()
Configuration conf = builder.withCredentials(['test']).build()
echo conf.credentials
}
上面的代码在 Jenkins 上运行时返回此错误:
java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.EchoStep.message expects class java.lang.String but received class java.util.ArrayList
at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:492)
at org.jenkinsci.plugins.structs.describable.DescribableModel.buildArguments(DescribableModel.java:409)
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:329)
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:272)
at org.jenkinsci.plugins.workflow.steps.StepDescriptor.newInstance(StepDescriptor.java:202)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:262)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:176)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
at sun.reflect.GeneratedMethodAccessor346.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
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:158)
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:157)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:156)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:160)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:130)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
Caused: java.lang.IllegalArgumentException: Could not instantiate {message=[test]} for org.jenkinsci.plugins.workflow.steps.EchoStep
知道出了什么问题吗?
我尝试以最简单的方式实现它,认为这是 Jenkins 的一些限制!
最佳答案
您将 ArrayList
传递给 echo
步。此步骤仅接受 String
类型的值。
java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.EchoStep.message expects class java.lang.String but received class java.util.ArrayList
您可以通过调用 conf.credentials.toString()
来修复此错误
node("commons") {
ConfigurationBuilder builder = new ConfigurationBuilder()
Configuration conf = builder.withCredentials(['test']).build()
echo conf.credentials.toString()
}
关于使用 groovy 类的 Jenkinsfile 在使用列表时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61258443/