azure-devopsyamlazure-pipelinesvariable-expansion

Variable expansion not working as expected in Azure DevOps Yaml Pipeline when stages are associated with different variable groups


I receive the error message 'Job Job1: Environment $(environmentName) could not be found. The environment does not exist or has not been authorized for use.' when I run the pipeline below.


trigger:
- main

pool:
  vmImage: ubuntu-latest

stages:
  - stage: Dev
    variables:
      - group: Config.Dev
    jobs:
      - deployment:
        environment: $(environmentName)
        strategy: 
          runOnce:
            deploy:
              steps:
              - checkout: self 
              - task: AzureCLI@2
                inputs:
                  azureSubscription: $(azureSubscriptionName)
                  scriptType: 'bash'
                  scriptLocation: 'inlineScript'
                  inlineScript: |
                    az deployment sub create --location uksouth --template-file main.bicep

  - stage: Prd
    variables:
      - group: Config.Prd
    jobs:
      - deployment:
        environment: $(environmentName)
        strategy: 
          runOnce:
            deploy:
              steps:
              - checkout: self 
              - task: AzureCLI@2
                inputs:
                  azureSubscription: $(azureSubscriptionName)
                  scriptType: 'bash'
                  scriptLocation: 'inlineScript'
                  inlineScript: |
                    az deployment sub create --location uksouth --template-file main.bicep
                


Solution

  • I solved my problem as follows.

    It relies upon Environments, Service Connections and Variable Groups being named the same.

    azure-pipelines.yml

    trigger:
    - main
    
    pool:
      vmImage: ubuntu-latest
    
    stages:
      - template: deploy.yml
        parameters: 
          environment: Dev
      - template: deploy.yml
        parameters: 
          environment: UAT
      - template: deploy.yml
        parameters: 
          environment: Prd
    

    deploy.yml

    parameters:
      - name: environment
        type: string
    
    stages:
      - stage: ${{parameters.environment}}
        variables:
          - group: Simply.Infrastructure.${{parameters.environment}}    
        jobs:
          - deployment:
            environment: ${{parameters.environment}}
            strategy: 
              runOnce:
                deploy:
                  steps:
                  - checkout: self 
                  - task: AzureCLI@2
                    inputs:
                      azureSubscription: ${{parameters.environment}}
                      scriptType: 'bash'
                      scriptLocation: 'inlineScript'
                      inlineScript: |
                        az deployment sub create --location uksouth --template-file main.bicep --parameters "{ \"environmentName\": { \"value\": \"$(environmentName)\" }, \"versionNumber\": { \"value\": \"$(versionNumber)\" } }"
    

    main.bicep

    targetScope = 'subscription'
    
    param environmentName string
    param versionNumber string
    
    resource resourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' = {
      name: 'Simply.Infrastructure.${versionNumber}'
      location: 'uksouth'
    } 
    
    module vnet './vnet.bicep' = {
      name: 'vnet'
      scope: resourceGroup   
      params: {
        name: 'smplyinf${toLower(environmentName)}${versionNumber}'
      }
    }