Reputation: 1359
we are using the extend feature to reuse templates in our pipelines in a secure way. For easier defining the parameters for the template I would like to use variables but I feel this is not possible.
But since I could not find an answer in the official docs I am asking in this round.
My yml file looks like this:
name: '[$(Date:yyyyMMdd).$(Rev:r)][$(Build.SourceBranchName)]'
# ========================================================================
# Pipeline Triggers
# ========================================================================
schedules:
- cron: "0 22 * * *" # time in UTC timezone
displayName: Daily midnight build for mainline branches
branches:
include:
- develop
- master
always: true
- cron: "0 22 * * *" # time in UTC timezone
displayName: Daily midnight build for release branches
branches:
include:
- release/*
always: false
# ========================================================================
# Template resource
# ========================================================================
resources:
repositories:
- repository: templates # id for reuse in below script code
type: git
name: Tools/pipeline-templates
ref: develop
variables:
Test: TestGroup
# ========================================================================
# Template reference
# ========================================================================
extends:
template: template.yml@templates # Template reference
parameters:
Param1:
- "-T test"
When I try to run it I get following error:
__built-in-schema.yml (Line: xx, Col: yy): 'variables' is already defined
I feel since our template is also using variables it cannot be used in the root yml file.
Thank you
Upvotes: 10
Views: 7111
Reputation: 3968
It is possible to set a pipeline variable in the classic way if you are having issues set it in YAML files.
You can set a variable for a build pipeline by following these steps:
- Go to the Pipelines page, select the appropriate pipeline, and then select Edit.
- Locate the Variables for this pipeline.
- Add or update the variable.
- To mark the variable as secret, select Keep this value secret.
- Save the pipeline.
Source: Set variables in pipeline
Upvotes: 1
Reputation: 1152
Do you mean you want to use the variables to help your define the template parameters? If yes, we recommend you can use the ${{variables.VariableName}}
Here is the demo script that I changed the stages to the key words(extends):
resources:
repositories:
- repository: templates
type: git
name: Tech-Talk/template
trigger: none
variables:
- name: Test
value: TestGroup
pool:
vmImage: ubuntu-latest
extends:
template: temp.yaml@templates
parameters:
agent_pool_name: ''
db_resource_path: $(System.DefaultWorkingDirectory)
variable_group: ${{variables.Test}}
And here is the my temp.yaml:
parameters:
- name: 'variable_group'
type: string
default: 'default_variable_group'
- name: agent_pool_name
default: ""
- name: db_resource_path
default: ""
stages:
- stage:
displayName: ${{parameters.db_resource_path}}
variables:
- group: ${{parameters.variable_group}}
jobs:
- job: READ
displayName: Reading Parameters
steps:
- script: |
echo variable_group: ${{parameters.variable_group}}
- powershell: echo "$(ServiceConnection)"
Attach the new test result: You can find the variable TestGroup has been pass to the temp yaml:
Upvotes: 5