我在 Jenkins Groovy 脚本化管道中有以下代码。
cfn_template = sh(script: "aws --region $aws_region cloudformation get-template --stack-name $asg_cfn_stack_name --query 'TemplateBody' --output json", returnStdout: true).trim()
echo "Debugging test line-2"
def min_size = sh(script: "echo '${cfn_template}' | jq -r '.Resources.devfoenergybkrblueasgASG12345678.Properties.MinSize'", returnStdout: true).trim()
第一个
sh(script: "aws ... json").trim()
工作正常,并将 CFN 模板存储到 Groovy 变量 cfn_template
中。然而,第三行给出了语法错误。
以下是相关的控制台日志行:
13:18:46 [Pipeline] sh
13:18:46 + aws --region us-east-1 cloudformation get-template --stack-name dev-fo-energy-bkr-blue-asg --query TemplateBody --output json
13:18:49 [Pipeline] echo
13:18:49 Debugging test line-2
13:18:49 [Pipeline] sh
13:18:50 /home/jenkins/agent/workspace/and_deployment_feature_AGM-448-2/cdk_build@tmp/durable-6410a3dd/script.sh: 700: Syntax error: "(" unexpected
13:18:50 [Pipeline] echo
13:18:50 Failure at CDK deployment. hudson.AbortException: script returned exit code 2
请注意,出于某种原因,我尝试从 CFN 模板获取自动缩放组 (ASG) 的
MinSize
属性,而不是从 ASG 本身获取它。
这里有什么错误,我该如何解决这个问题?
我还有一个 Groovy 变量
asg_logical_id
,它使值 devfoenergybkrblueasgASG12345678
感到痛苦。我想在现在给出语法错误的命令中使用这个变量,而不是这个直接值。我不确定在通过当前问题后我是否还面临任何其他问题。因此,如果您可以将其包含在您的解决方案中,这也会有所帮助。
更新:
将命令从代码库引入此处后添加的第一个命令
(CFN)
中的拼写错误已被删除。
以下两个在 Linux 终端上工作:
cfn_template=$(aws --region $aws_region cloudformation get-template --stack-name $asg_cfn_stack_name --query 'TemplateBody' --output json)
min_size=$(echo ${cfn_template} | jq -r '.Resources.devfoenergybkrblueasgASG12345678.Properties.MinSize')
如评论中所述,
cfn_template
的值包含'
字符——这些字符也用于尝试引用它。所以有效的调用基本上是这样的:echo ''('' | ...
.
所以你的选择是:
cfn_template
,这样当你将字符串敲在一起作为 shell 时可以使用它echo
jq
aws
的输出发送到 jq
(例如 aws ... | jq ...
)jq
调用