Execute pipeline tasks from different Azure DevOps Organizations

Overview

Azure DevOps pipelines can reference pipeline jobs and tasks from repositories in other organizations via a template.

This sample app demonstrates how to use and call Azure Pipelines tasks template that exists in different Azure DevOps organization. The pipeline calls two different templates, one which is in the same repository and the other which is stored in a different Azure DevOps organization.

remote-access-diagram

Getting Started

Folder Structure

Here's the folder structure for the sample:

  • build
    • azure-pipelines.yaml - Main Azure Pipelines yaml file to create pipeline
    • templates
      • hello-alpha.yaml - Template file in the same repository to run a task

Prerequisites

Running the Sample

To run this sample, follow the steps below:

  1. Fork this repository in your Github account and clone it.

  2. Create two different organizations, for example, organization-alpha and organization-beta Follow the guide here on how to create a new organization.

  3. Create an Azure DevOps project, a repository and the yaml template file hello-beta.yaml in organization-beta.

  4. Create a personal access token (PAT) in the organization where you created the hello-beta.yaml template (organization-beta). Please see the guide here. This PAT can be used when setting up the service connection.

  5. Create a new service connection of type Azure Repos/Team Foundation Server on the organization you will run your pipeline from (organization-alpha). This will define and secure connection to the other organization. Please follow this guide here on how to create this service connection. Use the PAT token you created above.

  6. Create your pipeline in Azure Pipelines using existing the azure-pipelines.yaml file. For more instructions on how to create a pipeline, please see this guide.

  7. Open the azure-pipelines.yaml file, and change variables section as needed along with the resource configuration according to the step below. After the configuration updates, commit your changes.

  8. Run your pipeline. The pipeline output of a successful run will look like below:

    pipeline

Referencing pipeline steps

For templates in the same repository, the yaml file containing the pipeline steps is referenced via the template step(s):

steps:
- template: templates/hello-alpha.yaml

Configuring access to other organizations

For files in repositories that are in another organization, a service connection of type Azure Repos/Team Foundation Server is required to access the organization. For a guide on how to set this up, follow this document

The pipeline references the service connection through a resource where the endpoint points to a service connection configured in this organization. The repository keyword lets you specify an external repository.

resources:
  repositories: 
    - repository: remoteRepo # Simple name to reference this repository at a later step
      type: git
      name: project-name/repo-name
      endpoint: remote-git-connection # Azure DevOps service connection
      ref: refs/heads/main 
      
# Note: When referencing a repository in another organization, branches may need to be set explicitly using `ref` in the repository definition. 

# It may be necessary to do even when a file is located in the other organization's default branch.

The template then references this repository resource by adding the repository reference to the file reference:

# The template hello-beta.yaml is stored in a repository and is being called from a pipeline that exists in another repository. 

# The syntax of using @remoteRepo allows the calling pipeline to get the remoteRepo reference that was defined above and resolve it.

- template: templates/hello-beta.yaml@remoteRepo

To see the full contents of the templates/hello-beta.yaml, please see the code sample section

Code Sample

See below the template templates/hello-beta.yaml file that installs and runs azurite and is located in another organization under project-name/repo-name:

steps:  
- bash: |
    echo "Hello from a different Azure DevOps Organization!"
  displayName: 'Hello from Beta'

References