如何在每个阶段引用不同的变量组 - 环境中每个环境的阶段 - Azure Devops、模板、变量组

How do I reference a different Variable group per stage - stage per environment in environments - Azure Devops, template, variable groups

当我 运行 下面的管道时,我在 jobs.yml 内收到一个错误 - 它抱怨环境参数的“意外值”......并且在 stages.yml 内“意外值:参数” - 我在这里做错了什么?这个想法是获取环境和关联的变量组 - 循环遍历 Stages.yml 中的环境数组并为每个环境创建一个阶段...为每个阶段插入正确的变量组...使用变量组值在 [=21] 中执行作业=] - 每个变量组包含相同的 vars.. 具有不同的值。

这是main.yml

#main.yml
trigger: none

extends:
  template: /Build/Stages.yml
  parameters:
    environments:
      - environment: Dev
        variableGroup: Project-Dev-VarGrp
      - environment: QA
        variableGroup: Project-QA-VarGrp
        dependsOn: Dev
      - environment: UAT
        variableGroup: Project-UAT-VarGrp
        dependsOn: QA
      - environment: UAT2
        variableGroup: Project-UAT2-VarGrp
        dependsOn: UAT

那么接下来就是... Stages.yml

parameters:
  - name: dataFolder
    displayName: 'Data folder to process'
    type: string
    default: '/DataMigrations/Master_Data/'
  - name: dataFiles
    displayName: List of Data Files or Folder names
    type: string
    default: 'Dev_Data.zip'
  - name: environments
    type: object
    default: []
    
stages:
  - ${{ each environment in parameters.environments }}: 
    - stage: '${{ parameters.environment }}'
      jobs:
        - template: ./jobs.yml
      variables:
      - group: ${{ parameters.variableGroup }}
        parameters:
           environment: '${{ parameters.environment }}'
           crmURL: $(crmURL)
           oauthAppId: $(ClientID)
           ClientPass: $(ClientPass)
           dataFolder: '${{ parameters.dataFolder }}'
           env: '${{ parameters.environment }}'

这里是jobs.yml

    jobs:
  - deployment: DeployData
    displayName: 'Deploying Data to ${{ parameters.environment }}'
    environment: ${{ parameters.environment }}
    pool:
      vmImage: 'windows-latest'
    strategy:
      runOnce:
        deploy:
          steps:
          - checkout: self
            clean: false
          - powershell: | 
              Write-Host "##vso[task.setvariable variable=crmConnectionString]'AuthType=ClientSecret;Url=$(crmURL);ClientId=$(ClientID);ClientSecret=$(ClientPass)'"
            displayName: 'PreDeploy configuration'
          - task: PowerShell@2
            displayName: 'Powershell: Run update-data.ps1'
            inputs:
              targetType: 'filePath'
              filePath: $(System.DefaultWorkingDirectory)/DataMigrations/Scripts/update-data.ps1
              arguments: -folderName '${{ parameters.dataFolder }}' -environment '${{ parameters.env }}'
              workingDirectory: $(System.DefaultWorkingDirectory)/DataMigrations
          - task: PowerShell@2
            displayName: 'Powershell: Run zip-import-data.ps1'
            inputs:
              targetType: 'filePath'
              filePath: $(System.DefaultWorkingDirectory)/DataMigrations/Scripts/zip-import-data.ps1
              arguments: -folderName '${{ parameters.dataFolder }}' -dataMigrationFilenames '${{ parameters.dataFiles }}' -connectionString $(crmConnectionString)
              workingDirectory: $(System.DefaultWorkingDirectory)/DataMigrations

这里有几件事:

  1. 当您使用 ${{ each environment in parameters.environments }} 时,嵌套的 environmentvariableGroup 可使用此语法 ${{ environment.environment }}${{ environment.variableGroup}}

  2. 在您的 Stages.yml 文件中,您试图调用 ./jobs.yml 模板,但相关参数是在 - group: ${{ parameters.variableGroup }} 之后定义的。有效语法应如下所示:

    stages:
    - ${{ each environment in parameters.environments }}: 
      - stage: '${{ environment.environment }}'
        variables:
        - group: ${{ environment.variableGroup }}
        jobs:
        - template: ./jobs.yml    
          parameters:
            environment: '${{ environment.environment }}'
            crmURL: $(crmURL)
            oauthAppId: $(ClientID)
            ClientPass: $(ClientPass)
            dataFolder: '${{ parameters.dataFolder }}'
            env: '${{ environment.environment }}'
    

您也有一些 space 错别字。我知道这很烦人,但你需要有准确的 yaml 语法,否则文件无法解析。