我在Jenkinsfile中运行以下groovy脚本,该脚本在构建期间执行:
import java.util.stream.Collectors
import java.util.stream.Stream
import groovy.transform.Field
@Field
Map<String, String> sampleMap = [
'SampleKey1': 'SampleValue1',
'SampleKey2': 'SampleValue2'
]
//example param value: "C/SampleKey1/someFile, C/SampleKey2/someFile2"
private Collection<String> getValues(String param) {
Stream.of(param.split(','))
.map { getValueFromOnePath(it) }
.filter { !it.isEmpty() }
.distinct()
.collect(Collectors.toList())
}
private String getValueFromOnePath(String path) {
String[] pathParts = path.split('/')
if (pathParts.size() < 2) {
return ''
}
return sampleMap[pathParts[1]] ?: ''
}
上述参数值代码的预期结果等于:
"C/SampleKey1/someFile, C/SampleKey2/someFile2"
是:
[SampleValue1, SampleValue2]
然而,当这个代码在Jenkins上执行时,我收到一个奇怪的错误,我无法理解(为什么会发生):
hudson.remoting.ProxyException:org.codehaus.groovy.runtime.typehandling.GroovyCastException:无法转换对象&#39; SampleValue1&#39;与class&#39; java.lang.String&#39; to class&#39; java.util.Collection&#39; at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:405) at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319) at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnCollection(DefaultTypeTransformation.java:267) at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:219) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:603) at Unknown.Unknown(未知) 在 cps.transform (原生方法) 在com.cloudbees.groovy.cps.impl.LocalVariableBlock $ LocalVariable.set(LocalVariableBlock.java:45) 在com.cloudbees.groovy.cps.impl.AssignmentBlock $ ContinuationImpl.assignAndDone(AssignmentBlock.java:70) at sun.reflect.GeneratedMethodAccessor303.invoke(未知来源) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) 在com.cloudbees.groovy.cps.impl.ContinuationPtr $ ContinuationImpl.receive(ContinuationPtr.java:72) 在com.cloudbees.groovy.cps.impl.LocalVariableBlock $ LocalVariable.get(LocalVariableBlock.java:39) 在com.cloudbees.groovy.cps.LValueBlock $ GetAdapter.receive(LValueBlock.java:30) 在com.cloudbees.groovy.cps.impl.LocalVariableBlock.evalLValue(LocalVariableBlock.java:28) 在com.cloudbees.groovy.cps.LValueBlock $ BlockImpl.eval(LValueBlock.java:55) 在com.cloudbees.groovy.cps.LValueBlock.eval(LValueBlock.java:16) 在com.cloudbees.groovy.cps.Next.step(Next.java:83) 在com.cloudbees.groovy.cps.Continuable $ 1.call(Continuable.java:174) 在com.cloudbees.groovy.cps.Continuable $ 1.call(Continuable.java:163) 在org.codehaus.groovy.runtime.GroovyCategorySupport $ ThreadCategoryInfo.use(GroovyCategorySupport.java:122) 在org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261) 在com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163) 在org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access $ 001(SandboxContinuable.java:19) 在org.jenkinsci.plugins.workflow.cps.SandboxContinuable $ 1.call(SandboxContinuable.java:35) 在org.jenkinsci.plugins.workflow.cps.SandboxContinuable $ 1.call(SandboxContinuable.java:32) 在org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108) 在org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:32) 在org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174) 在org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:331) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access $ 100(CpsThreadGroup.java:82) 在org.jenkinsci.plugins.workflow.cps.CpsThreadGroup $ 2.call(CpsThreadGroup.java:243) 在org.jenkinsci.plugins.workflow.cps.CpsThreadGroup $ 2.call(CpsThreadGroup.java:231) 在org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService $ 2.call(CpsVmExecutorService.java:64) at java.util.concurrent.FutureTask.run(FutureTask.java:266) 在hudson.remoting.SingleLaneExecutorService $ 1.run(SingleLaneExecutorService.java:112) 在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) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624) 在java.lang.Thread.run(Thread.java:748)
有什么想法吗?
答案 0 :(得分:-1)
您可以尝试使用@NonCPS注释对getValues进行注释。还要避免在要在Jenkins沙箱中执行的代码内部使用Streams,这通常是导致错误的原因。希望对您有所帮助。