简体   繁体   English

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

[英]Parameterize azureSubscription from variable group in yaml pipeline

I'm using Yaml for Azure devops pipeline.我将 Yaml 用于 Azure devops 管道。 I am using a hierarchical style for that.我为此使用了分层样式。

I'm having one top level yaml: feature.yaml which has following structure:我有一个顶级 yaml: feature.yaml 具有以下结构:

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

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

I have deploy.yaml as:我已 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

However, whenever I run this from Azure DevOps, I'm getting this error:但是,每当我从 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."

It seems pipeline is not able to resolve value of azureSubscription name from variable group.似乎管道无法从变量组中解析 azureSubscription 名称的值。

Any suggestions?有什么建议么?

I discovered that when the YAML is initially parsed it expects the variable group to be in scope.我发现最初解析 YAML 时,它期望变量组位于 scope 中。 I needed to move my variable group to the top of the YAML file and then it found the azure subscription variable.我需要将我的变量组移动到 YAML 文件的顶部,然后它找到了 azure 订阅变量。 Not what I was expecting.不是我所期待的。

for those who are looking for resolution,对于那些正在寻找解决方案的人,

Here's what you'll need to do: In the Top-level feature.yaml, Add the desired variable group name and pass in the parameter from the template which has the azure subscription name:以下是您需要做的: 在顶级功能.yaml 中,添加所需的变量组名称并从具有 azure 订阅名称的模板中传入参数:

variables:
  - group: xxx

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

And utilize that parameter in template deploy.yaml:并在模板 deploy.yaml 中使用该参数:

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

Its a known issue currently that we can not use variable from variable group directly to azureSubscription input of AzureKeyVault task as per this thread.当前的一个已知问题是,我们无法根据 线程将变量组中的变量直接用于 AzureKeyVault 任务的 azureSubscription 输入。

Thi work around should work fine!解决方法应该可以正常工作!

We had a similar issue to this and it was because we missed off the - in the yaml under variables:我们遇到了类似的问题,这是因为我们错过了 yaml 中变量下的 -:

variables:
  group: ...

To:至:

variables:
  - group: ...

This fixed the issue for us.这为我们解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM