azure 你能在yaml中传递变量组值到子模板吗?

jvlzgdj9  于 2023-10-22  发布在  其他
关注(0)|答案(2)|浏览(128)

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

管道.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)'

sub-template.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)'

我也尝试在子模板中添加变量组,但也不能正确解析。
有人知道这是否可能吗?

ejk8hzay

ejk8hzay1#

服务连接端点不能在阶段级别下定义的变量组中引用,这是一个已知的问题。
您可以通过以下解决方法解决此问题:
1、在全局级别而不是阶段级别定义变量组,如下所示:

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页面链接变量组。
在yaml管道编辑页面上-->单击3dots-->Triggers-->Variables Tab-->Link Variable组

请参阅下面的线程了解更多信息。
Using a variable for the service connection
Azure subscription endpoint ID cannot be provided through a variable in build definition YAML file

8wtpewkr

8wtpewkr2#

你需要将变量组名传递给模板,并在模板级别上将其应用于变量作用域,如下所示:

parameters:
  env: 'DEFAULTVALUE'
  subscriptionName: 'DEFAULTVALUE'
  subscriptionId: 'DEFAULTVALUE'
  variableGroupName: 'DEFAULTVALUE`

jobs:
  - deployment: ResourceDeployment
    displayName: Deploy Resources ${{ parameters.env }}
    environment: ${{ parameters.env }}
    variables:
      - group: {{ paramaters.variableGroupName }}
    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)'

类似于this topic
出于安全原因,我们只允许您通过显式参数将信息传递到模板代码中。https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops
这意味着使用您的模板的管道的作者需要提交更改,其中他们显式地将所需信息传递到模板代码中。
有一些例外,变量在同一个文件中或管道编译时静态定义,但一般来说,对于不涉及系统定义的只读动态变量和自定义动态输出变量的所有内容,使用参数可能更好。
我必须把变量组

  • QA
  • PROD

都有isProd变量
然后我的模板是

parameters:
  variableGroupName: 'QA'

jobs:
- job: ResourceDeployment
  variables:
  - group: ${{ parameters.variableGroupName }}
  steps:
  - script: echo '$(isProd)'

主文件是

trigger: none
pr: none

stages:
- stage: QA
  jobs:
  - template: sub-template.yml
    parameters:
      variableGroupName: 'QA'

- stage: PROD
  jobs:
  - template: sub-template.yml
    parameters:
      variableGroupName: 'PROD'

我得到了:

相关问题