jenkins - 在管道中使用参数时 Jenkins 语法错误
问题描述
我有一个詹金斯设置,每个节点都有一个用于 Prod 和 UAT 的从节点。我需要运行一个作业,如果用户选择 uat1 或 uat2 中的任何一个,该作业就会在 UAT 节点上运行。如果用户选择 prod,它应该在 Prod 节点中运行作业。该工作涉及 1. 从 git 下载执行代码 2. 根据选择的是 UAT 还是 Prod,ec2 实例应承担适当的角色
在下面的代码中,我尝试在主节点上运行设置作业参数的第一阶段以及节点选择。此后,我在脚本中使用了一个脚本来为 agentLabel 变量提供一个值,以便在进一步的步骤中使用,以便作业在适当的 nde 上运行。
pipeline {
agent {
label 'master'
}
stages {
stage('Get Inputs') {
steps {
parameters {
string(
name: 'branch',
defaultValue: 'master',
description: 'Branch of code to be deployed'
)
choice(
name: 'service',
choices: 'calcs-service\naudits-service\nrate-alerts',
description: 'Service to be cleaned'
)
choice(
name: 'environment',
choices: 'uat1\nuat2\nprod',
description: 'Environement whose services need cleanup'
)
}
script {
switch("${environment}") {
case uat1:
agentLabel = 'uat'
case uat2:
agentLabel = 'uat'
case prod:
agentLabel = 'prod'
}
}
}
}
stage('Clone cleanup_lambda_functions') {
agent {
label "${agentLabel}"
}
steps {
checkout([$class: 'GitSCM',
branches: [[name: "*/${branch}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[url: 'git@bitbucket.org:canstar-dev/devops.git']]])
}
}
stage('Clean up Lambda un UAT') {
agent {
label "${agentLabel}"
}
when {
expression {"${agentLabel}" == 'uat'}
}
steps {
withAWS(role: 'arn:aws:iam::000000000006:role/cns-iam-role-delete-lambda-version') {
ansiColor('xterm') {
sh '''
set -x
set -eo pipefail
echo "Assumed Support role for:"
echo "${environment}"
'''
}
}
}
}
stage('Clean up Lambda un Prod') {
agent {
label "${agentLabel}"
}
when {
expression {"${agentLabel}" == 'prod'}
}
steps {
withAWS(role: 'arn:aws:iam::0000000000005:role/cns-iam-role-delete-lambda-version') {
ansiColor('xterm') {
sh '''
set -x
set -eo pipefail
echo "Assumed Support role for Prod"
'''
}
}
}
}
}
post {
cleanup {
cleanWs()
}
}
}
但这因错误而失败:
org.codehaus.groovy.control.MultipleCompilationErrorsException: 启动失败: WorkflowScript: 9: Missing required parameter: "parameterDefinitions" @ line 9, column 17. parameters { ^
WorkflowScript:10:缺少必需参数:“trim”@ 第 10 行,第 21 列。字符串(^
2 个错误
我对编写 jenkinsfiles 非常陌生,所以如果有人能指出我正确的方向,那将非常有帮助。
解决方案
必须在parameters
管道范围内声明,而不是在单个阶段的范围内声明。(来源)。
以下应该修复您的语法错误:
pipeline {
agent {
label 'master'
}
parameters {
string(
name: 'branch',
defaultValue: 'master',
description: 'Branch of code to be deployed'
)
choice(
name: 'service',
choices: 'calcs-service\naudits-service\nrate-alerts',
description: 'Service to be cleaned'
)
choice(
name: 'environment',
choices: 'uat1\nuat2\nprod',
description: 'Environement whose services need cleanup'
)
} // close parameters
stages {
stage('Get Inputs') {
...
推荐阅读
- node.js - 退出代码 127 - 未知浏览器查询 `basedir=$(dirname "$(echo "$0" | sed -e 's`
- sql - 汇总分组中子查询的结果
- r - 删除列表中矩阵的 NA 行
- c - 使用 gcc target_clones 函数多版本控制静态链接存档时如何修复未定义的引用链接器错误
- angular - Angular 属性指令获取对其他同级属性引用的引用
- php - 使用php以excel xml格式导出期间增加行限制
- typescript - 如何忽略“类型实例化过深并且可能无限。ts(2589)”打字稿错误?
- html - 如何使 flex 子元素像内联元素一样换行到下一行
- android - 是否应授权测试人员进行内部应用共享以使用该链接安装应用?
- scala - Spark 2.2 的 SBT 编译失败