如何有多个 foreach 迭代 azurepipeline yaml?

Vow*_*eee 1 azure-pipelines azure-pipelines-release-pipeline azure-pipelines-yaml

在azuredevops yaml管道中,我们可以嵌套forloop吗?或多个for循环。

我的要求是通过映射的变量值迭代管道,这应该跨环境完成。

例如:在我的 ADO 存储库中,每个应用程序有 10 个文件夹,每个应用程序中每个环境有 4 个不同的变量模板文件,即“vars-development.yaml、vars-staging.yaml、vars-prod.yaml、vars-dr” .yaml”

我需要的是在每个环境特定变量模板中,我需要有不同的警报规则输入(每个环境会有多个警报)。变量.development.yaml

variables:
  alert1:
     environment: development
     subscription: 'mysubscription'
     resourceGroup: myrg
     AlertRuleName: myalertrule
     query:myquery
     timeWindowInMinutes: 1    
     severity: 1
     operator: GreaterThanOrEqual
     threshold: 20
     
  alert2:
     environment: development
     subscription: 'mysubscription'
     resourceGroup: myrg
     AlertRuleName: myalertrule
     query:myquery
     timeWindowInMinutes: 1    
     severity: 1
     operator: GreaterThanOrEqual
     threshold: 20
     autoMitigate: false
     
etc.............

  alertn: 
Run Code Online (Sandbox Code Playgroud)

azure-pipeline.yaml

name: $(Build.SourceBranchName)-$(Build.BuildId)
trigger: none

parameters:
- name: appname
  type: string
  values:
    - app1
    - app2
    - app3
    - app4
    - app5
    - app5
stages:
  - template: loganalytic-alertrulet.yaml
    parameters:
      rootModuleFolder: ${{ parameters.appname }}
      envList:
        - development
        - staging
        - dr
        - production
Run Code Online (Sandbox Code Playgroud)

loganalytic-alertrulet.yaml

parameters:
- name: appname
  type: string
  default: ""
- name: envList
  type: object
- name: rootModuleFolder
  type: string

stages:
- stage: BuilD_ARM_LogAnalytic_Artifact
  displayName: 'Build and validate Query Based Alert for aggregator Count'
  jobs:
    - ${{ each environment in parameters.envList }}:
      - job: BuilD_ARM_LogAnalytic_Artifact_${{ environment }}
        variables:
          - group: ${{ environment }}-alert-config
          - template: ../${{ parameters.rootModuleFolder }}/vars-${{ environment }}.yaml      
        workspace:
          clean: all
        pool:
          ${{ if eq(environment, 'development')}}:
            name: devpool
          ${{ if eq(environment, 'staging')}}:
            name: stagepool
          ${{ if eq(environment, 'production')}}:
            name: prodpool
          ${{ if eq(environment, 'dr')}}:
            name: drpool               
        steps:
          - ${{ each alert in variable.alertList }}:        
            - task: qetza.replacetokens.replacetokens-task.replacetokens@3
              inputs:
                rootDirectory: '$(System.DefaultWorkingDirectory)/'
                targetFiles: '$(System.DefaultWorkingDirectory)/logsearch-numberofresult/*.json'
                encoding: 'auto'
                writeBOM: true
                actionOnMissing: 'warn'
                keepToken: false
                tokenPrefix: '#{'
                tokenSuffix: '}#'
                useLegacyPattern: false
                enableTelemetry: true
            - task: AzureCLI@2
              displayName: "validate the templates"
              inputs:
                azureSubscription: ${{ variables.subscription }}
                scriptType: 'bash'
                scriptLocation: 'inlineScript'
                inlineScript: 'az group deployment validate --resource-group ${{ variables.resourceGroup }} --template-file $(System.DefaultWorkingDirectory)/logsearch-numberofresult/numofresult_template.json --parameters $(System.DefaultWorkingDirectory)/ogsearch-numberofresult/numofresulparam.json'
            - task: AzureCLI@2
              displayName: "verify the change result"
              inputs:
                azureSubscription: ${{ variables.subscription }}
                scriptType: 'bash'
                scriptLocation: 'inlineScript'
                inlineScript: 'az deployment group what-if --resource-group ${{ variables.resourceGroup }} --template-file $(System.DefaultWorkingDirectory)/logsearch-numberofresult/numofresult_template.json --parameters $(System.DefaultWorkingDirectory)/logsearch-numberofresult/numofresulparam.json'          
            - task: PublishBuildArtifacts@1
              inputs:
                PathtoPublish: '$(System.DefaultWorkingDirectory)/05-logsearch-numberofresult/'
                ArtifactName: 'arm-source-${{ variables.environment }}'
                publishLocation: 'Container'
              name: "Publish_arm_code"
              displayName: "Publish arm code as build artifact"
    
- ${{ each environment in parameters.envList }}:         
    - stage: Create_LogSearch__Alert_${{ environment }}
      displayName: 'Create LogSearch Count Aggregation alert Alert ${{ environment }}'
      condition: succeeded()
      variables:
        - group: ${{ environment }}-alert-config
        - template: ../${{ parameters.rootModuleFolder }}/vars-${{ environment }}.yaml
      jobs:     
      - deployment: deploy_Alert_${{ environment }}
        ${{ if eq(variables.subscription, 'NonProd') }}:
          environment: DEV        
        ${{ if eq(variables.subscription, 'Test') }}:    
          environment: DEV
        ${{ if eq(variables.subscription, 'PROD') }}:
          environment: PROD
        ${{ if eq(variables.subscription, 'DR') }}:    
          environment: DR              
        displayName: "Creating ALert in ${{ environment }}"
        pool:
          ${{ if eq(variables.subscription, 'NonProd') }}:          
            name: devpool
          ${{ if eq(variables.subscription, 'Test') }}:
            name: stagepool
          ${{ if eq(variables.subscription, 'PROD') }}:          
            name: prodpool
          ${{ if eq(variables.subscription, 'DR') }}:
            name: drpool                
        strategy:
          runOnce:
            deploy:
              steps:
              - task: DownloadBuildArtifacts@0
                inputs:
                  artifactName: 'arm-source-${{ environment }}'
                  downloadPath: $(System.ArtifactsDirectory)              
              - task: CopyFiles@2
                inputs:
                  sourceFolder: $(System.ArtifactsDirectory)/arm-source-${{ environment }}
                  contents: '**'
                  targetFolder: $(System.DefaultWorkingDirectory)/arm-source-${{ environment }}
                  cleanTargetFolder: true                   
              - task: AzureResourceGroupDeployment@2
                ${{ if eq(environment, 'development') }}:              
                  inputs:
                    azureSubscription: 'NonProd'
                    action: 'Create Or Update Resource Group'
                    resourceGroupName: ${{ variables.resourcegroup }}
                    location: 'US'
                    templateLocation: 'Linked artifact'
                    csmFile: '$(System.DefaultWorkingDirectory)/arm-source-development/numofresult_template.json '
                    csmParametersFile: '$(System.DefaultWorkingDirectory)/arm-source-development/numofresulparam.json'
                ${{ if eq(environment, 'staging') }}:   
                  inputs:
                    azureSubscription: 'NonProd'
                    action: 'Create Or Update Resource Group'
                    resourceGroupName: ${{ variables.resourcegroup }}
                    location: 'US'
                    templateLocation: 'Linked artifact'
                    csmFile: '$(System.DefaultWorkingDirectory)/arm-source-staging/numofresult_template.json '
                    csmParametersFile: '$(System.DefaultWorkingDirectory)/arm-source-staging/numofresulparam.json'
                ${{ if eq(environment, 'production') }}:   
                  inputs:
                    azureSubscription: 'PROD'
                    action: 'Create Or Update Resource Group'
                    resourceGroupName: ${{ variables.resourcegroup }}
                    location: 'West Europe'
                    templateLocation: 'Linked artifact'
                    csmFile: '$(System.DefaultWorkingDirectory)/arm-source-production/numofresult_template.json '
                    csmParametersFile: '$(System.DefaultWorkingDirectory)/arm-source-production/numofresulparam.json'
                ${{ if eq(environment, 'dr') }}:   
                  inputs:
                    azureSubscription: 'DR'
                    action: 'Create Or Update Resource Group'
                    resourceGroupName: ${{ variables.resourcegroup }}
                    location: 'US'
                    templateLocation: 'Linked artifact'
                    csmFile: '$(System.DefaultWorkingDirectory)/arm-source-dr/numofresult_template.json '
                    csmParametersFile: '$(System.DefaultWorkingDirectory)/arm-source-dr/numofresulparam.json'                                           
Run Code Online (Sandbox Code Playgroud)

所以这里我使用arm模板来创建警报,并且需要跨4个环境生成这些警报规则。在 Azurepipeline yaml 中,我如何将 2 级 foreach 迭代用于警报规则以及环境范围。

小智 6

请参阅示例以了解如何在 Azure 管道中使用嵌套循环处理复杂对象

parameters:
- name: environmentObjects
  type: object
  default: 
  - environmentName: 'dev'
    regionAbrvs: ['westeurope']
  - environmentName: 'uat'
    regionAbrvs: ['france', 'uk']


steps:
- ${{ each environmentObject  in parameters.environmentObjects }}: 
  - ${{ each regionAbrv  in environmentObject.regionAbrvs }}:
    - script: echo ${{ regionAbrv  }}
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述 在此输入图像描述 在此输入图像描述

对于其他场景;

https://github.com/microsoft/azure-pipelines-yaml/blob/master/design/each-expression.md#scenarios