How can I define additional parameters in jenkinsfile who inherit from a pipeline shared lib?

偶尔善良 提交于 2019-12-23 23:12:22

问题


I would like to add a possibility to extends the global parameters define in a Jenkins pipeline. Each JenkinsFile who call the default pipeline have default parameters and he as able to define parameters himself like this:

@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')
            }

}

my generic_pipeline is define in a shared lib generic_pipeline.groovy and they have already default parameters like this:

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')
        }
    }
}

How can I do that ? how can I define additional parameters for the inheritances ?

Thank you


回答1:


We have a separate jobParams.groovy under /vars that sets common params

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
}

Then in your Jenkinsfile, just concatenate the list with the specifics:

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)
    ])


来源:https://stackoverflow.com/questions/51382761/how-can-i-define-additional-parameters-in-jenkinsfile-who-inherit-from-a-pipelin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!