Pipeline Stage Uses Value From A Variable in Another Variable Group

sohlae 170 Reputation points
2025-04-03T16:12:37.38+00:00

I have 3 stages in my pipeline - one for DEV, QUA, and PRD. I added some of the steps below. As you can see, each stage is using its own variable group. I have noticed two things when I executed the pipeline:

  1. The value of deploymentEnvironment is AKS-Qua.qua for both stages.
  2. The values of aksServiceConnection and aksNamespace are both correct, that is, their values were taken from their respective variable groups.
stage: Deploy_DEV
  displayName: 'Deploy application in DEV environment'
  variables:
  - group: 'App-DEV'
  dependsOn: BuildAndPush_DEV # Stage omitted for brevity
  condition: succeeded()
  jobs:
  - template: templates/deployment-template.yaml
    parameters:
      deploymentEnvironment: $(deploymentEnvironment) # AKS-Dev.dev in App-DEV
      aksServiceConnection: $(aksServiceConnection)
      aksNamespace: $(aksNamespace)


- stage: Deploy_QUA
  displayName: 'Deploy application in QUA environment'
  variables:
  - group: 'App-QUA'
  dependsOn: BuildAndPush_QUA # Stage omitted for brevity
  condition: succeeded()
  jobs:
  - template: templates/deployment-template.yaml
    parameters:
      deploymentEnvironment: $(deploymentEnvironment) # AKS-Qua.qua in App-QUA
      aksServiceConnection: $(aksServiceConnection)
      aksNamespace: $(aksNamespace)


Why is it that the pipeline was not able to get the correct value of deploymentEnvironment when it was able to do so for the other 2 variables? I ended up hard-coding its value for it to work.

Azure DevOps
0 comments No comments
{count} votes

Accepted answer
  1. Abiola Akinbade 29,405 Reputation points Volunteer Moderator
    2025-04-03T22:44:05.71+00:00

    When you pass a variable as a parameter to a template, it gets evaluated at compile time rather than runtime. So when your pipeline is compiled, it's likely that the variable group's value for deploymentEnvironment is being used for both stages.

    Try to use stage-specific variable names in your variable groups:

    e.g:

    # In App-DEV variable group:
    # dev_deploymentEnvironment: AKS-Dev.dev
    
    # In App-QUA variable group:
    # qua_deploymentEnvironment: AKS-Qua.qua
    
    # Then in  pipeline:
    parameters:
      deploymentEnvironment: $(dev_deploymentEnvironment)  # For DEV stage
      # Or:
      deploymentEnvironment: $(qua_deploymentEnvironment)  # For QUA stage
    
    

    See: https://learn.microsoft.com/en-us/azure/devops/pipelines/library/variable-groups?view=azure-devops**
    You can mark it 'Accept Answer' and 'Upvote' if this helped you**

    Regards,

    Abiola


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.