简体   繁体   English

无法从模板化管道参数访问Jenkins参数

[英]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策略,并且delegateMap 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 = [:] . 其中delegatedef 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.paramsnull

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 (见的意义这个问题/答案thisClosure ):

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: 任何:

  1. Map
  2. Domain specific object for your pipeline 管道的域特定对象
  3. Multiple arguments 多个参数

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.

相关问题 从模板化父级访问子typedef - Accessing child typedef from templatized parent C ++模板:何时在模板化结构中包含模板参数? - C++ Templates: When to include template parameters in templatized struct? 从 YAML 管道到模板文件的 Azure Pipeline 动态参数 - Azure Pipeline dynamic parameters to template file from YAML pipeline 成员访问不完整类型错误:模板化结构C ++ - Member access into incomplete type error: Templatized struct C++ 如何在 c++ 的多个位置访问 class 的模板化 static 变量 - How to access templatized static variable of a class in multiple places in c++ 访问另一个类中的模板化嵌套类函数和对象 - access a templatized nested class function and object in another class 如何在OpenCV中访问/修改矩阵元素? 为什么在()被模板化了? - How to access/modify matrix element in OpenCV? Why at() is templatized? 为什么不将带有默认参数的 function 传递给模板化构造函数并使用 lambda 将其存储为 std::function<void()> 工作?</void()> - Why doesn't passing a function with default parameters to a templatized constructor and storing it using a lambda as std::function<void()> work? Jenkins管道:使用变量模板化文件 - Jenkins pipeline : templating a file with variables 为什么模板化的派生类可以在gcc上访问其基本私有成员? - Why can a templatized derived class access its base private members on gcc?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM