0

I am generating output variables in matrix job A:

  - job: A
    strategy:
      matrix:
        nonprod:
          environment: test
        prod:
          environment: prod
    steps:
      - pwsh: Write-Host "##vso[task.setvariable variable=Hostname;isOutput=true]'StuffFrom$(environment)'"
        name: OutputVariable

Now I need to access them in subsequent matrix strategy in respective jobs:

  - job: B
    dependsOn: A
    strategy:
      matrix:
        nonprod:
          environment: test
        prod:
          environment: prod
    steps:
      - pwsh: Write-Host How can I use output variables from job A in here?
  • It seems you have figured out the solution. You may [Accept it as an Answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), this can be beneficial to other community members reading this thread. – Cece Dong - MSFT Mar 05 '20 at 09:24

3 Answers3

4

The matrix also accept a runtime expression containing a stringified JSON object. For example:

    jobs:
    - job: generator
      steps:
      - bash: echo "##vso[task.setVariable variable=legs;isOutput=true]{'a':{'myvar':'A'}, 'b':{'myvar':'B'}}"
        name: mtrx
      # This expands to the matrix
      #   a:
      #     myvar: A
      #   b:
      #     myvar: B
    - job: runner
      dependsOn: generator
      strategy:
        matrix: $[ dependencies.generator.outputs['mtrx.legs'] ]
      steps:
      - script: echo $(myvar) # echos A or B depending on which leg is running

Check here:

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/phases?tab=yaml&view=azure-devops&tabs=yaml#multi-job-configuration

RWAM
  • 6,760
  • 3
  • 32
  • 45
Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
  • Looks promising, but how to expand this to actually run two parallel jobs in generator which produce those outputs rather than hardcoding them? – Aurimas Stands with Ukraine Mar 04 '20 at 12:53
  • Can you elaborate it? How do you want to produce the outputs? – Cece Dong - MSFT Mar 05 '20 at 09:23
  • As an example I would create vms in job A one for test and one for prod in parallel, I would output their names and than use that in job B to perform some actions with each vm in parallel. – Aurimas Stands with Ukraine Mar 05 '20 at 09:34
  • @CeceDong-MSFT - I've set this up but in some situations the matrix is empty and then instead of not running the dependent job it fails on populating 'pool' from the empty matrix as we have `pool: $(imageName)` where imageName is normally set by the matrix. – MichaelJones Feb 14 '23 at 09:30
2

You need to map the output variable, then you can use them. you can do it in this way:

  jobs:
  - job: A
    strategy:
     matrix:
      nonprod:
        environment: test
      prod:
        environment: prod
    steps:
      - pwsh: Write-Host "##vso[task.setvariable variable=Hostname;isOutput=true]'StuffFrom$(environment)'"
        name: OutputVariable
  - job: B
    dependsOn: A
    variables:
        varFromAprod: $[ dependencies.A.outputs['prod.OutputVariable.Hostname'] ]
        varFromAnonprod: $[ dependencies.A.outputs['nonprod.OutputVariable.Hostname'] ]
    strategy:
     matrix:
      nonprod:
        environment: test
      prod:
        environment: prod
    steps:
      - pwsh: Write-Host "$(varFromAprod)"
        name: PrintVariableProd
      - pwsh: Write-Host "$(varFromAnonprod)"
        name: PrintVariableNonprod

More info you can find here.

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • I probably should have emphasised more strongly that I need to use each variable in respective jobs, that is I need output of job A nonprod to be used specifically in nonprod of jon B, I can see how this could be done in the powershell task itself with some simple if statement provided your answer, but it feels like there should be a more elegant solution to this? – Aurimas Stands with Ukraine Mar 03 '20 at 21:11
  • @AurimasN. I tried to make the variables available respective to the matrix but without success. if you will success please update here :) – Shayki Abramczyk Mar 04 '20 at 08:40
  • I've edited my post below to a cleaner version than using if statement in the pwsh task. – Aurimas Stands with Ukraine Mar 04 '20 at 12:21
2

Based on @Shayki Abramczyk answer I ended up using this approach:

  jobs:
  - job: A
    strategy:
     matrix:
      nonprod:
        environment: test
      prod:
        environment: prod
    steps:
      - pwsh: Write-Host "##vso[task.setvariable variable=Hostname;isOutput=true]'StuffFrom$(environment)'"
        name: OutputVariable
  - job: B
    dependsOn: A
    variables:
        varFromAprod: $[ dependencies.A.outputs['prod.OutputVariable.Hostname'] ]
        varFromAnonprod: $[ dependencies.A.outputs['nonprod.OutputVariable.Hostname'] ]
    strategy:
     matrix:
      nonprod:
        environment: test
        hostname: $(varFromAnonprod)
      prod:
        environment: prod
        hostname: $(varFromAprod)
    steps:
      - pwsh: |
          Write-Host Hostname for this job is $Hostname