Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use variables in Azure DevOps Pipeline templates

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'
like image 684
Michael Lihs Avatar asked Sep 16 '20 10:09

Michael Lihs


People also ask

How do you pass variables in Azure pipelines Yml tasks?

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.

How do I add parameters to Azure DevOps pipeline?

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.

How do I use Azure DevOps pipeline template?

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.

What are variable template files in azure pipelines?

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.

Is it recommended to use a template in Azure DevOps?

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.

What are variables in Azure DevOps?

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.

How do I reference variables in azure pipelines?

Azure Pipelines supports three different ways to reference variables: 1 macro expression 2 template expression 3 runtime expression More ...


2 Answers

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 }}.
like image 110
Krzysztof Madej Avatar answered Sep 28 '22 00:09

Krzysztof Madej


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
like image 24
Jane Ma-MSFT Avatar answered Sep 27 '22 22:09

Jane Ma-MSFT