Jenkins管道将所有参数传递给下游作业

Cra*_*ues 10 jenkins jenkins-pipeline

我有一个名为buildall的管道作业,如下所示:

pipeline {
    stages {
        stage("job1") {
            build job: "job1"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

该buildall作业有25个参数.我想将所有buildall的参数传递给job1.有没有一种简单的方法可以做到这一点,而不是手动指定每个参数?

在这个问题中:管道传递参数到下游作业 一个子问题被问到但从未回答过:或者更好的是,我可以通过一种不那么麻烦的方式将所有管道参数传递给下游作业.

那是我的同一个问题.

Phi*_*lip 10

以下似乎工作(虽然我没有广泛测试):

pipeline {
    agent any
    parameters {
        string(name: 'PARAM1', description: 'Param 1?')
        string(name: 'PARAM2', description: 'Param 2?')
    }
    stages {
        stage('Example') {
            steps {
                echo "${params}"
                script {
                    def myparams = currentBuild.rawBuild.getAction(ParametersAction).getParameters()
                    build job: 'downstream-pipeline-with-params', parameters: myparams
                }    
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

缺点:要访问rawBuild和getAction,您必须在"管理Jenkins">"进程内脚本批准"下禁用Groove沙箱或批准Jenkins中的这些签名.此对话框将向您显示您可能引入了一个安全漏洞.因此,如果您想冒这个风险,这取决于您的环境.


Ako*_*kom 6

If you don't care about the parameter types, this approach does not require disabling the Groovy Sandbox - it simply assumes that all parameters can be treated as a string (won't work for "File", for example):

def myparams = params.collect{
    string(name: it.key, value: it.value)
}
build job: 'downstream-job', parameters: myparams
Run Code Online (Sandbox Code Playgroud)

It wouldn't be too hard to expand the logic to handle predefined non-string parameter types, but I agree that this should not be necessary. A better approach would be to expose parameters in the format required by the build() DSL closure, including type specifics that are currently not visible via the "params" global variable, or perhaps to add a boolean, eg:

// I wish:
build job: 'downstream-job', includeMyParameters: true, parameters: anyExtras
Run Code Online (Sandbox Code Playgroud)