azure-devopsazure-pipelinesazure-pipelines-yaml

Azure DevOps Pipeline: Passing variables from a deployment job to another stage


I'm working on an Azure DevOps Pipeline, and I'm having an issue where I'm unable to pass an output variable from a deployment job to a subsequent stage. Here's a simplified version of my pipeline YAML:

trigger:
- master

pool:
  vmImage: windows-latest

stages:
- stage: Deploy
  jobs:
  - deployment: DeployJob
    environment: 
      name: 'Local VM Deployment'
      resourceName: 'Resource-1'
      resourceType: VirtualMachine
    strategy:
      runOnce:
        deploy:
          steps:
          - task: PowerShell@2
            name: SetDeployResult
            inputs:
              targetType: 'inline'
              script: |
                Write-Host "Setting output variable"
                Write-Host "##vso[task.setvariable variable=DeployResult;isOutput=true]success"

- stage: PostDeployment
  dependsOn: Deploy
  jobs:
  - job: PostDeployJob
    variables:
      deployResult: $[ dependencies.Deploy.outputs['DeployJob.Deploy_Resource1.SetDeployResult.DeployResult'] ] 
    steps:
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          Write-Host "The deploy result from the Deploy stage is $(deployResult)"
      displayName: 'Use Output Variable from Deploy Stage'

Problem:

In the PostDeployment stage, I’m trying to access the output variable DeployResult from the Deploy stage using the expression:

deployResult: $[ dependencies.Deploy.outputs['DeployJob.Deploy_Resource1.SetDeployResult.DeployResult'] ]

However, I am getting the following output:

Job preparation parameters
Variables:
  deployResult:
    Parsing expression: <dependencies.Deploy.outputs['DeployJob.Deploy_Resource1.SetDeployResult.DeployResult']> 
    Evaluating: dependencies['Deploy']['outputs']['DeployJob.Deploy_Resource1.SetDeployResult.DeployResult']
    Expanded: Null
    Result: '' 

It seems that the output variable DeployResult is not being set correctly, or there's an issue with how I'm referencing it. Could someone help me understand why this variable is not being passed correctly from the deployment job to the PostDeployment stage?


Solution

  • Change to like as below which can work as expected:

    stages:
    - stage: Deploy
      jobs:
      - deployment: DeployJob
        environment:
          name: 'Local VM Deployment'
          resourceName: 'Resource-1'
          resourceType: VirtualMachine
        strategy:
          runOnce:
            deploy:
              steps:
              - pwsh: |
                  Write-Host "Setting output variable"
                  Write-Host "##vso[task.setvariable variable=DeployResult;isOutput=true]success"
                name: SetDeployResult
    
    - stage: PostDeployment
      dependsOn: Deploy
      jobs:
      - job: PostDeployJob
        variables:
          deployResult: $[ stageDependencies.Deploy.DeployJob.outputs['Deploy_Resource-1.SetDeployResult.DeployResult'] ]
        steps:
        - checkout: none
        - pwsh: |
            Write-Host "The deploy result from the Deploy stage is $(deployResult)."
          displayName: 'Use Output Variable from Deploy Stage'
    

    enter image description here

    Related documentations: