When building or deploying to multiple environments using Azure DevOps, it can be useful to have different variable groups per environment. As well as being easier to work with compared to combining everything in a single variable group, it also allows permissions to be adjusted per environment.
When running multiple builds/deployments for the same app in parallel (for example, in a multi-tenanted environment), we can usually use the strategy
property to provide a matrix that pipelines will use to start up multiple jobs. For example:
1 | jobs: |
Unfortunately we can’t dynamically construct a variable group name using strategy/matrix. The following would result in an “variable group not found” sort of error:
1 | variables: |
Instead, we can use a more primitive each
loop within a template that takes a list of environments (or whatever) as a parameter. The template is needed a we can’t define a complex object anywhere else but the parameter input.
The main pipeline looks like this:
1 | jobs: |
And the template, which constructs the variable group name, looks like this:
1 | parameters: |