【发布时间】:2021-02-08 10:14:53
【问题描述】:
当我的 azure-pipelines.yml 文件定义具有整数类型值的变量并将其作为参数传递给 AWS Cloudformation 模板时,模板无法将该参数用作数字类型。
这会导致我的 Cloudformation 堆栈失败。如何指示 Azure 管道保留整数类型?这是azure-pipelines.yml中的变量定义(有问题的是budget_amount和amount_threshold):
variables:
email_recipients: "example@gmail.com"
budget_amount: 100
amount_threshold: 80
然后将它们发送到 azure-pipelines 模板deploy_env.yml:
- template: azure-pipelines/stages/deploy_env.yml
parameters:
stage_name: deploy
aws_credentials: $(aws_dev_credentials)
aws_region: $(aws_region)
user_initials: $(user_initials)
email_recipients: $(email_recipients)
budget_amount: $(budget_amount)
amount_threshold: $(amount_threshold)
并将它们传递给 Cloudformation 模板:
- task: CloudFormationCreateOrUpdateStack@1
displayName: budgets
inputs:
awsCredentials: ${{parameters.aws_credentials}}
regionName: ${{parameters.aws_region}}
stackName: ${{parameters.stack_name}}
templateSource: 'file'
templateFile: $(Pipeline.Workspace)/${{parameters.project_name}}-templates/budgets.yml
templateParametersSource: "inline"
templateParameters: |
- ParameterKey: EmailRecipients
ParameterValue: ${{parameters.email_recipients}}
- ParameterKey: BudgetAmount
ParameterValue: ${{parameters.budget_amount}}
- ParameterKey: AmountThreshold
ParameterValue: ${{parameters.amount_threshold}}
useChangeSet: true
changeSetName: 'role-changeset'
captureStackOutputs: asVariables
captureAsSecuredVars: false
Cloudformation 模板budgets.yml:
Parameters:
EmailRecipients:
Type: String
Description: Name of the Email Recipient
BudgetAmount:
Type: Number
Default: 500
Description: Budget Amount
AmountThreshold:
Type: Number
Default: 80
Description: Budget Threshold
Resources:
BudgetExample:
Type: "AWS::Budgets::Budget"
Properties:
Budget:
BudgetLimit:
Amount: !Sub ${BudgetAmount}
Unit: USD
TimeUnit: MONTHLY
BudgetType: COST
NotificationsWithSubscribers:
- Notification:
NotificationType: ACTUAL
ComparisonOperator: GREATER_THAN
Threshold: !Sub ${AmountThreshold}
Subscribers:
- SubscriptionType: EMAIL
Address: !Sub ${EmailRecipients}
抛出的错误是:
##[error]MultipleValidationErrors: There were 2 validation errors:
* InvalidParameterType: Expected params.Parameters[1].ParameterValue to be a string
* InvalidParameterType: Expected params.Parameters[2].ParameterValue to be a string
【问题讨论】:
-
你能验证一下吗?您正在设置
variables但使用parameters访问它们。在docs 中似乎应该是variables才能访问它们? -
@Marcin 据我了解:在 Azure 管道中使用变量,在 AWS Cloudformation 中使用参数。
-
我明白了。我虽然可能
${{parameters.email_recipients}}应该是${{variables.email_recipients}}。 -
更新了问题,以更好地展示变量如何作为模板参数向下传递。
标签: amazon-web-services azure-devops azure-pipelines amazon-cloudformation azure-pipelines-release-pipeline