Home > Net >  How to reference output variable from azure pipeline job that uses matrix strategy?
How to reference output variable from azure pipeline job that uses matrix strategy?

Time:11-23

I have the following azure pipeline yml (at the bottom, some parts omitted). I have 3 jobs in total. The first runs with a matrix strategy, the second runs normally and the third attempts to pick up output variables set by the previous two jobs.

The output variable reference for the second job seems to work fine. But when referencing the output variables rom the first job, I don't know what job name to use.The job is called 'Build' but the name seems to be influenced by the names in the strategy matrix. When I run:

echo $(Agent.JobName)
echo $(System.JobName)
echo $(System.JobDisplayName)

For a job called 'Build' with a matrix entry called 'linux' then I get:

Build linux
linux
Build linux

But using any of the following don't seem to work:

$[ dependencies.Build.outputs['SetJobStatus.x64linuxStatus'] ]
$[ dependencies.linux.outputs['SetJobStatus.x64linuxStatus'] ]
$[ dependencies.Build_linux.outputs['SetJobStatus.x64linuxStatus'] ]
$[ dependencies['Build linux'].outputs['SetJobStatus.x64linuxStatus'] ]

How is this meant to work? The fact that the reference to the second job works gives me hope but I'm clearly getting something wrong with the job name. I've tried so many combinations now that I've completely lost track of what I have and haven't tried.

When the results are reported, I see a value for the create_universal_macos_binary job status but not for the others which come from the first job.

stages:
- stage: Build
  jobs:
  - job: Build
    timeoutInMinutes: 90
    strategy:
      matrix:
          linux:
            tripletCompressed: 'x64linux'
          win64:
            tripletCompressed: 'x64windows'
          macOS_x64:
            tripletCompressed: 'x64osx'
          macOS_arm64:
            tripletCompressed: 'arm64osx'
    pool:
      vmImage: $(imageName)
    steps:

    ...

    # Set output variable with job status
    - bash: |
        echo $(Agent.JobName)
        echo $(System.JobName)
        echo $(System.JobDisplayName)
        echo "vso[task.setvariable variable=$(tripletCompressed)Status;isOutput=true]${AGENT_JOBSTATUS}"
        echo "##vso[task.setvariable variable=$(tripletCompressed)Status;isOutput=true]${AGENT_JOBSTATUS}"
      name: SetJobStatus
      displayName: Set Job Status Variable

  - job: create_universal_macos_binary
    displayName: 'Create universal MacOS binary'
    dependsOn: 'Build'
    condition: and(and(eq(${{ parameters.BUILD_MACOS_X64 }}, true), eq(${{ parameters.BUILD_MACOS_ARM64 }}, true)), ${{ containsValue(parameters.TARGET_PACKAGES, 'core') }})
    pool:
      vmImage: macOS-11

    ...

    # Set output variable with job status
    - bash: |
        echo $(Agent.JobName)
        echo $(System.JobName)
        echo $(System.JobDisplayName)
        echo "vso[task.setvariable variable=universalStatus;isOutput=true]${AGENT_JOBSTATUS}"
        echo "##vso[task.setvariable variable=universalStatus;isOutput=true]${AGENT_JOBSTATUS}"
      name: SetJobStatus
      displayName: Set Job Status Variable

  - job: message_teams
    displayName: 'Report Status to Teams'
    dependsOn:
      - 'Build'
      - create_universal_macos_binary
    pool:
      vmImage: ubuntu-20.04

    variables:
      windowsStatus: $[ dependencies.win64.outputs['SetJobStatus.x64windowsStatus'] ]
      linuxStatus: $[ dependencies['Build linux'].outputs['SetJobStatus.x64linuxStatus'] ]
      macosIntelStatus: $[ dependencies.macOS_x64.outputs['SetJobStatus.x64osxStatus'] ]
      macosArmStatus: $[ dependencies.macOS_arm64.outputs['SetJobStatus.arm64osxStatus'] ]
      universalStatus: $[ dependencies.create_universal_macos_binary.outputs['SetJobStatus.universalStatus'] ]

    steps:

    ...

    - task: PythonScript@0
      inputs:
        scriptSource: 'filePath'
        scriptPath: ./process-status.py
      env:
        CI_BUILD_ID: $(Build.BuildId)
        WINDOWS_STATUS: $(windowsStatus)
        LINUX_STATUS: $(linuxStatus)
        MACOS_INTEL_STATUS: $(macosIntelStatus)
        MACOS_ARM_STATUS: $(macosArmStatus)
        UNIVERSAL_STATUS: $(universalStatus)
      condition: and(succeededOrFailed(), ${{ parameters.REPORT_TO_TEAMS }})
      displayName: "Report result to Teams"

CodePudding user response:

you could see the enter image description here

To get the output variable in the next job in the same stage, you could use format : $[ dependencies.Build.outputs['linux.SetJobStatus.x64linuxStatus'] ]

In your issue, if you set variables in job 3 message_team as belows, you are supposed to use them.

variables:
  windowsStatus: $[ dependencies.Build.outputs['win64.SetJobStatus.x64windowsStatus'] ]
  linuxStatus: $[ dependencies.Build.outputs['linux.SetJobStatus.x64linuxStatus'] ]
  macosIntelStatus: $[ dependencies.Build.outputs['macOS_x64.SetJobStatus.x64osxStatus'] ]
  macosArmStatus: $[ dependencies.Build.outputs['macOS_arm64.SetJobStatus.arm64osxStatus'] ]
  universalStatus: $[ dependencies.Build.outputs['SetJobStatus.universalStatus'] ]

If you echo the variables here like this:

steps:
- bash: |
    echo $(windowsStatus)
    echo $(linuxStatus)
    echo $(macosIntelStatus)
    echo $(macosArmStatus)
    echo $(universalStatus)

You will able to get the output like this:

enter image description here

I hope this could do some help.

  • Related