Marko
Marko

Reputation: 1359

Use variables in azure pipelines yml that extends from template possible?

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

Answers (2)

lastr2d2
lastr2d2

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:

  1. Go to the Pipelines page, select the appropriate pipeline, and then select Edit.
  2. Locate the Variables for this pipeline.
  3. Add or update the variable.
  4. To mark the variable as secret, select Keep this value secret.
  5. Save the pipeline.

Screenshot of the New variable window

Source: Set variables in pipeline

Upvotes: 1

Felix
Felix

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:

enter image description here

Upvotes: 5

Related Questions