Miq
Miq

Reputation: 1

Azure DevOps: How can I nest YML template within YML template when both are defined as resource

The following example fails with an error. Keyword "resources" is not allowed in nested templates. "buildAndPackage.yml" cannot declare "resources".

pipeline.yml:

resources:
  repositories:
  - repository: templates
    type: git
    name: templateLibrary/pipeline

stages:

- template: buildAndPackage.yml@templates
  parameters:
  - projectName: 'myProject'

buildAndPackage.yml in templateLibrary/pipeline repo:

parameters:
- name: projectName
  default: ''

resources:
  repositories:
  - repository: templates
    type: git
    name: templateLibrary/pipeline

stages:
- stage: Build_${{ parameters.projectName }}
  jobs:

  - template: build.yml@templates
    parameters:
    - projectName: ${{ parameters.projectName }}

  - template: package.yml@templates
    parameters:
    - projectName: ${{ parameters.projectName }}

build.yml in templateLibrary/pipeline repo:

parameters:
- name: projectName
  default: ''

Upvotes: 0

Views: 793

Answers (2)

qbik
qbik

Reputation: 5908

It can be done in a simpler, cleaner way. If the nested template (build.yml) is located in the same repository as the main one (buildAndPackage.yml), you can just use relative paths to reference it:

# buildAndPackage.yml

parameters:
- name: projectName
  default: ''

stages:
- stage: Build_${{ parameters.projectName }}
  jobs:

  - template: build.yml
    parameters:
    - projectName: ${{ parameters.projectName }}

  - template: package.yml
    parameters:
    - projectName: ${{ parameters.projectName }}

Upvotes: 1

Miq
Miq

Reputation: 1

I knew the answer before asking the question here, I just wanted to share the solution. The template that was not resolved in nested template can be, instead, resolved in pipeline.yml and passed as a parameter. This is not a clean solution as it breaks "separation of concern" principle in Onion Architecture, but at least it solves the issue.

pipeline.yml:

...

resources:
  repositories:
  - repository: templates
    type: git
    name: templateLibrary/pipeline

stages:

- template: buildAndPackage.yml@templates
  parameters:
  - projectName: 'myProject'
  - buildYml: build.yml@templates
  - packageYml: package.yml@templates

buildAndPackage.yml in templateLibrary/pipeline repo:

parameters:
- name: projectName
  default: ''
- name: buildYml
  type: object
- name: packageYml
  type: object

stages:
- stage: Build_${{ parameters.projectName }}
  jobs:

  - template: ${{ parameters.buildYml }}
    parameters:
    - projectName: ${{ parameters.projectName }}

  - template: ${{ parameters.packageYml }}
    parameters:
    - projectName: ${{ parameters.projectName }}

build.yml in templateLibrary/pipeline repo:

parameters:
- name: projectName
  default: ''

Upvotes: 0

Related Questions