将 Azure 管道变量作为参数传递给 AWS Cloudformation 模板,类型为:Number

Pass Azure pipeline variable to AWS Cloudformation template as parameter with type: Number

当我的 azure-pipelines.yml 文件定义一个具有整数类型值的变量并将其作为参数传递给 AWS Cloudformation 模板时,该模板无法将该参数用作数字类型。 这会导致我的 Cloudformation 堆栈失败。如何指示 Azure 管道保留整数类型?这是azure-pipelines.yml中的变量定义(有问题的是budget_amountamount_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

在Azure Devops中,变量默认为字符串类型。所以当你使用变量将值传递给参数时,值仍然是字符串类型。这可能是此问题的根本原因。

要解决这个问题,您需要使用数字类型Parameters

格式:

parameters:
- name: myNumber
  type: number
  default: 2

在你的情况下,你可以在你的Yaml模板中定义数字类型的参数(deploy_env.yml),并设置数字类型的参数来传递主Yaml中的值(azure-pipelines.yml) .

这是一个例子:

parameters:

    - name: budget_amount
      type: number
      default: 100
    
    - name: amount_threshold
      type: number
      default: 80
    
    - name: email_recipients
      type: string
      default: "example@gmail.com"
    
    
    
    steps: 
    
    - task: CloudFormationCreateOrUpdateStack@1
    ....

主Yaml(azure-pipelines.yml):

parameters:
  - name: budget_amount
    type: number
    default: 90

  - name: amount_threshold
    type: number
    default: 80

  - name: email_recipients
    type: string
    default: "example@gmail.com"


pool:
  vmImage: 'ubuntu-latest'

steps:
- template: deploy_env.yml
  parameters:
    budget_amount: ${{parameters.budget_amount}}
    amount_threshold: ${{parameters.amount_threshold}}
    email_recipients: ${{parameters.email_recipients}}