我正在尝试使用“script”指令中的load函数将脚本导入到groovy脚本中。以下是我要实现的简化代码
项目结构
Jenkinsfile
script.groovy
helper/
Builder.groovy
Jenkinsfile
....
stage ('Build') {
steps {
script {
def executer = load "script.groovy"
executer.printText()
}
}
}
....
script.groovy
import helper.Builder
builder = new Builder()
def printText(){
def text = builder.getText()
println text
}
Builder.groovy
package helper
class Builder{
def getText(){
return "blah"
}
}
但是,在运行Jenkinfile时,它会一直失败,我注意到代码“import helps.builder”会导致问题。但我应该能够将Builder类导入“script.groovy”,以便我可以调用Builder.getText()函数。
我不明白的一件事是,当我尝试使用终端运行script.groovy时,它运行良好且无问题。
有没有人有想法?
顺便说一下,以下是Jenkins的错误消息
an exception which occurred:
in field collector
in field abnormal
in field outcome
in field body
in field step
in field thread
....
....
Caused: java.io.NotSerializableException: org.codehaus.groovy.control.ErrorCollector
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860)
at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
....
....
答案 0 :(得分:0)
jenkins管道groovy沙箱并不像命令行那样。可以对此进行调试,但您的里程数必然会有所不同,具体取决于jenkins核心版本和您正在运行的管道插件。即使你开始工作,它在未来版本中也可能会中断。您应该使用jenkins shared libraries,这是将共享代码注入管道的受支持方式。