3

I have a pipeline yaml file that runs a series of stages, each of which runs a single deployment job that has been templatized. The code itself is pretty straightforward:

template.yaml

jobs:
- deployment: foo
...

pipeline.yaml

stages:
- stage: Uno
  displayName: Numero_uno
  jobs:
  - job: Foo
    steps:
    - template:  template.yaml
      parameters:
     stuff: things
- stage: Dos
  displayName: Numero_dos
  jobs:
  - job: Foo
    steps:
    - template:  template.yaml
      parameters:
     stuff: things

I've discovered that one of my stages needs some extra scripting run within it, so I want to add some extra jobs to this particular stage:

- stage: Cuarenta_y_dos
  displayName: Numero_cuarenta_y_dos
  jobs:
  - job: prep
    steps:
    - task: ...
  - job: Foo
    steps:
      - template: template.yaml
        parameters:
          stuff: things
  - job: unprep
    steps:
    - task: ...

Doing so breaks the entire pipeline though, as my template is constructed around re-usable jobs and it immediately throws an error about "Unexpected value 'jobs'".

I understand why it's happening, but I'm not sure how to address it. Is what I want to do even possible?

Dlayknee
  • 123
  • 8

2 Answers2

4

Figured it out. My issues were two-fold:

  1. I had the template at the job level, not the steps level
  2. I was specifying "- job:" under the "jobs:" entries on the templated steps (that only had the single job).

Original code, not working:

stages:
- stage: Uno
  displayName: Numero_uno
  jobs:
  - job: Foo
    steps:
    - template:  template.yaml
      parameters:
        stuff: things

- stage: Cuarenta_y_dos
  displayName: Numero_cuarenta_y_dos
  jobs:
  - job: prep
    steps:
    - task: ...
  - job: Foo
    steps:
      - template: template.yaml
        parameters:
          stuff: things
  - job: unprep
    steps:
    - task: ...

Fixed code:

stages:
- stage: Uno
  displayName: Numero_uno
  jobs:
  - template:  template.yaml
      parameters:
      stuff: things

- stage: Cuarenta_y_dos
  displayName: Numero_cuarenta_y_dos
  jobs:
  - job: prep
    steps:
    - task: ...
  - template: template.yaml
    parameters:
      stuff: things
  - job: unprep
    steps:
    - task: ...

It's minor, but it mattered!

Dlayknee
  • 123
  • 8
2

Yes, Azure Devops pipeline can have both templated and untemplated jobs. Even more, jobs that references a template can also have regular tasks. See example below:

stages:
- stage: CIBuild
  displayName: 'Build'
  jobs:
  - job: Build
    pool:
      vmImage: ubuntu-latest
    steps:
      - task: PowerShell@2
        ...
           
      - template: PipelineTemplates/ci-template.yml@templates
        parameters: ...
        
  - job: Test
    displayName: 'Test job'
    pool:
      vmImage: ubuntu-latest
    steps:
      - task: PowerShell@2
        ...

I'm not sure why you're having that issue but maybe your .yaml is invalid. Check if it's valid by clicking on dots in top right corner and select "Validate":

Validate yaml

Let us know the results.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77