0

I want to get the environments and the associated variableGroup - loop thru the environments array in Stages.yml and create a stage per environment... insert correct variableGroup per stage... use variablesGroup values to perform jobs in jobs.yml - each variable group contains the same vars.. with different values.

`` I have a main.yml file in which I reference my environments and the associated group variable

main.yaml :

 name: $(BuildID)
    
    
    trigger: none
    
    pool:
      vmImage: 'leetchi-agentspool-windows'
    
    
    variables:
      - template: vars/variables-api.yaml
    
    extends:
        template: deploy-stages.yaml
        parameters:
          environements:
            - environement: 'DV1'
              variableGroupName: tf-dv1
            - environement: 'IN1'
              variableGroupName: tf-in1

A stage template which is supposed to apply with the environment parameters and the group variable.

stage.yaml :

    ---
    parameters:
      - name: environements
        type: object
        default: []
    
    stages:
      - ${{ each environement in parameters.environements }}: 
      - stage: 'Deploy to ${{ environement.environements }}'
        variables:
        - group: ${{ environement.variableGroup }}
        jobs:
          - template: template-terraform-deployment.yml
            parameters:
              environement: ${{environement.environements}}
              project: $(project)
              definition: $(def)
              project1: $(project1)
              definition1: $(def1)
              stateKey: ${{environement.environement}}$(tfkey)
              TerraformDirectory: $(TfDirectory)
              azureSubscription: $(service_connection_name)
              TfStateResourceGroupName: $(tf_resource_group)
              TfStateStorageAccountName: $(tf_storage_account)
              TStateContainerName: $(tf_container)
              commandOptions: -out=$(System.DefaultWorkingDirectory)/terraform.tfplan -detailed-exitcode

 

I start in YAML. Could you help me please? Thank you very much in advance

LazPnt
  • 1

1 Answers1

0

You can modify your main.yaml as below:

trigger: none

pool:
  vmImage: 'leetchi-agentspool-windows'


variables:
  - template: vars/variables-api.yaml

extends:
    template: deploy-stages.yaml
    parameters:
    - name: environment
      values:
      - DV1
      - IN1
    - name: variableGroupName
      values:
      - tf-dv1
      - tf-in1

stage.yaml :

stages:
  - stage: 'Deploy to ${{ parameters.environment }}'
    variables:
    - group: ${{ parameters.variableGroupName}}
    jobs:
      - template: template-terraform-deployment.yml
        parameters:
          environment: ${{ parameters.environment }}
          project: $(project)
          definition: $(def)
          project1: $(project1)
          definition1: $(def1)
          stateKey: ${{ parameters.environment }} + $(tfkey)
          TerraformDirectory: $(TfDirectory)
          azureSubscription: $(service_connection_name)
          TfStateResourceGroupName: $(tf_resource_group)
          TfStateStorageAccountName: $(tf_storage_account)
          TStateContainerName: $(tf_container)
          commandOptions: -out=$(System.DefaultWorkingDirectory)/terraform.tfplan -detailed-exitcode
Vita Zhang-MSFT
  • 246
  • 1
  • 4