If you only name the variable group in YAML pipelines, anyone who can push code to your repository could extract the contents of secrets in the variable group. Therefore, to use a variable group with YAML pipelines, you must authorize the pipeline to use the group. You can authorize a pipeline to use a variable group in the Azure Pipelines user interface or by using the Azure DevOps CLI.
Authorization via the Pipelines UI
You can authorize pipelines to use your variable groups by using the Azure Pipelines user interface.
- In your Azure DevOps project, select Pipelines > Library from the left menu.
- On the Library page, select the variable group you want to authorize.
- On the variable group page, select the Pipeline permissions tab.
- On the Pipeline permissions screen, select + and then select a pipeline to authorize. Or, select the More actions icon, select Open access, and select Open access again to confirm.
Selecting a pipeline authorizes that pipeline to use the variable group. To authorize another pipeline, select the + icon again. Selecting Open access authorizes all project pipelines to use the variable group. Open access might be a good option if you don't have any secrets in the group.
Another way to authorize a variable group is to select the pipeline, select Edit, and then queue a build manually. You see a resource authorization error and can then explicitly add the pipeline as an authorized user of the variable group.
Authorization via the Azure DevOps CLI
In Azure DevOps Services, you can authorize variable groups by using the Azure DevOps CLI.
Azure DevOps CLI commands aren't supported for Azure DevOps Server.
To authorize all project pipelines to use the variable group, set the authorize
parameter in the az pipelines variable-group create command to true
. This open access might be a good option if you don't have any secrets in the group.
Link a variable group to a pipeline
Once you authorize a YAML pipeline to use a variable group, you can use variables within the group in the pipeline.
To use variables from a variable group, add a reference to the group name in your YAML pipeline file.
variables:
- group: my-variable-group
You can reference multiple variable groups in the same pipeline. If multiple variable groups include the variables with the same name, the last variable group that uses the variable in the file sets the variable's value. For more information about precedence of variables, see Expansion of variables.
You can also reference a variable group in a template. The following variables.yml template file references the variable group my-variable-group
. The variable group includes a variable named myhello
.
variables:
- group: my-variable-group
The YAML pipeline references the variables.yml template, and uses the variable $(myhello)
from the variable group my-variable-group
.
stages:
- stage: MyStage
variables:
- template: variables.yml
jobs:
- job: Test
steps:
- script: echo $(myhello)
Use variables in a linked variable group
You access the variable values in a linked variable group the same way you access variables you define within the pipeline. For example, to access the value of a variable named customer
in a variable group linked to the pipeline, you can use $(customer)
in a task parameter or a script.
If you use both standalone variables and variable groups in your pipeline file, use the name
-value
syntax for the standalone variables.
variables:
- group: my-variable-group
- name: my-standalone-variable
value: 'my-standalone-variable-value'
To reference a variable in a variable group, you can use macro syntax or a runtime expression. In the following examples, the group my-variable-group
has a variable named myhello
.
To use a runtime expression:
variables:
- group: my-variable-group
- name: my-passed-variable
value: $[variables.myhello]
- script: echo $(my-passed-variable)
To use macro syntax:
variables:
- group: my-variable-group
steps:
- script: echo $(myhello)
You can't access secret variables, including encrypted variables and key vault variables, directly in scripts. You must pass these variables as arguments to a task. For more information, see Secret variables.