9

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.

2
  • Have you checked the following replies? Is your issue solved? Feb 26, 2021 at 9:58
  • Hi Cece Don, the below answers helped but it is still proceeding with next step without waiting for depends on, I found a solution under Environment pipelines on clicking on a environment there is a setting called approval & check we can add there instead of depends on.. I will upvote the answers thank you
    – VR1256
    Mar 3, 2021 at 13:53

2 Answers 2

22

You declare a DependsOn object parameter in your template. It will be required, but checked in the template. If you don't want a dependency, just pass an empty DependsOn. This is set up as an object, so that you can pass a list of stages, in case you have a stage that's dependent on more than one.

Set your pipeline up like this:

stages:
- template: azure-pipelines-multi-stage-release.yml  # Template reference
  parameters:
      Environment: 'Dev'
      Enabled: True
      WebAppName: 'azureappservicehelloworldapp-dev'
      Stage: Dev
      ArtifactName : 'helloWorldArtifact'
      # empty DependsOn, as Dev depends on nothing
      DependsOn:
- template: azure-pipelines-multi-stage-release.yml  # Template reference
  parameters:
      Environment: 'UAT'
      Enabled: True
      WebAppName: 'azureappservicehelloworld-uat'
      Stage: UAT
      ArtifactName : 'helloWorldArtifact'
      DependsOn:
      - Dev
- template: azure-pipelines-multi-stage-release.yml  # Template reference
  parameters:
      Environment: 'Prod'
      Enabled: True
      WebAppName: 'azureappservicehelloworld'
      Stage: Prod
      ArtifactName : 'helloWorldArtifact'
      DependsOn:
      - UAT

and your template up like this:

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

stages:
- stage: ${{ parameters.Stage }}  
  displayName: '${{ parameters.Stage }} Stage'
  # only include the DependsOn parameter if provided
  ${{ if parameters.DependsOn }}:
    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 your pipeline will expand to:

stages:
- stage: Dev
  displayName: Dev Stage
  jobs:
  - deployment: Dev
    timeoutInMinutes: 70
    environment:
      name: Dev Environment
    pool:
      vmImage: $(vmImageName)
    strategy:
      runOnce:
        deploy:
          steps:
          - task: DownloadBuildArtifacts@0
            inputs:
              buildType: 'current'
              downloadType: 'single'
              artifactName: helloWorldArtifact
              downloadPath: '$(System.ArtifactsDirectory)'
          - task: AzureRmWebAppDeployment@4
            inputs:
              ConnectionType: 'AzureRM'
              azureSubscription: 'AzureConnectionSC'
              appType: 'webApp'
              WebAppName: azureappservicehelloworldapp-dev
              package: '$(System.ArtifactsDirectory)/**/*.zip'
- stage: UAT
  displayName: UAT Stage
  dependsOn:
  - Dev
  jobs:
  - deployment: UAT
    timeoutInMinutes: 70
    environment:
      name: UAT Environment
    pool:
      vmImage: $(vmImageName)
    strategy:
      runOnce:
        deploy:
          steps:
          - task: DownloadBuildArtifacts@0
            inputs:
              buildType: 'current'
              downloadType: 'single'
              artifactName: helloWorldArtifact
              downloadPath: '$(System.ArtifactsDirectory)'
          - task: AzureRmWebAppDeployment@4
            inputs:
              ConnectionType: 'AzureRM'
              azureSubscription: 'AzureConnectionSC'
              appType: 'webApp'
              WebAppName: azureappservicehelloworld-uat
              package: '$(System.ArtifactsDirectory)/**/*.zip'
- stage: Prod
  displayName: Prod Stage
  dependsOn:
  - UAT
  jobs:
  - deployment: Prod
    timeoutInMinutes: 70
    environment:
      name: Prod Environment
    pool:
      vmImage: $(vmImageName)
    strategy:
      runOnce:
        deploy:
          steps:
          - task: DownloadBuildArtifacts@0
            inputs:
              buildType: 'current'
              downloadType: 'single'
              artifactName: helloWorldArtifact
              downloadPath: '$(System.ArtifactsDirectory)'
          - task: AzureRmWebAppDeployment@4
            inputs:
              ConnectionType: 'AzureRM'
              azureSubscription: 'AzureConnectionSC'
              appType: 'webApp'
              WebAppName: azureappservicehelloworld
              package: '$(System.ArtifactsDirectory)/**/*.zip'

8

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.