3

我想增加一种扩展 Jenkins 管道中定义的全局参数的可能性。每个调用默认管道的 JenkinsFile 都有默认参数,他可以自己定义参数,如下所示:

@Library('mylib') _ 
generic_pipeline {

 parameters {
            choice(choices: namespaces, description: 'namespaces ?', name: 'namespaceIdChoice')
            string(defaultValue: "$BRANCH_NAME-$BUILD_NUMBER", description: 'What is the project name ?', name: 'projectName')
            }

}

我的 generic_pipeline 是在共享库 generic_pipeline.groovy 中定义的,它们已经具有如下默认参数:

def call(Closure body) {
    def params = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = params
    body()


    pipeline {

        agent {
            label 'master'
        }

        parameters {
            string(defaultValue: "defaultParam2", description: 'What is defaultParam2 ?', name: 'defaultParam2')
        }
    }
}

我怎样才能做到这一点 ?如何为继承定义附加参数?

谢谢

4

1 回答 1

7

我们在 /vars 下有一个单独的 jobParams.groovy 来设置常用参数

List commonParams() {
     //return list of parameters
     def paramsList = [
         choice(name: 'ACCOUNT_NAME', choices: ['account1', 'account2'].join('\n'),  description: 'Account Name'),
         choice(name: 'AWS_REGION', choices: PipelineUtils.regions.join('\n'), description: 'AWS Region to build/deploy'),
    ]

     return paramsList
}

然后在您的 Jenkinsfile 中,将列表与具体信息连接起来:

List commonParams = jobParams.commonParams()
properties([
        buildDiscarder(logRotator(numToKeepStr: '20')),
        parameters([
            choice(name: 'MY_SPECIFIC_PARAM', choices: ['1', '2', '3'].join('\n'), description: ''),
            choice(name: 'PARAM2', choices: ['value'].join('\n'), description: ''),
        ] + commonParams)
    ])
于 2018-07-18T06:53:33.123 回答