0

I'm trying to figure how to dynamically create a dependency and run the job based on the condition.

Here is the structure of my pipeline:

main.yaml:

stages:
- stage: build
  jobs:
  - template: build.yaml

- stage: deployDev
  dependsOn: build
  jobs:
  - template: deployApp1.yaml
    parameters:
      environmentName: Dev
  - template: deployApp2.yaml
    parameters:
      environmentName: Dev

- stage: deployQA
  dependsOn: deployDev
  jobs:
  - template: promote.yaml
    parameters:
      environmentName: QA
  - template: deployApp1.yaml
    parameters:
      environmentName: QA
  - template: deployApp2.yaml
    parameters:
      environmentName: QA

promote.yaml

jobs:
- job: copy
  steps:
  - task:

deployApp1.yaml

jobs:
-job: deployApp1
 steps:
 - task:

deployApp2.yaml

jobs:
- job: deployApp2
  steps:
  - task:

In deployQA i have a separate job which copies the build artifacts and the next two jobs (deployApp1 and deployApp2) will fail without the copy step in deployQA.

I would like to create a conditional dependency on job: copy for job: deployApp1 so that it should be able to skip if i'm deploying to Dev which doesn't have this dependency. I already tried different solutions from different posts without any luck.

I know if i can add additional stage for the copy that would solve my problem but i would like to have the copy as part of the QA stage.

Ia1
  • 500
  • 1
  • 7
  • 15

2 Answers2

0

You want deployApp1 to depend on copy when running in stage deployQA and not depend on anything when running in stage deployDev?

You could add a dependsOn parameter to your template and use it to control job's dependencies:

stages:
- stage: build
  jobs:
  - template: build.yaml

- stage: deployDev
  dependsOn: build
  jobs:
  - template: deployApp1.yaml
    parameters:
      environmentName: Dev
  - template: deployApp2.yaml
    parameters:
      environmentName: Dev

- stage: deployQA
  dependsOn: deployDev
  jobs:
  - template: promote.yaml
    parameters:
      environmentName: QA
  - template: deployApp1.yaml
    parameters:
      environmentName: QA
      dependsOn: copy
  - template: deployApp2.yaml
    parameters:
      environmentName: QA
      dependsOn: copy

# promote.yaml

jobs:
- job: copy
  steps:
  - task:

# deployApp1.yaml

parameters:
- name: environmentName
- name: dependsOn
  default: []

jobs:
- job: deployApp1
  dependsOn: ${{ parameters.dependsOn }}
  steps:
  - task:

# deployApp2.yaml
parameters:
- name: environmentName
- name: dependsOn 
  default: []

jobs:
- job: deployApp2
  dependsOn: ${{ parameters.dependsOn }}
  steps:
  - task:
qbik
  • 5,502
  • 2
  • 27
  • 33
  • Thank you for the solution. I haven't tried your solution yet but i'm very much interested in trying this solution. – Ia1 Jun 28 '21 at 19:03
0

I was able to figure out the solution for my scenario from below post however to be able to achieve the answer to my specific use-case i had to set the makeExplicitDependency parameter to false.

${{ if eq(parameters.makeExplicitDependency, true) }}: dependsOn: Test

thanks to below post: How to dynamically reference previous jobs in Azure Pipelines if there are any in the current stage

Ia1
  • 500
  • 1
  • 7
  • 15