How to Reference YAML Templates Across Projects in Azure DevOps?

Arafat Mohammed 0 Reputation points
2025-04-25T17:06:58.5933333+00:00

How to Reference YAML Templates Across Projects in Azure DevOps?

Hi all,

I'm trying to implement a shared pipeline strategy in Azure DevOps and would really appreciate some guidance on whether this setup is supported or if there's a workaround others are using.


What I'm trying to accomplish

I want to reuse YAML pipeline templates that are stored in one Azure DevOps project (DevOps), and reference them in a pipeline defined in a different project (WebSuite) all within the same Azure DevOps organization.


My setup

  • Template location (Project A):
    Repository: DevOps/FrontendApp-Pipeline
    Templates: /templates/build.yml, /templates/test.yml
  • Pipeline location (Project B):
    Repository: WebSuite/FrontendApp-Pipelines
    File: azure-pipelines.yml

What I've tried so far

In the pipeline YAML, I added the external repository using a resources.repositories block:

resources:
  repositories:
    - repository: external
      type: git
      name: DevOps/FrontendApp-Pipeline
      endpoint: FrontendApp-Pipeline-ServiceConnection

Then referenced a template like this:

- template: templates/build.yml@external

I also created a service connection in the WebSuite project:

  • Type: Other Git
  • Authentication: Basic Authentication using a valid Personal Access Token (PAT) with Code (Read) access
  • URL: https://dev.azure.com/[org]/DevOps/_git/FrontendApp-Pipeline

The problem

Even with a successful service connection, the pipeline fails to start, resulting in an "informational run" with this error:

"There was a YAML error preventing Azure Pipelines from determining if the pipeline should run."

I've tried reducing the pipeline to just this for debugging:

steps:
  - checkout: external
  - script: ls -R $(Build.SourcesDirectory)

But the same error persists.


My questions

  1. Is referencing a YAML template from a repository in a different Azure DevOps project supported?
  2. If yes, what is the correct way to do it?
  3. If not, are there any best practices for reusing templates across projects --- like GitHub mirrors, submodules, or another pattern?

Any insight would be greatly appreciated --- I'd love to avoid duplicating templates across projects if there's a supported solution.

Thanks in advance, Arafat

Azure DevOps
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Suwarna S Kale 2,906 Reputation points
    2025-04-28T23:06:47.7133333+00:00

    Hello Arafat Mohammed,

    Thank you for posting your question in the Microsoft Q&A forum. 

    Yes, referencing YAML templates across Azure DevOps projects is fully supported. Your approach is fundamentally correct but needs these adjustments: 

    Service Connection Configuration: 

    • Use "Azure Repos/Team Foundation Server" type (not "Other Git") 
    • The endpoint URL should be just *https://dev.azure.com/[org]* 
    • PAT needs Build (Read) permission, not just Code (Read) 

    Repository Resource Syntax (yaml):

     

    resources:
       
          - repository: templates
              type: git
              name: DevOps/FrontendApp-Pipeline
              ref: main  # explicit branch reference
    
    

    The challenge of reusing YAML pipeline templates across different Azure DevOps projects within the same organization represents a common architectural need for many development teams. While Azure DevOps does support this functionality, proper implementation requires careful attention to several configuration details that differ from standard intra-project template references. 

    The fundamental architecture involves declaring the external repository as a resource in your pipeline configuration. This approach leverages Azure DevOps' built-in capabilities for cross-project collaboration, but success depends on three critical elements: the correct service connection type, appropriate permissions, and precise reference syntax. The service connection must be configured as "Azure Repos/Team Foundation Server" rather than the more generic "Other Git" option, with authentication handled through a Personal Access Token that includes Build (Read) permissions rather than just basic Code access. 

    Alternative Approaches: 

    • Extension Method: Package templates as a custom extension 
    • GitHub Mirror: Push templates to a GitHub repo (simpler auth model) 
    • Pipeline Artifacts: Publish templates as build artifacts and download them 

     

    If the above answer helped, please do not forget to "Accept Answer" as this may help other community members to refer the info if facing a similar issue. Your contribution to the Microsoft Q&A community is highly appreciated. 

    0 comments No comments

  2. Gaurav Kumar 380 Reputation points Microsoft External Staff Moderator
    2025-04-29T10:52:46.8466667+00:00

    Hi @Arafat Mohammed ,

    If you're trying to reuse YAML templates across different Azure DevOps projects, yes, it’s possible, but you need to set it up properly.

    Instead of using a "Generic Git" (Other Git) connection, you need to create a Service Connection of type Azure Repos / Git.

    Use the correct Service Connection

    • Create a Service Connection of type Azure Repos / Git (not "Other Git").
    • Go to Project Settings → Service Connections → New Service Connection → Azure Repos / Git.
    • Use a PAT (Personal Access Token) with Code Read permissions.
    • Approve the Service Connection for use in pipelines.

    Refer this MS Document - GitHub service connection

    Reference the external repository in your pipeline:

    resources:
      repositories:
        - repository: external
          type: git
          name: DevOps/FrontendApp-Pipeline   
          endpoint: FrontendApp-Pipeline-ServiceConnection
    

    Use the template from the external repo:

    stages:
    - template: templates/build.yml@external
    

    Important checks

    • Service Connection has access to the repository.
    • Repository permissions allow read access.
    • No need for checkout: external unless you want to work with the files directly.

    Hope it helps!


    Please do not forget to click "Accept the answer” and Yes wherever the information provided helps you, this can be beneficial to other community members.

    If you have any other questions or still running into more issues, let me know in the "comments" and I would be happy to help you.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.