如何将参数传递给DSL中的管道工具

时间:2018-03-13 17:19:26

标签: jenkins groovy jenkins-pipeline jenkins-job-dsl

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

我跟着this article。因此,如果在实现本文中提到的步骤后运行下面的DSL脚本,我的脚本就可以运行。

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

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

问题是我如何在Jenkins文件中传递/覆盖参数。

// 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 { }
                }
            }
        }
    }
}

修改

参数效果很好。这是最新版本的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 { }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

scriptPath('Jenkinsfile$key')