[英]Cannot access Jenkins parameters from a templatized pipeline argument
I cannot access to any Jenkins parameter (ex: A
as below) from the scope of myPipelineTemplate
. 我无法从myPipelineTemplate
的范围访问任何Jenkins参数(例如: A
如下)。
From the Jenkinsfile
file: 从Jenkinsfile
文件:
library 'myPipelineTemplate'
properties([
parameters([
booleanParam(name: 'A', defaultValue: false, description: '')
])
])
myPipelineTemplate {
arg1 = A
arg2 = true
}
From the pipeline template: 从管道模板中:
def call(body) {
def args = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = args
body()
echo "$args.arg1" // return (null)
echo "$args.arg2" // return (true)
pipeline {
...
}
Any lead ? 有铅吗?
EDIT: Jenkins ver. 编辑:詹金斯版。 2.107.1 2.107.1
You are using Closure.DELEGATE_FIRST
strategy and the delegate
is a Map
. 您正在使用Closure.DELEGATE_FIRST
策略,并且delegate
是Map
。 Your arg2 = true
assigns the property, but the A
property lookup is a key lookup on that map which is why the assignment is null
. 您的arg2 = true
会分配属性,但是A
属性查找是该映射上的键查找,这就是分配为null
。 The A
property lookup is never done on the owner
context which would eventually delegate to params.A
. 永远不会在owner
上下文上进行A
属性查找,该owner
上下文最终将委托给params.A
。
With your delegation strategy, the invocation sort of looks like this: 使用您的委派策略,调用看起来像这样:
delegate.arg1 = delegate.A
delegate.arg2 = true
where delegate
is the def args = [:]
. 其中delegate
是def args = [:]
。 The delegate
in this case handles the property lookup. 在这种情况下, delegate
处理属性查找。 The value is null
and then it is assigned to delegate.arg1
. 该值为null
,然后将其分配给delegate.arg1
。 Your map after the call is [arg1:null, arg2:true]
. 调用后的地图为[arg1:null, arg2:true]
。 If you change it to params.A
, it would be like delegate.params.A
which will fail with a NullPointerException
because delegate.params
is null
. 如果您将其更改为params.A
,它会像delegate.params.A
这将失败, NullPointerException
,因为delegate.params
是null
。
To make sure the call is resolved to the owner, you could instead use this.params
(see this question/answer for meaning of this
in a Closure
): 为了确保呼叫解决车主,你也可以使用this.params
(见的意义这个问题/答案this
在Closure
):
myPipelineTemplate {
arg1 = this.params.A
arg2 = true
}
You could also change the resolution strategy: 您还可以更改解析策略:
body.resolveStrategy = Closure.OWNER_FIRST
delegate.arg1 = A
delegate.arg2 = true
I would recommend just changing your call
method from a Closure
parameter to. 我建议仅将您的call
方法从Closure
参数更改为。 any of: 任何:
Map
For example, for Map
: 例如,对于Map
:
def call(Map args) {
// ...
}
myPipelineTemplate([
arg1: params.A
arg2: true
])
Workaround: 解决方法:
properties([
parameters([
booleanParam(name: 'A', defaultValue: false, description: '')
])
])
def params = params
myPipelineTemplate {
arg1 = A
arg2 = true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.