0
votes

I am trying to set up an azure yaml pipeline that uses two deployment templates for two respective jobs (test and deploy). The jobs should be run in sequence for each stage, as the test-job creates an artifact that the deploy-job consumes. This works nicely.

However, for one environment I have split the deployment into two stages, one stage that only runs tests and one stage that runs the actual deployment. The problem I run into is that my deploy-job has a "dependson" which references the test-job. Because of this, my pipeline is invalid when I add the these two stages for my environment.

My question is if there is some sort of dynamic "dependson" reference for a job, something like "depends on any previous jobs in this stage, if there are any"?

Snippets of code from the yaml-files below for reference:

Main.yaml

#This first stage would work as it uses both templates in succession

stages:
  - stage: BothTemplates
    displayName: 'Run both templates'
    jobs:
    - template: TemplateTest.yml
    - template: TemplateDeploy.yml #depends on the job in TemplateTest.yml

# The pipeline is invalid because of OnlyDeploy, as the TemplateDeploy.yml depends in the job "Test", which does not exist in the OnlyDeploy-context

  - stage: OnlyTest
    dependsOn: BothTemplates
    displayName: 'Run only test template'
    jobs:
    - template: TemplateTest.yml

  - stage: OnlyDeploy
    dependsOn: OnlyTest
    displayName: 'Run only deploy template'
    jobs:
    - template: TemplateDeploy.yml

TemplateTest.yaml

jobs:
- deployment: Test
  dependsOn: 
  displayName: Test
  continueOnError: false
  strategy:
    runOnce:
      deploy:
        steps:
          #Here the steps for the tests are

Current TemplateDeploy.yaml

jobs:
- deployment: Deploy
  dependsOn: Test
  displayName: Deploy
  continueOnError: false
  strategy:
    runOnce:
      deploy:
        steps:
          #Here the steps for the deployment are

My idea would be to change TemplateDeploy.yaml to something like this:

jobs:
- deployment: Deploy
  dependsOn: previousJob() #Wait until previous job in stage has finished, if there are any
  displayName: Deploy
  continueOnError: false
  strategy:
    runOnce:
      deploy:
        steps:
          #Here the steps for the deployment are
1

1 Answers

1
votes

Since you already have dependencies between stages

  - stage: OnlyTest
    dependsOn: BothTemplates
    displayName: 'Run only test template'
    jobs:
    - template: TemplateTest.yml

  - stage: OnlyDeploy
    dependsOn: OnlyTest
    displayName: 'Run only deploy template'
    jobs:
    - template: TemplateDeploy.yml

I think that you actually don't need here dependency to a job on TemplateDeploy.yml but if you want to make this dependent on previous jobs you can achieve this using parameters


parameters:
- name: makeExplicitDependency
  displayName: 'Make excplicit job dependency'
  type: boolean
  default: true

jobs:
- deployment: Deploy
  ${{ if eq(parameters.makeExplicitDependency, true) }}:
     dependsOn: Test
  displayName: Deploy
  continueOnError: false
  strategy:
    runOnce:
      deploy:
        steps:
          #Here the steps for the deployment are

and then:


stages:
  - stage: BothTemplates
    displayName: 'Run both templates'
    jobs:
    - template: TemplateTest.yaml
    - template: TemplateDeploy.yaml #depends on the job in TemplateTest.yml

  - stage: OnlyTest
    dependsOn: BothTemplates
    displayName: 'Run only test template'
    jobs:
    - template: TemplateTest.yaml

  - stage: OnlyDeploy
    dependsOn: OnlyTest
    displayName: 'Run only deploy template'
    jobs:
    - template: TemplateDeploy.yaml
      parameters:
        makeExplicitDependency: false

So removing dependsOn works like your expected dependsOn: previousJob() #Wait until previous job in stage has finished, if there are any