We have a collection of Azure DevOps pipeline templates that we re-use across multiple repositories. Therefore we wanted to have a file that contains variables for all of our templates.
The repo structure looks like this
template repo
├── template-1.yml
├── template-2.yml
└── variables.yml
project repo
├── ...
└── azure-pipelines.yml
The variables.yml
looks like this
...
variables:
foo: bar
In template-1.yml
we are importing the variables.yml
as described in here
variables:
- template: variables.yml
In the azure-pipelines.yml
we are using the template like this
resources:
repositories:
- repository: build-scripts
type: git
name: project-name/build-scripts
steps:
...
- template: template-1.yml@build-scripts
When we now try to run the pipeline, we get the following error message:
template-1.yml@build-scripts (Line: 10, Col: 1): Unexpected value 'variables'
Passing variables between tasks in the same jobSet the value with the command echo "##vso[task. setvariable variable=FOO]some value" In subsequent tasks, you can use the $(FOO) syntax to have Azure Pipelines replace the variable with some value.
Option 1: Create a pipeline parameter in the settings panel Next to the name of your pipeline draft, select the gear icon to open the Settings panel. In the Pipeline parameters section, select the + icon. Enter a name for the parameter and a default value.
Content from one file is inserted into another file. When a template controls what is allowed in a pipeline, the template defines logic that another file must follow. Use templates to define your logic once and then reuse it several times. Templates combine the content of multiple YAML files into a single pipeline.
This sample app demonstrates the use of variable template files in Azure Pipelines. It creates a sample python application with Flask and deploys it to Azure Web App for three different environments, 'dev', 'qa' and 'prd'. Variable template files allow you to group pipeline variables.
It is not recommended because if you do that you will: You will not know which parameters are needed to use the template. As you can see in these tutorials, you have multiple ways to use parameters and variables inside Azure DevOps, be sure to check the context to apply the right one, you do not access your variables and parameters the same way.
In general, variables in Azure Devops can be classified under System Variables, Environment Variables and User Defined Variables. System Variables: Contains predefined values for the pipeline run, like Build.ArtifactStagingDirectory, Build.BuildID etc. A comprehensive list of System variables can be found in this article.
Azure Pipelines supports three different ways to reference variables: 1 macro expression 2 template expression 3 runtime expression More ...
The issue is because you used variable template at steps scope. And variables
simply doesn't exists at that level. This should work for you:
resources:
repositories:
- repository: build-scripts
type: git
name: project-name/build-scripts
variables:
- template: template-1.yml@build-scripts
steps:
...
this is available to use at any place where variables are possible to use. So for instance you can use this in that way:
jobs:
- job: myJob
timeoutInMinutes: 10
variables:
- template: template-1.yml # Template reference
pool:
vmImage: 'ubuntu-16.04'
steps:
- script: echo My favorite vegetable is ${{ variables.favoriteVeggie }}.
If your template file only has variables
, you can refer to Krzysztof Madej's answer.
If your template file has both variables
and steps
as shown below, it can only be used by extends.
# File: template-1.yml
variables: ...
steps: ...
Or you can write them in a stage, as shown below.
# File: template-1.yml
stages:
- stage: {stage}
variables: ...
jobs:
- job: {job}
steps: ...
Then insert it as a separate stage.
# azure-pipelines.yml
stages:
- stage: ...
- template: template-1.yml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With