Jenkins:groovy DSL:使用三元运算符区分 FreeStyleJob 和 MatrixJob
Jenkins: groovy DSL: using the ternary operator to distinguish between FreeStyleJob and MatrixJob
我正在尝试为 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
如何根据布尔值重写上面的表达式来生成两个不同的作业?
我认为问题与闭包的三元运算符的使用有关,也许它不打算以这种方式使用?
我是这样解决的:
def jobInstance = !config.toolchainsBuild ? job("job1") : matrixJob("job2")
jobInstance.with {
// ... job definition follows
}
即使用with
方法。这样,闭包只写一次。
我正在尝试为 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
如何根据布尔值重写上面的表达式来生成两个不同的作业?
我认为问题与闭包的三元运算符的使用有关,也许它不打算以这种方式使用?
我是这样解决的:
def jobInstance = !config.toolchainsBuild ? job("job1") : matrixJob("job2")
jobInstance.with {
// ... job definition follows
}
即使用with
方法。这样,闭包只写一次。