jenkins - Jenkins 只执行 groovy 闭包的第一行
问题描述
我有以下两个类,它们在 groovy 本地工作得很好,但是一旦我将它们与 Jenkins 共享库一起使用,我就会遇到一些问题。
./模板.groovy
class Template {
String arch
String type
def body = {}
Template(type, arch, body) {
this.arch = arch
this.type = type
this.body = body
}
}
./TemplateBook.groovy
import Template
class TemplateBook {
def templates = []
TemplateBook() {
def template = new Template("test", "lnx", { args -> sh('echo "Hello World!"'); sh("echo Test"); sh("echo $args")})
templates.push(template)
}
def getTemplate(type, arch) {
def template
for (def i = 0; i < templates.size(); i++) {
if (templates[i].arch == arch && templates[i].type == type) {
template = templates[i].getBody()
i = templates.size()
}
}
return template
}
}
直接使用就Template.body
可以正常工作(在本地和在 Jenkins 上),但使用TemplateBook.getTemplate()
only 执行闭包的第一行(主体)。
def templateBook = new TemplateBook()
def body = templateBook.getTemplate("test", "lnx")
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = this
body("test")
输出:
世界你好!
def template = new Template("type", "arch", { args -> sh('echo "Hello World!"'); sh("echo Test"); sh("echo $args")})
def body2 = template.body
body2.resolveStrategy = Closure.DELEGATE_FIRST
body2.delegate = this
body2("test")
输出:
世界你好!
测试
测试
解决方案
推荐阅读
- sql - SQL:在多组值之间进行查询?
- c# - 如何在 UWP 中使用动画 PNG?
- python - 如何克服 TypeError:日期时间类型的对象不是 JSON 可序列化的
- azure - 能否跨 Azure SQL 和 Azure Cosmos Db 执行分布式事务?
- javascript - React - 当我尝试导入“firebase/app”时出现 Firebase 模块错误
- python - 无法将 pandas 数据帧插入 mongo - 无法编码对象:数组([],dtype=int64),类型:
- java - 如何解决 Hibernate Jpa 中的内存错误?
- javascript - 将数组的对象迭代/打印到 HTML 中,同时保持 Angular 7 中的顺序
- python - Bet365 检测到 Selenium Chromedriver
- mysql - Netbeans 8 和 Java 的连接池问题