Jenkins 管道共享库 - 传递参数

use*_*263 4 groovy jenkins jenkins-pipeline

我正在尝试构建一个接受参数以覆盖默认值的函数,但我一直收到“null”。

我写了一个简单的函数:

// vars/Run.groovy
def test(String type, String parallel = 'yes') {
    println(type)
    println(parallel)
}
Run Code Online (Sandbox Code Playgroud)

我的管道如下所示:

node('master') {
    Run.test('unit')
    Run.test('unit', parallel = 'no')
}
Run Code Online (Sandbox Code Playgroud)

我得到的结果是:

unit
yes

unit
null
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

Fid*_*del 5

你只需要传递值。这将覆盖您的默认值。

Run.test('unit', 'no')
Run Code Online (Sandbox Code Playgroud)

  • 但是如果你有几个可选的参数,你会怎么做?例如: def test(String type, String foo = 'bar', String parallel = 'yes') 而我只想覆盖 'parallel' ? (3认同)