azureazure-devopsazure-pipelinesazure-pipelines-yaml

Output variable for use in future stages


Here is my YAML file. When my Redis stage is run it successfully creates my Azure Redis cache server and sent an output to next task RedisSetvar and it successfully writes my output host and displays the value.

  - stage: Redis
    dependsOn: "Information"
    jobs:
      - deployment: "Redis"
        pool:
          name: "Azure VM 2019"
        environment: "dev"
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: ""
                  path: ""
                - task: AzureResourceManagerTemplateDeployment@3
                  name: Redis 
                  displayName: "Redis"
                  inputs:
                    deploymentScope: "Resource Group"
                    azureResourceManagerConnection: "$(GlobalResourceManagerConnection)"
                    subscriptionId: "$(GlobalSubscriptionId)"
                    action: "Create Or Update Resource Group"
                    resourceGroupName: "$(ResourceGroupNameValue)"
                    location: "$(GlobalLocation)"
                    templateLocation: "Linked artifact"
                    deploymentMode: "Incremental"
                    csmFile: "$(Agent.BuildDirectory)/abc/Pipelines/Deployment/Architecture/Redis/Redis.azuredeploy.json"
                    csmParametersFile: "$(Agent.BuildDirectory)/abcPipelines/Deployment/Architecture/Redis/Redis.azuredeploy.parameters.json"
                    overrideParameters: '-clientName "$(clientName)" -resourcePrefix "$(resourcePrefix)"'
                    deploymentOutputs: "RedisResourcesOutputs"
                - task: PowerShell@2
                  name: RedisSetvar
                  displayName: "Display RedisOutputs"
                  inputs:
                    targetType: "inline"
                    script: |
                      $RedisdeploymentOutput = ConvertFrom-Json -InputObject '$(RedisResourcesOutputs)'
                      $redisKey = $RedisdeploymentOutput.RedisKey.value
                      Write-Host "redisKey: $redisKey"
                      Write-Host "##vso[task.setvariable variable=redisKey;isOutput=true]$redisKey"

Issue comes in next stage where I want to get this output and want to display in next stage which is my app service. It's not a value though it's just null.

- stage: AppServices
    dependsOn:
    - Redis
    variables:
      redisKey: $[stageDependencies.Redis.Redis.outputs['RedisSetvar.redisKey']]
                       
    jobs:
      - deployment: "AppServices"
        pool:
          name: "Azure VM 2019"
        environment: "dev"
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: "abc"
                  path: "abc"
                - task: PowerShell@2
                  displayName: "Print Variables"
                  inputs:
                    targetType: "inline"
                    script: |
                      Write-Host "redisKey: $(redisKey)"

Solution

  • To reference an output variable from a previous deploy job in another stage, you should do like as below:

    1. If the previous deploy job only has the environment specified, you should use the expression "$[ stageDependencies.<stage-name>.<job-name>.outputs['<job-name>.<step-name>.<variable-name>'] ]" to access the output variable. See below example.
    stages:
    - stage: stage1
      jobs:
      - deployment: deploy01
        environment: 'EnvVM'  # Only specify the environment.
        strategy:
          runOnce:
            deploy:
              steps:
              - pwsh: |
                  Write-Host "##vso[task.setvariable variable=myOutVar;isOutput=true]abc123"
                name: setOutVar
                displayName: 'Set output variable'
    
    - stage: stage2
      dependsOn: stage1
      variables:
        getOutVar: $[ stageDependencies.stage1.deploy01.outputs['deploy01.setOutVar.myOutVar'] ]
      jobs:
      - deployment: deploy02
        environment: MyEnv02
        strategy:
          runOnce:
            deploy:
             steps:
              - pwsh: |
                  Write-Host "getOutVar = $(getOutVar)"
                displayName: 'Print output variable'
    

    enter image description here

    1. If the previous deploy job has detailed resource specified from the environment, you should use the expression "$[ stageDependencies.<stage-name>.<job-name>.outputs['Deploy_<resource-name>.<step-name>.<variable-name>'] ]" to access the output variable. See below example.
    stages:
    - stage: stage1
      jobs:
      - deployment: deploy01
        environment:
          name: 'EnvVM'
          resourceName: 'Win11-01'  # Specified a detailed resource from the environment.
        strategy:
          runOnce:
            deploy:
              steps:
              - pwsh: |
                  Write-Host "##vso[task.setvariable variable=myOutVar;isOutput=true]abc123"
                name: setOutVar
                displayName: 'Set output variable'
    
    - stage: stage2
      dependsOn: stage1
      variables:
        getOutVar: $[ stageDependencies.stage1.deploy01.outputs['Deploy_Win11-01.setOutVar.myOutVar'] ]
      jobs:
      - deployment: deploy02
        environment: MyEnv02
        strategy:
          runOnce:
            deploy:
             steps:
              - pwsh: |
                  Write-Host "getOutVar = $(getOutVar)"
                displayName: 'Print output variable'
    

    enter image description here

    Related documentations: