Smjert
Smjert

Reputation: 425

Azure Pipelines YAML how to use variables between jobs that have strategies

Let's say I'm starting with this configuration that works:

jobs:
  - job: ARelease

    pool:
      vmImage: 'Ubuntu-16.04'

    steps:
    - script: |
        echo "##vso[task.setvariable variable=ReleaseVar;isOutput=true]1"
      name: JobResult


  - job: C

    pool:
      vmImage: 'Ubuntu-16.04'

    dependsOn: ARelease

    variables:
      AVar: $[ dependencies.A.outputs['JobResult.ReleaseVar'] ]

    steps:
    - script: |
        echo $(AVar)

As expected job C outputs 1.

Now let's say that I have to add a new job ADebug, which is almost identical to ARelease, so I use a strategy:

jobs:
  - job: A

    strategy:
      matrix:
        Release:
          BUILD_TYPE: Release
        Debug:
          BUILD_TYPE: Debug

    pool:
      vmImage: 'Ubuntu-16.04'

    steps:
    - script: |
        echo "##vso[task.setvariable variable=$(BUILD_TYPE)Var;isOutput=true]1"
      name: JobResult


  - job: C

    pool:
      vmImage: 'Ubuntu-16.04'

    dependsOn: A

    variables:
      AReleaseVar: $[ dependencies.A.outputs['JobResult.ReleaseVar'] ]
      ADebugVar: $[ dependencies.A.outputs['JobResult.DebugVar'] ]

    steps:
    - script: |
        echo $(AReleaseVar)
        echo $(ADebugVar)

I would expect that everything works and that I can see the outputs.. but the output is empty.

Queuing the job with diagnostics it seems that $[ dependencies.A.outputs['JobResult.ReleaseVar'] ] and $[ dependencies.A.outputs['JobResult.DebugVar'] ] evaluate to Null. I've tried different variations to access those variables, but it always evaluates to Null.

Any idea what is the correct way?

Upvotes: 3

Views: 2923

Answers (1)

myermian
myermian

Reputation: 32515

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#set-a-multi-job-output-variable

If you're setting a variable from a matrix or slice, then to reference the variable, you have to include the name of the job as well as the step when you access it from a downstream job.

The format is as follows: dependencies.{job}.outputs['{matrix/slice key}.{step name}.{variable name}']


In your scenario, you have Job A run with a matrix strategy (Release, Debug), and you respectively set the variable names to be ReleaseVar and DebugVar. The way to accurately access these variables is:

  • dependencies.A.outputs['Release.JobResult.ReleaseVar']
  • dependencies.A.outputs['Debug.JobResult.DebugVar']

On a side note, perhaps just use the same variable name since it you are already able to distinguish between the values based on the matrix name.

Upvotes: 3

Related Questions