3

What I'm actually trying to do is the following: Use pipelines with the same deploy script for different environments. Each environment would have a variable group with the settings required to deploy there.

But it seems difficult to specify a variable group in the YAML file that is not hardcoded. I would like the pipeline to have a pipeline variable to specify which variable group to use.

I have found some pointers where this seems possible using templates. If that is the only way, then so be it. But I'm hoping there is a more straighforward way.

3
  • Have you tried to add the variable group name in the parameters? then you can replace the variable group name with the name of the received parameter Commented Jan 2, 2022 at 18:04
  • @Bruno thanks - using a parameter might work. But the problem remains - how to define the parameter outside the yml file. I want to define several pipelines that all use the same yml file, but use different variable groups in order to target different environments. Commented Jan 3, 2022 at 10:54
  • Note to self: Using the same pipeline to deploy to different environments is a good option, rather than having a pipeline per environment. Which environment to deploy to (and subsequently what settings to use) can be specified in variables/parameters. Commented Aug 25, 2023 at 9:12

3 Answers 3

7

here we drive variable-group value by parameter environment

cat mypipeline.yaml

variables:
  - ${{ if eq(parameters.environment, 'dev') }}:
      - group: myvariable-group-dev
  - ${{ if eq(parameters.environment, 'stg') }}:
      - group: myvariable-group-stg
  - name: environment
    value: ${{ parameters.environment }}

parameters:
  - name: environment
    displayName: Environment
    type: string
    default: dev
    values:
      - dev
      - stg

      

could be a pipeline template or just a pipeline

Sign up to request clarification or add additional context in comments.

2 Comments

This should be the correct answer.
I haven't seen this variant yet. However, it seems we still have to include all the variable groups/environments into the if statements in the yml file. So if we later add an environment, we would have to change the yml files for all pipelines.
1

It seems to be not possible using self defined pipeline variables as if expressions are evaluated before values of the variables are available for the executed pipeline. Here I described a solution that uses built-in variables https://stackoverflow.com/a/76879073/9578912

Comments

0

In YAML you can use "group" to specify which variable group to use. And you can reference multiple variable groups in the same pipeline. If multiple variable groups include the same variable, the variable group included last in your YAML file sets the variable's value.

It looks like this:

variables:

-group: my-first-variable-group

-group: my-second-variable-group

For more info, please refer to Use a variable group

If this does not meet your needs, I suggest you use Variable templates.

1 Comment

Thanks for your reply. I am already using variable groups, but I want to define outside the yml file which variable group to use (to avoid having one yml file per environment). So I am afraid your suggestions did not quite help me solve this challenge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.