【问题标题】:Passing an object into a template in Azure DevOps pipeline将对象传递到 Azure DevOps 管道中的模板
【发布时间】:2021-02-26 19:18:18
【问题描述】:

我正在尝试重构在大约 100 个帐户上运行的 Azure DevOps 作业,目前这些是管道中几乎相同的单个任务。他们碰巧在 AWS 中调用了一些 Cloud Formation,但这并不重要。

我想在管道中有一个模板参数,例如:-

parameters:
- name: stackparams
  type: object
  default:
    - displayname: AWS Test User Account
      awscredentials: TESTAWS-appaccounttest1-AzureDevOps
      templatefile: ./Test/TestApp/SwitchRolesGroupsPolicies.yml
      templateparametersfile: ./Test/LZTestApp/demoparams.json
    - displayname: Second Account
      awscredentials: TESTAWS-appaccounttest2-AzureDevOps
      templatefile: ./Test/Something/SwitchRolesGroupsPolicies.yml
      templateparametersfile: ./Test/Something/demoparams.json

在模板中类似:-

parameters:
- name: stackparams
  type: object

steps:
- ${{ each stackparam in parameters.stackparams }}:
  - checkout: self
  - task: AWSShellScript@1
    inputs:
      awsCredentials: ${{ stackparams.awscredentials }}
      regionName: 'eu-west-1' 
      scriptType: 'inline'
      inlineScript: |
        echo "\$STACKPARAMS_DISPLAYNAME: $STACKPARAMS_DISPLAYNAME"
    displayName: 'This is a test - ${{ stackparams.displayname }}'

但是,我不知道这是否可能,而且很少有关于这样做的文档。我暂时只用 AWSShellScript 替换了 Cloud Formation,只是为了证明概念。我目前遇到的错误是:-

> /Templates/CfnUpdateStack/CfnUpdateStack.yml (Line: 25, Col: 23):
> Unrecognized value: 'stackparams'. Located at position 1 within
> expression: stackparams.awscredentials. For more help, refer to
> https://go.microsoft.com/fwlink/?linkid=842996
> /Templates/CfnUpdateStack/CfnUpdateStack.yml (Line: 33, Col: 18):
> Unrecognized value: 'stackparams'. Located at position 48 within
> expression: format('Collect Cost Optimisation Data - {0}',
> stackparams.displayname). For more help, refer to
> https://go.microsoft.com/fwlink/?linkid=842996
> /Templates/CfnUpdateStack/CfnUpdateStack.yml (Line: 25, Col: 23):
> Unrecognized value: 'stackparams'. Located at position 1 within
> expression: stackparams.awscredentials. For more help, refer to
> https://go.microsoft.com/fwlink/?linkid=842996
> /Templates/CfnUpdateStack/CfnUpdateStack.yml (Line: 33, Col: 18):
> Unrecognized value: 'stackparams'. Located at position 48 within
> expression: format('Collect Cost Optimisation Data - {0}',
> stackparams.displayname). For more help, refer to
> https://go.microsoft.com/fwlink/?linkid=842996

【问题讨论】:

    标签: azure-devops


    【解决方案1】:

    是的,这是可能的。你快到了。您的模板中有一个小错误。 ${{ stackparams.awscredentials }} 应该是 ${{ stackparam.awscredentials }}。

    如果您使用相同的源代码库,则无需在循环中包含- checkout: self。因为任务在同一个作业中运行。源代码库只能签出一次。请参阅下面的完整示例:

    #azure-pipeline.yaml
    
    parameters:
    - name: stackparams
      type: object
      default:
        - displayname: AWS Test User Account
          awscredentials: TESTAWS-appaccounttest1-AzureDevOps
          templatefile: ./Test/TestApp/SwitchRolesGroupsPolicies.yml
          templateparametersfile: ./Test/LZTestApp/demoparams.json
        - displayname: Second Account
          awscredentials: TESTAWS-appaccounttest2-AzureDevOps
          templatefile: ./Test/Something/SwitchRolesGroupsPolicies.yml
          templateparametersfile: ./Test/Something/demoparams.json
    
    trigger: none
    pool: 
      vmImage: windows-latest
    
    steps:
    - template: stepsTemplate.yml
      parameters:
        stackparams: ${{parameters.stackparams}}  #pass the parameters to template parameters
    

    --

    #stepsTemplate.yml
    
    parameters:
    - name: stackparams
      type: object
    
    steps:
    - ${{ each stackparam in parameters.stackparams }}:
      
      - task: AWSShellScript@1
        inputs:
          awsCredentials: ${{ stackparam.awscredentials }}
          regionName: 'eu-west-1' 
          scriptType: 'inline'
          inlineScript: |
            echo "\$STACKPARAMS_DISPLAYNAME: $STACKPARAMS_DISPLAYNAME"
        displayName: 'This is a test - ${{ stackparam.displayname }}'
    

    【讨论】:

    • 谢谢你,这是一个愚蠢的错误(在你知道我猜的答案之前,他们都是这样)
    猜你喜欢
    • 2022-06-29
    • 1970-01-01
    • 2021-08-09
    • 2020-03-23
    • 2021-12-02
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    相关资源
    最近更新 更多