5

As illustrated in the yaml below, we populate the deploymentFailureReason variable when we know why the deployment failed during any of the tasks contained within parameters.deploymentTemplate. We would like to be able to pass that reason to our deployment tracker when called from the on:failure job.

We know how to pass variables between jobs and between stages. Both require knowing the job and/or stage that we are trying to get the variable from. These jobs get created on a per vm basis and we currently can't see a way to access the failure.reason output variable that gets created in the deployment jobs. We have verified that deploymentFailureReason is populated when arriving at the powershell step named "failure" below. Just can't seem to find a way to get it to the on:failure job.

We have toyed with the idea of writing the deploymentFailureReason to a file and reading it from the on:failure job since both run on the same machine, but surely there must be a better way.

We welcome any ideas, clarifying questions or thoughts that may nudge us out of the current box restraining our thinking on this issue.

parameters:
  - name: deploymentTemplate
    type: string

stages:
  - stage: DevStage
    jobs:
      - deployment: Dev_Deployment
        variables:
          - name: deploymentFailureReason
            value: ''
  
        environment:
          name: Dev-VM
          resourceType: VirtualMachine
        workspace:
          clean: all
        strategy:
          runOnce:
            deploy:
              steps:
                - template: ./deployment-initialize.yml    
                - template: ${{ parameters.deploymentTemplate }}
                - powershell: |
                    Write-Output "were here"
                    Write-Output "deploymentFailureReason = $(deploymentFailureReason)"
                    
                    Write-Host "##vso[task.setvariable variable=reason;isOutput=true]$(deploymentFailureReason)"
                  name: failure
                  condition: failed()
            on:  # On success or failure hook for runOnce deployment strategy
              success:  # Runs on success of all of the steps
                steps:
                  - template: ./call-deployment-tracker.yml
                    parameters:
                      functionName: "DeploymentSuccess"
              failure:  # Runs on failure of any step
                steps:
                  - template: ./call-deployment-tracker.yml
                    parameters:
                      functionName: "DeploymentFailure"
...
cResults
  • 733
  • 1
  • 5
  • 17

0 Answers0