【发布时间】:2020-04-21 11:45:52
【问题描述】:
说明
我正在将我的项目 jenkinsfiles 转换为共享库,在 groovy 中创建可重用的方法。我正在使用 extended_choice_parameter 插件在 Jenkins 中进行参数化构建。我使用它来将集群、阶段、凭据等值传递给 jenkins 作业。 现在我想使用 Jenkins 共享库,我可以在其中定义带有参数的函数,并在带有参数的 jenkinsfile 中调用它们。
问题
在使用共享库作为 jenkinsFile 中“管道”内的一个块的参数时,我收到与参数函数使用相关的错误。我创建了这个文件 jobParams.groovy
List commonParams() {
//return list of parameters
def paramsList = [
choice(name: 'stage', choices: ['lab', 'dev', 'qa', 'uat', 'prod'], description: 'select stage name' ),
choice(name: 'playbook',
choices: [ '',
'Linux',
'Unix',
'CentOS',
'Ubuntu',
'Debian'
],
description: 'select the playbook to deploy'
),
credentials(name: 'sshCredentialsId', defaultValue: 'lc_ikep_ansible', credentialType: "Username with password", required: true, description: 'SSH credentials for Ansible' ) ,
string(name: 'ansibleTags', defaultValue: '', description: 'Type in ansibleTags separated by space, example: "pre-install ikep-acls"'),
string(name: 'extraVars', defaultValue: '', description: 'Type in extraVars in format: --extra-vars=\'{key1: {key2: \"value2\", key3: \"value3\"}}\''),
choice(name: 'verboseLogging', choices: ['', '-v', '-vvv', '-vvvv'], description: 'Select ansible verbose logging'),
booleanParam( name: 'forceDeploy',
defaultValue: false,
description: 'Activate to force deployment for non-master branches'
)
]
return paramsList
}
现在我在 jenkinsfile 中调用这个列表,因为这些参数是在我们单击 jenkins 中的 BUILD 之前生成的。 使用以下代码,我可以打印上面 LIST 中定义的所有参数。但是现在我如何使用它作为参数在 Jenkins 中生成构建参数。
/* Pipeline */
def commonParams = jobParams.commonParams()
String listAsString = "[\"${commonParams.join('", "')}\"]"
echo "printing variables ${listAsString}"
请提出建议,因为我在搜索互联网时没有找到有效的用例。在 jenkins 文件中的管道块内我尝试了以下操作,但它给出了错误说明
pipeline {
agent { label "ansibleDeployments"}
options {
ansiColor('xterm')
disableConcurrentBuilds()
}
environment {
ansibleTags = 'all'
}
parameters {
(listAsString)
}
错误
WorkflowScript: 67: Expected a build parameter definition @ line 67, column 5.
(listAsString)
^
【问题讨论】:
标签: jenkins-pipeline shared-libraries jenkins-groovy