amazon-web-services - 将 Azure 管道变量作为类型 : Number 的参数传递给 AWS Cloudformation 模板

标签 amazon-web-services azure-devops azure-pipelines aws-cloudformation azure-pipelines-release-pipeline

当我的 azure-pipelines.yml 文件定义具有整数类型值的变量并将其作为参数传递到 AWS Cloudformation 模板时,该模板无法将该参数用作数字类型。 这会导致我的 Cloudformation 堆栈失败。如何指示 Azure 管道保留整数类型?以下是 azure-pipelines.yml 中的变量定义(有问题的是 budget_amountamount_threshold):

variables:
  email_recipients: "exam<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5a5b9b095b2b8b4bcb9fbb6bab8" rel="noreferrer noopener nofollow">[email protected]</a>"
  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)中定义Number类型参数,并设置Number类型参数以传递Main 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: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ce9f4ede1fce0e9ccebe1ede5e0a2efe3e1" rel="noreferrer noopener nofollow">[email protected]</a>"
    
    
    
    steps: 
    
    - task: CloudFormationCreateOrUpdateStack@1
    ....

主Yml(azure-pipelines.yml):

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

  - name: amount_threshold
    type: number
    default: 80

  - name: email_recipients
    type: string
    default: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="25405d4448554940654248444c490b464a48" rel="noreferrer noopener nofollow">[email protected]</a>"


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}}

关于amazon-web-services - 将 Azure 管道变量作为类型 : Number 的参数传递给 AWS Cloudformation 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66061022/

相关文章:

amazon-web-services - 用于创建多个SQS的Cloudformation模板

Azure DevOps 管道 - 输出文件的自定义名称

azure-devops - 在 VSTS Deploy to App Center 构建任务中格式化发行说明

php - AWS-CloudWatch : InvalidSequenceTokenException

java - 根据 SQS 消息数量触发 Lambda

powershell - TFS | VSTS - 自定义构建任务执行找不到 VstsTaskSdk.psd1

Azure devops 分支控制不应验证模板文件的分支

azure-devops - 使用 REST api 批准 Azure DevOps 中的 yaml 管道部署

amazon-web-services - 同一区域、不同 VPC 中的 Amazon RDS 只读副本

sql-server - VSTS : Deploy Azure SQL DACPAC throws an error