如何在jenkinsfile中定义从管道共享库继承的其他参数? [英] How can I define additional parameters in jenkinsfile who inherit from a pipeline shared lib?
问题描述
我想添加一种可能性来扩展Jenkins管道中定义的全局参数.每个调用默认管道的JenkinsFile都具有默认参数,他能够自己定义参数,如下所示:
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')
}
}
我的generic_pipeline是在共享库generic_pipeline.groovy中定义的 并且它们已经有像这样的默认参数:
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 ?
谢谢
推荐答案
我们在/vars下有一个单独的jobParams.groovy,用于设置常用参数
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
}
然后在您的Jenkins文件中,将列表与具体内容连接起来:
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)
])
这篇关于如何在jenkinsfile中定义从管道共享库继承的其他参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!