将Azure管道变量作为参数传递给AWS Cloudformation模板,其类型为:Number [英] Pass Azure pipeline variable to AWS Cloudformation template as parameter with type: Number

本文介绍了将Azure管道变量作为参数传递给AWS Cloudformation模板,其类型为:Number的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When my azure-pipelines.yml file defines and passes down a variable with an integer-type value to a AWS Cloudformation template as a parameter, the template fails to use that parameter as a Number type. This causes my Cloudformation stack to fail. How can I instruct the Azure pipeline to keep the integer type ? Here is the variable definitions in azure-pipelines.yml (The problematic ones are budget_amount and amount_threshold):

variables:
  email_recipients: "example@gmail.com"
  budget_amount: 100
  amount_threshold: 80

然后将它们发送到蔚蓝管道模板 deploy_env.yml :

Then sending them down to a azure-pipelines template 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模板:

And the passing of them to the Cloudformation template:

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

The Cloudformation template 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中,默认情况下,变量为字符串类型.因此,当您使用变量将值传递给参数时,该值仍然是字符串类型.这可能是此问题的根本原因.

In Azure Devops, the variable is String type by default. So when you use variable to pass the value to parameter, the value is still the string type. This could be the root cause of this issue.

要解决此问题,您需要使用数字类型

To solve this issue, you need to use the number type Parameters.

格式:

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

在这种情况下,您可以在Yaml模板(deploy_env.yml)中定义Number类型参数,并设置Number类型参数以传递Main Yaml中的值(azure-pipelines.yml).

In you case, you could define the Number type Parameters in your Yaml template(deploy_env.yml), and set the Number type Parameters to pass the value in 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: "example@gmail.com"
    
    
    
    steps: 
    
    - task: CloudFormationCreateOrUpdateStack@1
    ....

主要Yaml(azure-pipelines.yml):

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

这篇关于将Azure管道变量作为参数传递给AWS Cloudformation模板,其类型为:Number的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆