5

I'm using an extend template and i want to use another template in this just for parameters. But i'm not able to and i'm not sure if the syntax is wrong. getting this error : /templatetest.yml (Line: 8, Col: 1): Unexpected value 'template'

#This is in parameter file in separate repo .policyparams.yml 
parameters:
- name: runPerfTests
  type: boolean
  default: false

resources:
  repositories:
  - repository: MSLearnDocker
    type: git
    name: AzureCoreApp/MSLearnDocker
    ref: refs/heads/master

template: policyparams.yml@MSLearnDocker

stages:
- stage: Build
  displayName: Build
  jobs:
  - job: Build
    steps:
    - script: echo running Build


- stage: UnitTest
  displayName: Unit Test
  dependsOn: Build
  jobs:
  - job: UnitTest
    steps:
    - script: echo running UnitTest


- ${{ if eq(parameters.runPerfTests, true) }}:
  - stage: PerfTest
    displayName: Performance Test
    dependsOn: Build
    jobs:
    - job: PerfTest
      steps:
      - script: echo running PerfTest


- stage: Deploy
  displayName: Deploy
  dependsOn: UnitTest
  jobs:
  - job: Deploy
    steps:
    - script: echo running UnitTest

how can we get parameter template file in extend template?

Sanjeev
  • 415
  • 5
  • 17
  • Hi friend, please check the answer below resolves your original question. Would you mind accepting it as answer if it's helpful? So that more members can get useful info from marked answer and we can archive this thread for you. Thanks~ – LoLance Jul 16 '20 at 09:29

2 Answers2

4

But i'm not able to and i'm not sure if the syntax is wrong. getting this error : /templatetest.yml (Line: 8, Col: 1): Unexpected value 'template'

The Syntax is wrong. It doesn't support using another template in main template just for parameters. So you can't use the formats like:

resources:
  repositories:
  - repository: MSLearnDocker
    type: git
    name: AzureCoreApp/MSLearnDocker
    ref: refs/heads/master

template: policyparams.yml@MSLearnDocker
Or:
- template: policyparams.yml@MSLearnDocker

They're not supported and it will throw expected syntax error: Unexpected value 'template'.

Details:

The parameters have corresponding working scope, it's only valid in the .yml file where it's defined. So the parameters from policyparams.yml file won't be accessible for your main template file.

My Steps:

Main.yml:

parameters:
- name: appFullName
  type: string
  default: Lance

steps:
- script: echo ${{ parameters.appFullName }}
- template: getConfig.yml
- script: echo ${{ parameters.TestIfOk }}

Then:

getConfig.yml

parameters:
- name: Test
  type: string
  default: TestIfOk

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      Write-Host "Hello World"

The result:

enter image description here

LoLance
  • 25,666
  • 1
  • 39
  • 73
0

This is what you are looking for, these docs explain how to use parameters when extending from templates.

In short it means you define all steps in the template yaml file and pass parameter values from the extending yaml file. Keep in mind you can also pass buildsteps as parameter, see here for an example.

See below for a few snippets based on your case.

pipeline-template.yml in repository named TemplateRepo

parameters:
- name: runPerfTests
  type: boolean
  default: false

steps:
- script: echo Current parameter value ${{ parameters.runPerfTests }}.

azure-pipelines.yml in repository named ExtendRepo

# File: azure-pipelines.yml
trigger:
- master

resources:
  repositories:
  - repository: TemplateRepo
    type: git
    name: Test/TemplateRepo
    ref: refs/heads/master

extends:
  template: pipeline-template.yml@TemplateRepo
  parameters:
    runPerfTests: true