skip to Main Content

Why can’t I do this:

azure-pipeline.yml: extends build-and-deploy.yml@demo

...

parameters:
- name: allowDevDeployment
  displayName: "Allow this build to be deployed to DEV"
  type: boolean
  default: false

- name: selectDeploymentType
  displayName: Select deployment type
  type: string
  default: 0
  values:
    - 0 # (Standard/regular deployment)
    - 1 #(Single branch; master deploys to  DEV -> DEVTEST -> STAGE -> PROD)
    - 2 # (develop to DEV -> Auto PR to master -> DEVTEST -> STAGE -> PROD)


extends:
  template: templates/build-and-deploy.yml@demo
  parameters:
    allowDevDeployment: ${{ parameters.allowDevDeployment }}
    deploymentType: ${{ parameters.selectDeploymentType}}
    ...

In templates/build-and-deploy.yml: Based on what’s selected during pipeline runtime, either 0.yml, 1.yml or 2.yml is called (up till this point it works because I have a pwsh step (before it calls the template) that write-host the parameter that’s selected at runtime, and it outputs the right parameter. The step isn’t included here)

parameters:
  ...
- name: deploymentType
  type: string
  default: none

steps:
  - ${{ if eq(parameters.deploymentType, 0)}}:
    - template: deploymentTypes/0.yml
      parameters:
        allowDevDeployment: ${{ parameters.allowDevDeployment }}
        ...
  - ${{ if eq(parameters.deploymentType, 1)}}:
    - template: deploymentTypes/1.yml
      parameters:
        allowDevDeployment: ${{ parameters.allowDevDeployment }}
        ...
  - ${{ if eq(parameters.deploymentType, 2)}}:
    - template: deploymentTypes/2.yml
      parameters:
        allowDevDeployment: ${{ parameters.allowDevDeployment }}
        ...

and in deploymentTypes/0.yml (or 2.yml or 3.yml): At this point, I get an error that reads "/templates/deploymentTypes/0.yml@demo (Line: 184, Col: 1): Unexpected value ‘stages’"

enter image description here

This file (0.yml or 2.yml or 3.yml) essentially defines what stages must run depending on what branch is built or the pipeline is run from.

parameters:
...
- name: allowDevDeployment
  type: boolean
  default: false
    
stages:
- ${{ if gt(length(parameters.buildJobs), 0) }}:
  - stage: Build
    jobs:
    - ${{ each job in parameters.buildJobs }}:
      - ${{ if startsWith(job.pool.vmImage, 'ubuntu') }}:
        - job: ${{ job.job }}
          pool: ${{ job.pool }}
          ${{ if job.condition }}:
            condition: ${{ job.condition }}
            ...
# Develop, Nightly or Support Branch
- ${{ if or(in(variables['Build.SourceBranchName'], 'develop', 'nightly'), contains(variables['Build.SourceBranch'], '/support/'), eq(parameters.allowDevDeployment, true)) }}:
  - ${{ if gt(length(parameters.devJobs), 0) }}:
    - stage: Dev
      ...

# Develop or Support Branch
- ${{ if or(eq(variables['Build.SourceBranchName'], 'develop'), contains(variables['Build.SourceBranch'], '/support/')) }}:
  - ${{ if gt(length(parameters.devTestJobs), 0) }}:
    - stage: DevTest
      ${{ if ne(parameters.devTestStageLabel, 'none') }}:
        displayName: ${{ parameters.devTestStageLabel }}
        ...

2

Answers


  1. I can reproduce the same error with your yaml sample.

    enter image description here

    The error is caused by the steps parameter in templates/build-and-deploy.yml, you should change it to stages since it invokes stage level template in 0.yml,1.yml…

    templates/build-and-deploy.yml

    parameters:
    - name: allowDevDeployment
      type: boolean
      default: false
    
    - name: deploymentType
      type: string
      default: none
     
    stages:                  # change steps to stages here
      - ${{ if eq(parameters.deploymentType, 0)}}:
        - template: deploymentTypes/0.yml
          parameters:
            allowDevDeployment: ${{ parameters.allowDevDeployment }}
    
      - ${{ if eq(parameters.deploymentType, 1)}}:
        - template: deploymentTypes/1.yml
          parameters:
            allowDevDeployment: ${{ parameters.allowDevDeployment }}
    
      - ${{ if eq(parameters.deploymentType, 2)}}:
        - template: deploymentTypes/2.yml
          parameters:
            allowDevDeployment: ${{ parameters.allowDevDeployment }}
    
    Login or Signup to reply.
  2. You’re trying to reference stage templates inside of steps.

    Instead of:

    parameters:
      ...
    - name: deploymentType
      type: string
      default: none
    
    steps: # <--------------- This is wrong
    
      - ${{ if eq(parameters.deploymentType, 0)}}:
        - template: deploymentTypes/0.yml
          parameters:
            allowDevDeployment: ${{ parameters.allowDevDeployment }}
            ...
      - ${{ if eq(parameters.deploymentType, 1)}}:
        - template: deploymentTypes/1.yml
          parameters:
            allowDevDeployment: ${{ parameters.allowDevDeployment }}
            ...
      - ${{ if eq(parameters.deploymentType, 2)}}:
        - template: deploymentTypes/2.yml
          parameters:
            allowDevDeployment: ${{ parameters.allowDevDeployment }}
            ...
    

    Try:

    
    parameters:
      ...
    - name: deploymentType
      type: string
      default: none
    
    stages: # <----------------- value was replaced
      - template: deploymentTypes/${{ parameters.deploymentType }}.yml
        parameters:
          allowDevDeployment: ${{ parameters.allowDevDeployment }}
          ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search