Reputation: 1336
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?
Upvotes: 1
Views: 1897
Reputation: 31075
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:
Upvotes: 5
Reputation: 1336
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
Upvotes: 2
Reputation: 41765
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.
Upvotes: 2