从 yaml 管道中的变量组参数化 azureSubscription

Parameterize azureSubscription from variable group in yaml pipeline

我正在为 Azure devops 管道使用 Yaml。 我为此使用了分层样式。

我有一个顶级 yaml:feature.yaml 具有以下结构:

trigger:
...
pool:
  vmImage: ...
  
variables:
  group: ...

stages:
  - template: deploy.yaml
    parameters:
      subName: $(subscription) #This should be taken from Variable group

我有 deploy.yaml 作为:

stages:
    - stage: deploy
      jobs:
        - job: deploy
          steps:
          - task: AzureKeyVault@1
            inputs:
              azureSubscription: $(paramaters.subName) #This should be resolved from parameter passed form feature.yaml
              KeyVaultName: ...
              SecretsFilter: '*'
              RunAsPreJob: true

但是,每当我从 Azure DevOps 运行 执行此操作时,我都会收到此错误:

There was a resource authorization issue: "The pipeline is not valid. Job deploy: Step AzureKeyVault input ConnectedServiceName references service connection $(paramaters.subName) which could not be found. The service connection does not exist or has not been authorized for use. For authorization details, refer to https://aka.ms/yamlauthz."

管道似乎无法从变量组中解析 azureSubscription 名称的值。

有什么建议吗?

我认为你必须在 deploy.yaml!

中定义参数

https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=example%2Cparameter-schema

对于那些正在寻找解决方案的人,

您需要执行以下操作: 在顶层 feature.yaml,添加所需的变量组名称并从具有 azure 订阅名称的模板中传入参数:

variables:
  - group: xxx

  - template: deploy.yaml
    parameters:
      subName: $(_subscriptionName)

并在模板中使用该参数 deploy.yaml:

- task: AzureKeyVault@1
  inputs:
     azureSubscription: ${{ parameters.subName}}
     keyVaultName: xxx
     secretsFilter: '*'

我们不能根据 this 线程将变量组中的变量直接用于 AzureKeyVault 任务的 azureSubscription 输入,这是目前的一个已知问题。

这个解决方法应该没问题!

我们遇到了与此类似的问题,这是因为我们错过了变量下的 yaml 中的 -:

variables:
  group: ...

收件人:

variables:
  - group: ...

这为我们解决了问题。

我发现当最初解析 YAML 时,它希望变量组在范围内。我需要将我的变量组移动到 YAML 文件的顶部,然后它找到了 azure 订阅变量。出乎我的意料。