azure-pipelinesazure-pipelines-yaml

how can use template for parameters in azure pipelines


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?


Solution

  • 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