您可以将变量组值传递给 yaml 中的子模板吗? [英] Can you pass Variable Group values to sub template in yaml?

查看:21
本文介绍了您可以将变量组值传递给 yaml 中的子模板吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个多阶段管道,其中为管道的每个阶段定义了变量组.目标是将组中的值作为参数传递给子模板.似乎在阶段定义的 Group 的值没有传递到子模板中.它覆盖了DEFAULTVALUE"带有一个空字符串.

I'm trying to create a multi-stage pipeline which has Variable Groups defined for each stage of the pipeline. The goal is to pass values from the group as parameters to a sub template. It seems the value of the Group, defined at the stage, is not getting passed in to the sub template. It overrides the "DEFAULTVALUE" with an empty string.

pipeline.yml

trigger:
- none

pool:
  name: 'Azure Pipelines'
  vmImage: windows-latest

stages:
  - stage: DEV
    variables:
      - group: my-group-dev
    jobs:
      - template: sub-template.yml
        parameters:
          env: 'dev'
          subscriptionName: '$(SubscriptionName)' # This reference from the variable group doesn't get passed in
          subscriptionId: '$(SubscriptionId)'

  - stage: TEST
    variables:
      - group: my-group-test
    jobs:
      - template: sub-template.yml
        parameters:
          env: 'test'
          subscriptionName: '$(SubscriptionName)' # This reference from the variable group doesn't get passed in
          subscriptionId: '$(SubscriptionId)'

子模板.yml

parameters:
  env: 'DEFAULTVALUE'
  subscriptionName: 'DEFAULTVALUE'
  subscriptionId: 'DEFAULTVALUE'

jobs:
  - deployment: ResourceDeployment
    displayName: Deploy Resources ${{ parameters.env }}
    environment: ${{ parameters.env }}
    strategy:
      runOnce:
        deploy:
          steps:
            - task: AzureFileCopy@4
              displayName: 'Upload ARM Templates'
              inputs:
                sourcePath: '$(Pipeline.Workspace)/templates'
                azureSubscription: '${{ parameters.subscriptionName }}'
                destination: 'azureBlob'
                storage: 'my-storage-account'
                containerName: 'arm'
              name: AzureFileCopy

            - task: AzureResourceManagerTemplateDeployment@3
              inputs:
                deploymentScope: 'Resource Group'
                azureResourceManagerConnection: '${{ parameters.subscriptionName }}'
                subscriptionId: '${{ parameters.subscriptionId }}'
                action: 'Create Or Update Resource Group'
                resourceGroupName: 'my-resource-group'
                location: 'eastus2'
                templateLocation: 'URL of the file'
                csmFileLink: '$(AzureFileCopy.StorageContainerUri)${{ parameters.env }}/templates/main.json$(AzureFileCopy.StorageContainerSasToken)'

我也尝试在子模板中添加变量组,但也没有正确解析...

I have also tried adding the variable group within the sub-template but that also doesn't parse correctly...

有人知道这是否可能吗?

Does anyone know if this is possible?

推荐答案

在阶段级别定义的变量组中无法引用服务连接端点是一个已知问题.

It is a known issue that the service connection endpoint cannot be referenced in variable groups defined under stage level.

您可以通过以下解决方法解决此问题:

You can fix this issue by below workarounds:

1、在全局层面而不是阶段层面定义变量组见下图:

1, Define the variable groups in the global level instead of stage level See below:

trigger:
- none

pool:
  name: 'Azure Pipelines'
  vmImage: windows-latest

# define the variable group under global level.

variables:
- group: my-group-dev
- group: my-group-test


stages:
  - stage: DEV
    jobs:
      - template: sub-template.yml
        parameters:
          env: 'dev'
          subscriptionName: '$(SubscriptionName)' # This reference from the variable group doesn't get passed in
          subscriptionId: '$(SubscriptionId)'

  - stage: TEST
    jobs:
      - template: sub-template.yml
        parameters:
          env: 'test'
          subscriptionName: '$(SubscriptionName)' # This reference from the variable group doesn't get passed in
          subscriptionId: '$(SubscriptionId)'

2、链接UI页面上的变量组.

2, Link the variable groups on the UI page.

在yaml管道编辑页面-->单击 3dots--> 触发器--> 变量选项卡--> 链接变量组

On the yaml pipeline edit page--> Click the 3dots-->Triggers-->Variables Tab-->Link Variable group

有关详细信息,请参阅以下主题.

Please see below threads for more information.

使用服务连接的变量

无法通过构建定义 YAML 文件中的变量提供 Azure 订阅终结点 ID

这篇关于您可以将变量组值传递给 yaml 中的子模板吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆