7
votes

I use Azure DevOps Templates in Stage and I want some job to start only when Job from template is completed (dependsOn):

- stage: stage1
  jobs:
  - job: job1
    steps:
    - bash: |
      ...

  - template: template1.yml
    parameters:
      param1: 'val1'

  - job: job2
    **dependsOn: how to put `template: template1.yml` here?**
    steps:
    - bash: |
      ...

How could it be done?

2
is template1 a part of job1? in that case just depend on the whole job1 - 4c74356b41
no, it's dedicated one, that's why I'm asking - dependsOn: template doesn't work - prnt.sc/qtojej - kagarlickij
well, you can make it part of its own job and then template the steps? and depend on that new job - 4c74356b41
It will require template to be changed from Job template (docs.microsoft.com/en-us/azure/devops/pipelines/…) to Step template (docs.microsoft.com/en-us/azure/devops/pipelines/…) which is not bad, but doesn't feed my requirements to template functionality - kagarlickij

2 Answers

12
votes

Building on Eric Smith's answer you can pass in the name of the job that the template will depend on as a parameter.

#template1.yml

jobs:
- job: mytemplateJob1
  steps:
  - script: npm install
#template2.yml
parameters:
  DependsOn: []

jobs:
- job: mytemplateJob2
  dependsOn: ${{ parameters.DependsOn }}
  steps:
  - bash: pwd

By setting the default value for DependsOn to [] you ensure that the template will run if no value is passed in for DependsOn but you can optionally create a dependency like this:

stages:
- stage: stage1
  jobs:
  - template: template1.yml  # Template reference

  - template: template2.yml
    parameters:
      DependsOn: 'mytemplateJob1'
7
votes

You can accomplish this by using the name of the job, as it is defined in your template in the dependsOn.

#template1.yml

jobs:
- job: mytemplateJob
  steps:
  - script: npm install

and

stages:
- stage: stage1
  jobs:
    - job: job1
      steps:
      - bash: pwd

    - template: template1.yml  # Template reference
      parameters:
        param: 'val1'

    - job: job2
      dependsOn: mytemplateJob
      steps:
        - bash: pwd