【问题标题】:Azure Pipeline how to read libraries variables within a template loopAzure Pipeline 如何在模板循环中读取库变量
【发布时间】:2021-05-25 14:51:18
【问题描述】:

我正在尝试在模板循环中加载不同的变量组。我希望在每次迭代中阅读不同的内容,例如。 group: xadodemo-dev group(library),然后在下一次迭代中使用不同的迭代,为每个环境提供不同的上下文/值。我希望使用像 group: xadodemo-${{ environment }} 这样的构造,但它会出错:

“解析管道 YAML 时遇到错误: 作业开发:步骤输入 awsCredentials 引用了无法找到的服务连接 $(awsCredentials)。服务连接不存在或未被授权使用。授权详情请参考https://aka.ms/yamlauthz。 作业 ppd:步骤输入 awsCredentials 引用了无法找到的服务连接 $(awsCredentials)。服务连接不存在或未被授权使用。授权详情请参考https://aka.ms/yamlauthz." enter image description here

我不知道为什么这一行 environment: xadodemo-${{ environment }} 在被 ADO 渲染引擎解析时被逐字解释为 $(awsCredentials) 而不是它的库值中的 111111111111-dev-aws

作为解决方法,我将库中的值创建/处理到参数对象中并在管道内硬编码,但对于某些项目,每个环境有太多不同的值。

库名称:xadodemo-dev

awsCredentials: 111111111111-dev-aws
awsRegion: eu-west-1

库名称:xadodemo-ppd

awsCredentials: 22222222222-ppd-aws
awsRegion: us-east-1
├── ci
│   ├── awscredentials-pipeline.yaml
│   └── awscredentials.template.yaml

awscredentials-pipeline.yaml

name: awscredentials-$(Rev:r)

trigger: none

pool:
  vmImage: "ubuntu-20.04"

parameters:
- name       : environments
  displayName: Environments
  type       : object
  default    :
  - dev
  - ppd

stages:
- stage      : awscredentials
  displayName: awscredentials
  jobs       :
  - template  : awscredentials.template.yaml
    parameters:
       environments: ${{ parameters.environments }}

awscredentials.template.yaml

parameters:
- name       : environments
  displayName: 'List of environments (map)'
  type       : object

jobs:
- ${{ each environment in parameters.environments }}:
  - deployment : ${{ replace(environment,'-','_') }}
    displayName: ${{ environment }}
    environment: xadodemo-${{ environment }}

    variables:
    - group: xadodemo-${{ environment }}
    - name : ENVIRONMENT
      value: ${{ environment }}

    strategy:
      runOnce:
        deploy:
          steps:
          - checkout   : self
          - download   : none
          - script: echo awsCredentials $(awsCredentials), awsRegion $(awsRegion)
            displayName: echo
          - task       : AWSShellScript@1
            displayName: awsCredentials
            env:
              ADO_DEMO_SECRET: $(ado_demo_secret)
            inputs:
              awsCredentials      : $(awsCredentials)
              regionName          : $(awsRegion)
              failOnStandardError : false
              args                : ""
              scriptType          : inline
              inlineScript        : |
                #!/bin/bash
                echo "environment     ${{ environment }}"
                echo "awsCredentials  $(awsCredentials)"
                echo "awsRegion       $(awsRegion)"
                echo "ADO_DEMO_SECRET $ADO_DEMO_SECRET"
                env | grep ado_demo

【问题讨论】:

  • 我的问题似乎与stackoverflow.com/questions/57520028/… 有关,并且该任务的唯一服务连接输入似乎与其他输入不同。因此,我很乐意在调用模板时在参数中硬编码服务连接。然后模板在每次迭代中按预期加载库变量。

标签: azure-pipelines


【解决方案1】:

同意pio2pio,目前可行的方法是在调用模板时将服务连接硬编码到参数中。

另外,服务连接不能通过变量分阶段指定是一个已知问题,这里已经报告过:https://github.com/MicrosoftDocs/azure-devops-docs/issues/5964https://developercommunity.visualstudio.com/t/using-a-variable-for-the-service-connection-result/676259

但是根据产品组的反馈,这个功能已经在积压中了。由于其他一些优先工作,他们无法对此进行优先处理。您可以关注并投票主题:https://developercommunity.visualstudio.com/t/using-a-variable-for-the-service-connection-result/676259,或创建一个新的建议票here 来帮助跟踪这个正在进行的功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 2012-10-18
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多