joko_88
joko_88

Reputation: 85

Conditional dependson for multiple stage YAML

I have a YAML pipeline which contains some template files.

Within my pipeline, there are 4 stages that run in parallel to apply DSC. I then have a destroy task which i would like to run, only when all 4 tasks have ran successfully. When i try to add a depends on with a list:

dependsOn: 
   - Stage_A
   - Stage_B
   - Stage_C
   - Stage_D

The error I get is:

The 'dependsOn' parameter is not a valid String.

My template YAML looks like:

...

stages:

   ...
 
  - template: Apply-DSC.yml
    parameters:
      azureSub: '[sub]'
      AutoAccountResourceGroup: 'rg'
      AutoAccountName: 'aa'
      environment: 'b1'
      stageDependsOn: 'b1_apply'
  - template: Destroy-Pipeline.yml
    parameters:
      azureSub: '[sub]'
      terraformStorageAccountResourceGroup: 'rg'
      terraformStorageAccountName: '[]'
      terraformStorageContainerName: '[]'
      terraformStorageRemoteStateKey: '[].tfstate'
      environment: 'b1'
      terraformEnvironmentFileName: 'B01'
      dependsOn: 'Stage_A'

I have 4 stages within my Apply-DSC.yml

Stage_A
Stage_B
Stage_C
Stage_D

Question is, is this possible for my destroy stage to await a successful deployment of Stages A-D when using these stage templates?

Thanks.

Edit: Adding Destroy-Pipeline.yml

# Run & upload Terraform plan
parameters:
  - name: azureSub
    type: string
  - name: terraformStorageAccountResourceGroup
    type: string
  - name: terraformStorageAccountName
    type: string
  - name: terraformStorageContainerName
    type: string
  - name: terraformStorageRemoteStateKey
    type: string
  - name: environment
    type: string
  - name: terraformEnvironmentFileName
    type: string
  - name: dependsOn
    type: string

stages:
  - stage: Destroy_${{ parameters.environment }}
    dependsOn: ${{ parameters.dependsOn }}
    jobs:
    - deployment: '${{ parameters.environment }}_Destroy'
      displayName: '${{ parameters.environment }} Destroy'
      environment: '${{ parameters.environment }} destroy'      
      pool: 
        vmImage: windows-latest
      strategy:
        runOnce:
          deploy:
            steps:
            - download: current
              artifact: 'drop'
              name: 'Download_Terraform_code'
              displayName: 'Download Terraform code'            
            - task: ms-devlabs.custom-terraform-tasks.custom-terraform-installer-task.TerraformInstaller@0
              inputs:
                terraformVersion: '$(TerraformVersion)'
              displayName: 'Install Terraform'
            - task: TerraformCLI@0
              inputs:
                command: 'init'
                workingDirectory: '$(Pipeline.Workspace)/Drop'
                backendType: 'azurerm'
                backendServiceArm: '${{ parameters.azureSub }}'
                backendAzureRmResourceGroupName: '${{ parameters.terraformStorageAccountResourceGroup }}'
                backendAzureRmStorageAccountName: '${{ parameters.terraformStorageAccountName }}'
                backendAzureRmContainerName: '${{ parameters.terraformStorageContainerName }}'
                backendAzureRmKey: '${{ parameters.terraformStorageRemoteStateKey }}'
                allowTelemetryCollection: false
              displayName: 'Terraform Init'
            - task: PowerShell@2
              inputs:
                targetType: 'inline'
                script: |
                  terraform workspace select $(WorkspaceEnvironment)
                workingDirectory: '$(Pipeline.Workspace)/Drop'
              displayName: 'Select Workspace' 
            - task: TerraformCLI@0
              inputs:
                command: 'plan'
                environmentServiceName: '${{ parameters.azureSub }}'
                commandOptions: '-destroy -var-file="./environments/${{ parameters.terraformEnvironmentFileName }}.tfvars" -input=false'
                allowTelemetryCollection: false
                workingDirectory: '$(Pipeline.Workspace)/Drop'            
              displayName: 'Plan Destroy'
            - task: TerraformCLI@0
              inputs:
                command: 'destroy'
                workingDirectory: '$(Pipeline.Workspace)/Drop'
                environmentServiceName: '${{ parameters.azureSub }}'
                commandOptions: '-var-file="./environments/${{ parameters.terraformEnvironmentFileName }}.tfvars" -input=false '
                allowTelemetryCollection: false
              displayName: 'Run Destroy'

Upvotes: 1

Views: 2240

Answers (1)

joko_88
joko_88

Reputation: 85

I changed the type from string to object

parameters:
  - name: dependsOn
    type: object
    default: []

Then within my template block i added the object like:

 - template: Destroy-Pipeline.yml
    parameters:
      ...
      dependsOn: ['Stage_A', 'Stage_B' ...]

Upvotes: 3

Related Questions