如何在 DSL 中将参数传递给 pipelineJob

Onu*_*alp 5 groovy jenkins jenkins-job-dsl jenkins-pipeline

我有非常相似的管道作业,唯一的区别是参数。目标是通过在 DSL 脚本中传递参数来创建这些作业,而无需任何代码重复。

我关注了这篇文章。因此,如果您在执行本文中提到的步骤后运行下面的 DSL 脚本,我的脚本就可以运行。

TL;DR 在那篇文章中添加了一个共享库,并且让 Jenkinsfile 使用该共享库。

我有一个非常相似的方法。不同之处在于我想通过 DSL 创建构建作业,并通过 DSL 上的设置更改 Jenkinsfile 的默认参数。

问题是如何传递/覆盖 Jenkinsfile 中的参数。

// BTW I'll run this code below in a loop. Open for any suggesstion 
pipelineJob('AwesomeBild') {

    description("A pipeline created by dsl")

    definition {
        cpsScm {
            scm {
                git {
                    remote { url('https://github.com/jalogut/jenkinsfile-shared-library-sample.git') }
                    branches('master')
                    // how can I pass params to the file
                    scriptPath('Jenkinsfile')
                    extensions { }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

参数效果良好。这是 DSL 文件的最新版本。

pipelineJob('AwesomeBild') {

    description("A pipeline created by dsl")


    parameters {
        stringParam( "key", "value" )
    }

    definition {
        cpsScm {
            scm {
                git {
                    remote { url('https://github.com/jalogut/jenkinsfile-shared-library-sample.git') }
                    branches('master')
                    // how can I pass params to the file
                    scriptPath('Jenkinsfile')
                    extensions { }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 -1

解决方案很简单:只需使用 $key,但保留单引号:

scriptPath('Jenkinsfile$key')
Run Code Online (Sandbox Code Playgroud)