我正在尝试为 Jenkins 编写一个groovy-dsl脚本来生成两个作业:
- 第一份工作是 FreestyleJob
- 第二个是MatrixJob
它们的定义几乎相同;它们之间只有细微的差别。因此,我想重用大部分作业代码,我来到了以下重构场景(请关注第五行,在三元运算符中):
[
['toolchainsBuild': false],
['toolchainsBuild': true],
].each { Map config ->
config.toolchainsBuild ? job("job1") : matrixJob("job2") {
// job definition follows...for example:
out.println("debug")
steps {
cmake {
buildToolStep {}
}
}
// if (config.toolchainsBuild) {
// ... // different actions, depending on the job type
// }
}
}
但是,这不起作用。证明:debug
在日志文件中只打印一次(它应该出现两次,因为我想要定义两个不同的作业)。
我还尝试将三元运算符及其操作数括在括号中,如下所示:
(config.toolchainsBuild ? job("job1") : matrixJob("job2")) {
// ...
但是,这会导致语法错误:
Processing provided DSL script
ERROR: (script, line 20) No signature of method: javaposse.jobdsl.dsl.jobs.MatrixJob.call() is applicable for argument types: (script$_run_closure1$_closure2) values: [script$_run_closure1$_closure2@2cb2656f]
Possible solutions: wait(), label(), any(), wait(long), label(java.lang.String), each(groovy.lang.Closure)
Started calculate disk usage of build
Finished Calculation of disk usage of build in 0 seconds
Started calculate disk usage of workspace
Finished Calculation of disk usage of workspace in 0 seconds
Notifying upstream projects of job completion
Finished: FAILURE
如何根据布尔值重写上述表达式以产生两个不同的工作?
我相信这个问题与使用闭包的三元运算符有关,也许它不打算以这种方式使用?