Search code examples
azure-devopsazure-pipelinescicdazure-pipelines-yaml

Azure Pipelines: Unable to pass a template


I have an azure-pipeline.yml file that extends to another template (called build-and-deploy.yml) in a different repo, which in turn refers to 3 different yml templates (called 0.yml, 1.yml and 2.yml), in the same repo.

The issue is that azure-pipeline.yml passes a list of build jobs (among other things) listed in a pipeline/build.yml file to build-and-deploy.yml as a parameter, which is in turn passed to one of 0.yml, 1.yml or 2.yml depending on some condition.

Now, azure-pipeline.yml is in repoA and build-and-deploy.yml, along with 0.yml, 1.yml and 2.yml in demo.

For some reason, 0/1/2.yml is looking for pipeline/build.yml in its own repo (demo) and not in repoA. Now I know I can checkout repoA in build-and-deploy.yml, but the repo demo is used by other repos too (like repoB, repoC etc - not in context here), so I cannot hardcode it here.

Any suggestions?

azure-pipeline.yml:

...

parameters:
- name: allowDevDeployment
  displayName: "Allow this build to be deployed to DEV"
  type: boolean
  default: false

- name: selectDeploymentType
  displayName: Select deployment type
  type: string
  default: 0
  values:
    - 0 # (Standard/regular deployment)
    - 1 #(Single branch; master deploys to  DEV -> DEVTEST -> STAGE -> PROD)
    - 2 # (develop to DEV -> Auto PR to master -> DEVTEST -> STAGE -> PROD)


extends:
  template: templates/build-and-deploy.yml@demo
  parameters:
    allowDevDeployment: ${{ parameters.allowDevDeployment }}
    deploymentType: ${{ parameters.selectDeploymentType}}
    helmVersion: 3.12.3
    buildJobs:
    - template: pipeline/build.yml
    - template: pipeline/static-scans.yml

in build-and-deploy.yml:

parameters:
  ...
- name: deploymentType
  type: string
  default: none

stages:
  - ${{ if eq(parameters.deploymentType, 0)}}:
    - template: deploymentTypes/0.yml
      parameters:
        allowDevDeployment: ${{ parameters.allowDevDeployment }}
        ...
  - ${{ if eq(parameters.deploymentType, 1)}}:
    - template: deploymentTypes/1.yml
      parameters:
        allowDevDeployment: ${{ parameters.allowDevDeployment }}
        ...
  - ${{ if eq(parameters.deploymentType, 2)}}:
    - template: deploymentTypes/2.yml
      parameters:
        allowDevDeployment: ${{ parameters.allowDevDeployment }}
        ...

in 0.yml:

parameters:

...
- name: allowDevDeployment
  type: boolean
  default: false
    
stages:
- ${{ if gt(length(parameters.buildJobs), 0) }}:
  - stage: Build
    jobs:
    - ${{ each job in parameters.buildJobs }}:
      - ${{ if startsWith(job.pool.vmImage, 'ubuntu') }}:
        - job: ${{ job.job }}
          pool: ${{ job.pool }}
          ${{ if job.condition }}:
            condition: ${{ job.condition }}
            ...
# Develop, Nightly or Support Branch
- ${{ if or(in(variables['Build.SourceBranchName'], 'develop', 'nightly'), contains(variables['Build.SourceBranch'], '/support/'), eq(parameters.allowDevDeployment, true)) }}:
  - ${{ if gt(length(parameters.devJobs), 0) }}:
    - stage: Dev
      ...

# Develop or Support Branch
- ${{ if or(eq(variables['Build.SourceBranchName'], 'develop'), contains(variables['Build.SourceBranch'], '/support/')) }}:
  - ${{ if gt(length(parameters.devTestJobs), 0) }}:
    - stage: DevTest
      ${{ if ne(parameters.devTestStageLabel, 'none') }}:
        displayName: ${{ parameters.devTestStageLabel }}
        ...

This is continued from Trouble with Azure Pipelines in defining stages and branches.


Solution

  • You can use @self to refer to templates in the current repository:

    extends:
      template: templates/build-and-deploy.yml@demo # <----------------- reference template from repository 'demo'
      parameters:
        allowDevDeployment: ${{ parameters.allowDevDeployment }}
        deploymentType: ${{ parameters.selectDeploymentType}}
        helmVersion: 3.12.3
        buildJobs:
        - template: pipeline/build.yml@self          # <----------------- reference template from current repository
        - template: pipeline/static-scans.yml@self   # <----------------- reference template from current repository
    ```