'How to get a job on a different stage in azure pipeline

i have something like this in my azure pipeline

pool:
  name: name
  vmImage: Image

stages:
  stage: 1
    jobs:
     job: 1
     job: 2
     job: 3
  stage: 2
    condition: will run if stage 1 on job 2 is successful
  stage: 3
    condition: will run if job 1 or 3 in stage 1 is successful or stage 
               2 is successful

can I get the job 1 or 3 in stage 1? or can I do a

  (dependencies.stage3.job1.result,"Succeeded")

like code?



Solution 1:[1]

How to get a job on a different stage in azure pipeline

You could add a task for job1,job2,job3 to set veriables for each job, like:

  - task: InlinePowershell@1
    displayName: 'SetVariableInJobA'
    inputs:
      Script: 'Write-Host "##vso[task.setvariable variable=JobA;isOutput=true]true"'
    name: JobAResult

Then we could set the condition based on the variables from the job1 to job3 in the stage1 by the syntax stageDependencies.stageName.jobName.outputs['stepName.variableName'].

Please check the document Jobs can access output variables from previous stages for some more details.

So, my final test YAML file looks like:

stages:
  - stage: A
    jobs:
    - job: jobA
      steps:
      - script: echo "This is JobA"
      - task: InlinePowershell@1
        displayName: 'SetVariableInJobA'
        inputs:
          Script: 'Write-Host "##vso[task.setvariable variable=JobA;isOutput=true]true"'
        name: JobAResult

    - job: jobB
      steps:
      - script: echo "This is JobB"
      - task: InlinePowershell@1
        displayName: 'SetVariableInJobB'
        inputs:
          Script: 'Write-Host "##vso[task.setvariable variable=JobB;isOutput=true]No"'
        name: JobBResult

    - job: jobC
      steps:
      - script: echo "This is JobC"
      - task: InlinePowershell@1
        displayName: 'SetVariableInJobC'
        inputs:
          Script: 'Write-Host "##vso[task.setvariable variable=JobC;isOutput=true]true"'
        name: JobCResult


  - stage: B
    variables:
      testB: $[ stageDependencies.A.jobB.outputs['JobBResult.JobB']]
    
    jobs:     
    - job: 
      condition: and(succeeded(), eq(variables.testB, 'Yes'))
      steps:
      - task: InlinePowershell@1
        inputs:
          Script: 'Write-Output ''$(testB)'''


  - stage: C
    variables:
      testA: $[ stageDependencies.A.jobA.outputs['JobAResult.JobA']]
      testB: $[ stageDependencies.A.jobB.outputs['JobBResult.JobB']]
      testC: $[ stageDependencies.A.jobC.outputs['JobCResult.JobC']]
    jobs:
    - job: 
      condition: and(succeeded(), or(eq(variables.testA, 'Yes'), or(eq(variables.testB, 'Yes'), eq(variables.testC, 'Yes'))))
      steps:
      - task: InlinePowershell@1
        inputs:
          Script: 'Write-Output ''hello world'''  

Note:

We could not set the condotion on the stage level directly, that is because the variable $[ stageDependencies.A.jobA.outputs['JobAResult.JobA']] is compile variable, and the varibale we set by the task is runtime variable.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Leo Liu-MSFT