28

I'm using Azure Pipelines as a part of Azure DevOps. I'm trying to define variables in my template file, because I need to use the same value multiple times.

This is my stage-template.yml:

parameters:
 - name: param1
   type: string
 - name: param2
   type: string

variables:
  var1: path/${{ parameters.param2 }}/to-my-file.yml

stages:
 - stage: Deploy${{ parameters.param2 }}
   displayName: Deploy ${{ parameters.param1 }}
   jobs:  
    ...

When trying to use this pipeline, I get an error message:

/stage-template.yml (Line: 7, Col: 1): Unexpected value 'variables'

Why is this not working? What did I do wrong?

Draex_
  • 3,011
  • 4
  • 32
  • 50
  • 7
    IMO a somewhat better answer was posted to a duplicate question half a year later: https://stackoverflow.com/questions/63381680/azure-pipelines-yaml-unexpected-value-variables-and-unexpected-stages. – Avius Nov 11 '20 at 14:37

3 Answers3

9

You can't have variables in a template that is included as a stage, job or step template (i.e. included below a stages, jobs or steps element in a pipeline). You can only use variables in a variable template.

The documentation sadly is not really clear about that.

Including a stage template

# pipeline-using-stage-template.yml

stages:
- stage: stage1
[...]
# stage template reference, no 'variables' element allowed in stage-template.yml
- template: stage-template.yml 

Including a variable template

# pipeline-using-var-template.yml

variables:
# variable template reference, only variables allowed inside the template
- template: variables.yml

steps:
- script: echo A step.

If you are using a template to include variables in a pipeline, the included template can only be used to define variables.

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops#variable-reuse

riQQ
  • 9,878
  • 7
  • 49
  • 66
1

you cant have parameters in the pipeline, only in the templateReferences:

name: string  # build numbering format
resources:
  pipelines: [ pipelineResource ]
  containers: [ containerResource ]
  repositories: [ repositoryResource ]
variables: # several syntaxes, see specific section
trigger: trigger
pr: pr
stages: [ stage | templateReference ]

if you want to use variables in templates you have to use proper syntax:

parameters:
 - name: param1
   type: string
 - name: param2
   type: string

stages:
- stage: Deploy${{ parameters.param2 }}
  displayName: Deploy ${{ parameters.param1 }}
  variables:
    var1: path/${{ parameters.param2 }}/to-my-file.yml
  jobs:  
  ...
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • 4
    I can have them since I'm using a template. See this: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops#parameters The whole pipeline works (including parameters) fine without the variables section. – Draex_ Mar 29 '20 at 11:28
  • well, in that case you cant have variables, they should be under stage (or jobs) – 4c74356b41 Mar 29 '20 at 12:11
  • 1
    are you saying that variables can't be used in templates? or that need to be used differently? can you pls provide an example on how I could use them? – Draex_ Mar 29 '20 at 16:07
  • you can have parameters in the pipeline. I'm using them constantly, for input expected on pipeline initialization. https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema%2Cparameter-schema#parameters 'You can use parameters in templates and pipelines.' – BlueBunny Jun 03 '21 at 09:11
  • 1
    This feature (variables reuse through templates) is not present on Azure Devops Server 2019, available in later versions. Please check your version. – Devesh Chanchlani Feb 23 '22 at 10:34
1

This works for me:

In your parent yaml:

stages:
- stage: stage1
  displayName: 'stage from parent'
  jobs:
  - template: template1.yml  
    parameters:
        param1: 'somevalueforparam1'

inside template1:

parameters:  
    param1: ''  
    param2: ''    
jobs:
- job: job1  
  workspace:    
    clean: all  
    displayName: 'Install job'  
   pool:    
      name: 'yourpool'  
  variables:
   - name: var1     
     value: 'value1'
Community
  • 1
  • 1
Appy
  • 11
  • 1