Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolve Azure YAML Pipeline overlapping variable names in multiple variable groups

We're working on converting our Classic Azure Pipelines to YAML Pipelines. One thing that is not clear is how to ensure that two different variable groups with variables with the same name but different meaning don't step on each other.

For example, if I have variable groups vg1 and vg2, each with variable named secretDataDestination, how do I ensure that the correct secretDataDestination is used in a YAML Pipeline?

A more concerning example is, if we initially have two variable groups without overlapping variable names, how do we ensure that adding a newly-overlapping variable name to a group doesn't replace use of the variable as originally intended?

like image 335
GaTechThomas Avatar asked Oct 27 '25 05:10

GaTechThomas


1 Answers

A workaround is leveraging output variables in Azure DevOps with some small inline PowerShell task code.

First, create 2 jobs. Each job with their own variable group, in this case Staging and Prod. Both groups contain the variables apimServiceName and apimPrefix. Add the variables as a job output by echoing them as isOutput=true like this:

      - job: StagingVars
        dependsOn:
        variables:
          - group: "Staging"
        steps:
          - powershell: >-
              echo "##vso[task.setvariable variable=apimServiceName;isOutput=true]$(apimServiceName)"
              echo "##vso[task.setvariable variable=apimPrefix;isOutput=true]$(apimPrefix)"
            name: setvarStep

      - job: ProdVars
        dependsOn:
        variables:
          - group: "Prod"
        steps:
          - powershell: >-
              echo "##vso[task.setvariable variable=apimServiceName;isOutput=true]$(apimServiceName)"
              echo "##vso[task.setvariable variable=apimPrefix;isOutput=true]$(apimPrefix)"
            name: setvarStep

Then, use the variables in a new job, where you specify a new variable name and navigate to the job output to get a value, this works because the variable groups are each placed into their own job, so they will not overwrite any variable:

      - job:
        dependsOn:
          - StagingVars
          - ProdVars
        variables:
          ServiceNameSource: "$[ dependencies.StagingVars.outputs['setvarStep.apimServiceName'] ]"
          UrlprefixSource: "$[ dependencies.StagingVars.outputs['setvarStep.apimPrefix'] ]"
          ServiceNameDestination: "$[ dependencies.ProdVars.outputs['setvarStep.apimServiceName'] ]"
          UrlprefixDestination: "$[ dependencies.ProdVars.outputs['setvarStep.apimPrefix'] ]"
like image 73
r3verse Avatar answered Oct 30 '25 03:10

r3verse