How to get checkout repository path – Azure-devops

by
Ali Hasan
azure-devops azure-pipelines azure-pipelines-yaml

Quick Fix: You can use the pre-defined variable Build.SourcesDirectory. This variable points to the local path on the agent where your source code files are downloaded.

The Problem:

A user has an Azure DevOps pipeline template that checks out multiple repositories. They want to run a script that will cd into the self repository. However, they need a way to get the path that the checkout repository has been downloaded to.

The Solutions:

Solution 1: Using Predefined Variables

To obtain the path of the checked-out repository, you can utilize the predefined variable Build.SourcesDirectory. This variable represents the local path where the source code files are downloaded.

For a single Git repository checkout, this path will be the exact path to the code. However, if multiple repositories are checked out, the variable will revert to its default value, which is $(Pipeline.Workspace)/s.

By using the Build.SourcesDirectory variable, you can access the path of the checked-out repository and perform desired operations, such as cd into the self repository.

Solution 2: Using Build Variables

Azure DevOps pipelines provide various build variables that can be used to access information about the current pipeline execution. To get the path of the checked-out repository, you can use the following build variables:

  • Build.SourcesDirectory: The path to the directory where all checked-out repositories are stored.
  • Build.Repository.Name: The name of the current repository being checked out.

By combining these variables, you can construct the full path to the checked-out repository as follows:

$(Build.SourcesDirectory)/$(Build.Repository.Name)

For example, if the checked-out repository is named "my-repo" and it is stored in the directory "sources", the full path would be:

$(Build.SourcesDirectory)\my-repo

Q&A

How to get checkout repository path?

There are [pre-defined variables][1], you can see there the variable Build.SourcesDirectory

How to solve checkout repository path with multiple repositories?

Because of the different behavior of Build.Repository.LocalPath, the following path can be used instead: \((Build.SourcesDirectory)/\)(Build.Repository.Name)

Video Explanation:

The following video, titled "Checkout multiple repositories - Azure DevOps - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Checkout multiple repositories on your azure devops pipeline #azure #devops #repository #checkout #github #azure #repos.