7

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

Marko
  • 929
  • 9
  • 27
  • Hi @Marko. How about the issue? Does the answer below resolved your question, If yes, you could accept it as an answer, so it could help other community members who get the same issues and we could archive this thread, thanks. If not, please let us know if you would like further assistance. – Kevin Lu-MSFT Mar 09 '21 at 06:46

2 Answers2

4

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

Felix
  • 1,104
  • 3
  • 6
  • If you get it run with an "extends" and not just a stage template you get my respect :) I did not get it to run. – Marko Mar 09 '21 at 07:21
  • I have changed the script on my answer to use the 'extends', and attach the new test result. It works well, hope this will help you. – Felix Mar 10 '21 at 02:00
  • Ok. Thanks. I will test it on my side and see where my issue was that it was not working. – Marko Mar 10 '21 at 07:17
  • No it is still not working. Will paste my yml file for reference in the initial description. – Marko Mar 10 '21 at 07:21
  • Could you please share me your template.yml file and the __built-in-schema.yml file, it will help me a lot to locate this issue – Felix Mar 10 '21 at 08:01
  • it looks like the issue happens on your template.yml file, we recommend you can try to use my demo temp to do the test – Felix Mar 10 '21 at 08:17
  • 2
    I am pretty sure if you add global variables in your temp.yaml (not below "stages" but directly below the "parameters" block ) then it will fail also for you. – Marko Mar 11 '21 at 10:13
  • Can you try out what I described in the previous comment? If it still works for you I promise I will test it on my side and provide an obfuscated version of our template. – Marko Mar 11 '21 at 10:21
  • 1
    Marko, if we add the global variables in the temp.yaml not in the stages. This will course error: 'variables' is already defined. Because, we have already define the variable in our main yaml file. We can just define the global variables on the main yaml file. – Felix Mar 12 '21 at 06:53
  • By the way, if it is possible, you can try to use the variable temp:https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops#variable-reuse – Felix Mar 12 '21 at 06:56
  • Ok. Thanks for the confirmation that I want to do is not possible like that. At least I know now that I need to move the variables to specific stage so that global variables can be used in the main yaml file. – Marko Mar 15 '21 at 10:09
0

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

lastr2d2
  • 3,604
  • 2
  • 22
  • 37