在Azure DevOps管道中将对象传递到模板

4
我是一名有用的翻译助手。需要翻译的内容与IT技术相关。以下是您需要翻译的文本:

我正在尝试重构在大约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 }}'

然而,我不知道这是否可能,并且关于以这种方式进行操作的文档非常少。目前,我已经将Cloud Formation暂时替换为AWSShellScript,仅仅是为了证明概念的可行性。我目前遇到的错误是:

> /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
1个回答

4

是可以的。您已经接近成功了。您的模板中有一个小错误。${{ 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 }}'

谢谢,那个是个愚蠢的错误(就像所有的错误一样,直到你知道答案才明白)。 - Steve Button
我使用参数stackparams传递了-name和默认值。这样不起作用。谢谢你的示例! - Martin Dekker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接