Trying to use variables inside a template as discussed here doesn't work. When you run the build it says variables is an unexpected value. It doesn't matter whether the variables section precedes or follows the parameters.
parameters:
version: ""
isPrerelease: ""
buildLabel: ""
# Fails here with 'unexpected value variables`
variables:
GitVersion.SemVer: ''
UseGitVersion: false
steps:
- template: read-propsfile.yml
Note that elsewhere it is mentioned the syntax for variables is as follows but that doesn't work either.
variables:
- name: SomeVar
value: true
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
@mtaylorfsmb Thank you for the feedback, assigning this to the author for review.
@mtaylorfsmb You can't assign variables at the step level, only at job, stage, and global.
As @ElvenSpellmaker mentioned, your template include is in the wrong spot. Here's a rewritten version.
azure-pipelines.yml
parameters:
- name: version
type: string
default: ""
variables:
# a regular variable
- name: UseGitVersion
value: false
# a reference to a variable template
- template: vars.yml # Template reference
trigger: none
steps:
- script: echo My favorite vegetable is ${{ variables.favoriteVeggie }}.
vars.yml
# File: vars.yml
parameters:
version: ""
variables:
favoriteVeggie: 'brussels sprouts'
I'll add a more complex example to the docs.
@juliakm Are you sure variables can be defined at a step template level? I'm pretty sure I used to get errors for that.
@juliakm my takeaway from this is that the docs should make it clear that you cannot mix variables and steps in the same template. It doesn't work. You can have a template that contains nothing but variables and you can then reference that template in your existing variables section in the parent script OR you can create a template that contains steps (and no variables) that you can add to the steps section of the parent script.
You cannot create a single template that contains BOTH variables and steps sections because of the error I mentioned earlier. This greatly limits the usefulness of this feature but that is an issue for the YAML implementation, not the docs. It just isn't clear that this is true in the docs.
Also, are the 'variable templates' documented? I can't find any reference to them...
@ElvenSpellmaker here scroll down to Variable reuse section.
@mtaylorfsmb My understanding is that variables templates are used like an include file. You have a set of variables in your template that you then include in a pipeline. If you want to use steps and also define something, I think it would make more sense to use parameters in your template. Here's more information on passing parameters to templates and extending from templates.
You cannot create a single template that contains BOTH variables and steps sections because of the error I mentioned earlier. This greatly limits the usefulness of this feature but that is an issue for the YAML implementation, not the docs.
Is this tracked somewhere? For now I can workaround using
echo "##vso[task.setvariable variable= favoriteVeggie]potato"
as the first step in a template with steps, but it seems needlessly clunky.
You cannot create a single template that contains BOTH variables and steps sections because of the error I mentioned earlier. This greatly limits the usefulness of this feature but that is an issue for the YAML implementation, not the docs. It just isn't clear that this is true in the docs.
This was also my problem.
Being able to create and use variables with template parameters would simplify my yaml files.
Yes totally agree, this would make it is so much easier to re-use templates with a variables.yml, rather than using the chatty parameters syntax...
Most helpful comment
@juliakm my takeaway from this is that the docs should make it clear that you cannot mix variables and steps in the same template. It doesn't work. You can have a template that contains nothing but variables and you can then reference that template in your existing
variablessection in the parent script OR you can create a template that contains steps (and no variables) that you can add to thestepssection of the parent script.You cannot create a single template that contains BOTH
variablesandstepssections because of the error I mentioned earlier. This greatly limits the usefulness of this feature but that is an issue for the YAML implementation, not the docs. It just isn't clear that this is true in the docs.