'How to work with DependsOn having stage dependency in Azure DevOps

I have below template which does multi stage deployment:

parameters:
- name: Stage
  type: string
- name: Environment
  type: string
- name: Enabled  
  type: boolean
  default: false
- name: WebAppName
  type: string
- name: ArtifactName
  type: string

stages:

- stage: ${{ parameters.Stage }}  
  displayName: '${{ parameters.Stage }} Stage'
  dependsOn: '${{ parameters.DependsOn }}'
  jobs:
   - deployment: ${{ parameters.Environment }} 
     timeoutInMinutes: 70
     environment: '${{ parameters.Environment }} Environment'
     pool:
        vmImage: $(vmImageName)
     strategy:
      runOnce:
        deploy:
          steps:
          - task: DownloadBuildArtifacts@0
            inputs:
              buildType: 'current'
              downloadType: 'single'
              artifactName: ${{ parameters.ArtifactName }}
              downloadPath: '$(System.ArtifactsDirectory)'
          - task: AzureRmWebAppDeployment@4
            inputs:
              ConnectionType: 'AzureRM'
              azureSubscription: 'AzureConnectionSC'
              appType: 'webApp'
              WebAppName: ${{ parameters.WebAppName }}
              package: '$(System.ArtifactsDirectory)/**/*.zip'

And from my pipeline I am using the template:

- template: azure-pipelines-multi-stage-release.yml  # Template reference
    parameters:
       Environment: 'Dev'
       Enabled: True
       WebAppName: 'azureappservicehelloworldapp-dev'
       Stage: 'Dev'
       ArtifactName : 'helloWorldArtifact'

  - template: azure-pipelines-multi-stage-release.yml  # Template reference
    parameters:
       Environment: 'UAT'
       Enabled: True
       WebAppName: 'azureappservicehelloworld-uat'
       Stage: 'UAT'
       ArtifactName : 'helloWorldArtifact'

  - template: azure-pipelines-multi-stage-release.yml  # Template reference
    parameters:
       Environment: 'Prod'
       Enabled: True
       WebAppName: 'azureappservicehelloworld'
       Stage: 'Prod'
       ArtifactName : 'helloWorldArtifact'

How do I pass DependsOn onto the template, in dev there is no stage dependency so it should deploy directly but UAT is dependent on Dev, Prod is Dependent on UAT. How can I pass value to template, if nothing is passed it should go on with deployment and if something is passed as dependency it should validate that stage before installing.



Solution 1:[1]

You can use conditional insertion of a dependsOn block:

${{ if ne(parameters.DependsOn, '')}}:
  dependsOn: ${{ parameters.DependsOn }} 

You obviously will have to declare a DependsOn parameter to your template as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Mann