0
votes

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 tempalte, if nothing is passed it should go on with deployment and if something is passed as dependency it should validate that stage before installing.

Please help.

2
Have you checked the following replies? Is your issue solved?Cece Dong - MSFT
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 youVR1256

2 Answers

5
votes

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'

1
votes

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.