If you have a large repository it can be a long wait on the pipeline to get it checked out, only to use a few files. In this post, I will guide you through using PowerShell to download only the files you need with either the Azure DevOps REST API or CLI.
In both instances we will be using the REST API endpoint Get Items
This endpoint can return items from a Git repository within Azure DevOps, including its metadata, files content and/or the files themselves. There are multiple query parameters that affect the response of the API request. Below are the query parameters we will be using to download the JSON for either the files or folders metadata.
This is either the folder path or files path including the file name. The path should be relative to where it is within the repository not the file system.
This is the name of the branch where to source the code from. If you use a dynamic method like the pre-defined variable
System.SourceBranch then you will need to remove the
refs/heads/ to leave just the branch name.
You also cannot use the dynamic Pull Request branch path for example
This parameter tells the request how far down to scan and return back. We are going to use the value
full, which for a folder it will keep recursively scanning down until there are no more folders to go through and for a file it will just stop at that item.
The format query parameter details what format to return the requested content. Without this parameter when you request a folder it automatically returns it as JSON, but when requesting a file it tries to return the binary of the file. Therefore, adding this as default doesn’t affect the folder request and can force the file request to return it as JSON. The value for this parameter is