传递 Map 和使用 `body.resolveStrategy = Closure.DELEGATE_FIRST` 有什么区别
What is the difference between passing a Map and using `body.resolveStrategy = Closure.DELEGATE_FIRST`
这两个封装管道的示例 pipelineParams
来自两种不同的方法,但尚不清楚为什么一种优于另一种。
使用
的后果是什么
def call(body) {
// evaluate the body block, and collect configuration into the object
def pipelineParams= [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = pipelineParams
body()
pipeline {
echo pipelineParams.name
}
}
与使用
def call(Map pipelineParams) {
pipeline {
echo pipelineParams.name
}
}
示例代码来自 https://jenkins.io/blog/2017/10/02/pipeline-templates-with-shared-libraries/
不同之处在于,在第一种情况下,使用管道看起来像是声明式配置。就DSL而言,这就是所谓的builder strategy:
myDeliveryPipeline {
branch = 'master'
scmUrl = 'ssh://git@myScmServer.com/repos/myRepo.git'
...
}
而在第二种情况下,应用管道看起来像命令式代码,即它是一个常规函数调用:
myDeliveryPipeline(branch: 'master', scmUrl: 'ssh://git@myScmServer.com/repos/myRepo.git', ...)
the official Jenkins doc里面也有解释:
There is also a “builder pattern” trick using Groovy’s Closure.DELEGATE_FIRST, which permits Jenkinsfile to look slightly more like a configuration file than a program, but this is more complex and error-prone and is not recommended.
就我个人而言,我不能说我不推荐 DSL 方法。文档不推荐这样做,因为它有点复杂并且容易出错
这两个封装管道的示例 pipelineParams
来自两种不同的方法,但尚不清楚为什么一种优于另一种。
使用
的后果是什么def call(body) {
// evaluate the body block, and collect configuration into the object
def pipelineParams= [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = pipelineParams
body()
pipeline {
echo pipelineParams.name
}
}
与使用
def call(Map pipelineParams) {
pipeline {
echo pipelineParams.name
}
}
示例代码来自 https://jenkins.io/blog/2017/10/02/pipeline-templates-with-shared-libraries/
不同之处在于,在第一种情况下,使用管道看起来像是声明式配置。就DSL而言,这就是所谓的builder strategy:
myDeliveryPipeline {
branch = 'master'
scmUrl = 'ssh://git@myScmServer.com/repos/myRepo.git'
...
}
而在第二种情况下,应用管道看起来像命令式代码,即它是一个常规函数调用:
myDeliveryPipeline(branch: 'master', scmUrl: 'ssh://git@myScmServer.com/repos/myRepo.git', ...)
the official Jenkins doc里面也有解释:
There is also a “builder pattern” trick using Groovy’s Closure.DELEGATE_FIRST, which permits Jenkinsfile to look slightly more like a configuration file than a program, but this is more complex and error-prone and is not recommended.
就我个人而言,我不能说我不推荐 DSL 方法。文档不推荐这样做,因为它有点复杂并且容易出错