Search code examples
azure-devopsazure-devops-services

How do stage output variables work in Azure DevOps?


I have the following piece of YAML code:

stages:
  - stage: ApplyDnsPrefixes
    condition: and(succeeded(), ne(dependencies.PlanDnsPrefixes.outputs['PlanDnsPrefixes.COMPUTE_DNS_PREFIXES_PLAN.HAS_ABSENT'], 0))
    variables:
      HAS_ABSENT: $[dependencies.PlanDnsPrefixes.outputs['PlanDnsPrefixes.COMPUTE_DNS_PREFIXES_PLAN.HAS_ABSENT']]
      HAS_ABSENT_1: $[dependencies.PlanDnsPrefixes.PlanDnsPrefixes.outputs['PlanDnsPrefixes.COMPUTE_DNS_PREFIXES_PLAN.HAS_ABSENT']]
      HAS_ABSENT_2: $[dependencies.PlanDnsPrefixes.PlanDnsPrefixes.outputs['COMPUTE_DNS_PREFIXES_PLAN.HAS_ABSENT']]
      HAS_ABSENT_3: $[stageDependencies.PlanDnsPrefixes.outputs['PlanDnsPrefixes.COMPUTE_DNS_PREFIXES_PLAN.HAS_ABSENT']]
      HAS_ABSENT_4: $[stageDependencies.PlanDnsPrefixes.PlanDnsPrefixes.outputs['PlanDnsPrefixes.COMPUTE_DNS_PREFIXES_PLAN.HAS_ABSENT']]
      HAS_ABSENT_5: $[stageDependencies.PlanDnsPrefixes.PlanDnsPrefixes.outputs['COMPUTE_DNS_PREFIXES_PLAN.HAS_ABSENT']]

The stage is invoked, so the condition is satisfied. Therefore dependencies.PlanDnsPrefixes.outputs['PlanDnsPrefixes.COMPUTE_DNS_PREFIXES_PLAN.HAS_ABSENT'] is not zero.

But then trying to set a stage level variable to that value does not work - it produces an empty string!

As you can see, I am trying 6 different variants and then I output the environment using printenv|sort command (not shown). All the 6 variants are empty!!!

enter image description here

What does one have to do to make it work?


Solution

  • Found the problem. So I did not describe it in the question, but now I understand it matters.

    There are three stages actually here:

    PlanDnsPrefixes -> Apply -> ApplyDnsPrefixes
    

    In this layout we have 2 implicit direct dependencies:

    1. Apply depends on PlanDnsPrefixes
    2. ApplyDnsPrefixes depends on Apply

    However, the dependency I care about is of ApplyDnsPrefixes on PlanDnsPrefixes, which is an implicit transitive dependency and apparently this is not enough for ApplyDnsPrefixes to see output variables of PlanDnsPrefixes. We must specify the dependency explicitly:

        stage: ApplyDnsPrefixes
        dependsOn:
          - PlanDnsPrefixes
          - Apply
        condition: and(succeeded(), ne(dependencies.PlanDnsPrefixes.outputs['PlanDnsPrefixes.COMPUTE_DNS_PREFIXES_PLAN.HAS_ABSENT'], 0))
        variables:
          ABSENT_DNS_PREFIXES: $[stageDependencies.PlanDnsPrefixes.PlanDnsPrefixes.outputs['COMPUTE_DNS_PREFIXES_PLAN.ABSENT']]
        displayName: Apply DNS Prefixes
    

    I find it confusing that condition and variables refer to the same output variable differently.