Download source locally from Azure Devops GIT repository

We have recently moved our source code from a local TFS repository to an Azure Devops Git repository.

For now we are going to continue building locally, so I need a script to download the latest version of a branch locally, before building.

I have tried a few different methods, but am getting confused about the best one to go with:

Git Bash

- I have installed Git Bash, and have managed to clone a branch successfully via the interface.

- I have looked at scripting Git requests in Powershell, but am struggling with scripting the authentication prior to Git requests.

- I'm also a bit unsure of the steps required to 'Get Latest' in Powershell prior to each build. I'm thinking the steps should be something like:

  1. Authenticate into repository.
  2. Change directory to the directory you want to download to (Get Latest into).
  3. Execute the Git clone command for a branch if it has not been cloned yet (how do you check this?)
  4. Execute the Git pull command for a branch if it has been cloned.

Azure Devops REST APIs

I have also investigated Azure Devops REST API Git repository requests. I have managed to execute a repositories request.

Is it possible to download a branch with the REST apis, or are they just for requesting information about the repositories?

Thanks.

Posted Oct 18, 2019, 4:27 AM UTC

37
lerun•

Why not just install the AzD agent on a local VM and use the pipeline task preconfigured to dl files from the AzD git repo you configure the pipeline to work with?

Also if you want to talk to AzD API using PS, have a look at this:
https://github.com/DarqueWarrior/vsteam

3
linkdudesmash•

Agreed with above. Use the tools from the box.

1
wyrdfish42•

AzDo build piplines download the code automatically.

If you just want to get a copy on your dev box click the clone icon in the top right and follow the instructions.

If you have your own build scripts, install git for windows and do a git clone as part of your script. ir do git pull if you are building incrementally.

honestly its so easy to use pipelines you should really do that instead of writing things yourself.

2
andymsli•

What's that mean for "going to continue building locally"? Do you mean that you want to build with local TFS in pipeline? or just get the source and build with local IDE (e.g build with local Visual Studio)?

If you want to get sources from Azure Devops Git repository in TFS build pipeline, then you can try to install Git Repository Downloader extension, then you can use that task to download the sources from the specific repository.

If you want to git clone to local workspace, then you can use any Git Client to do that. Of course you can also write a PowerShell script to do that, below sample for your reference:

$ErrorActionPreference = 'SilentlyContinue'
$gitremoteurl = "https://dev.azure.com/{organizatio}/{project}/_git/hello-world"
$PAT= "PAT here"

# Clone or pull repository
cd D:\temp  # cd to the specific directory which you want to clone the repo.

$initpath = get-location
$reponame = "test1019"

$url = $gitremoteurl -replace "://", ("://{0}@" -f $PAT)

#Check the repository, if not exists then git clone, if already there, then git pull 
if(!(Test-Path -Path $reponame)) {
        git clone $url $reponame
    } else {
        set-location $reponame
        git pull
        set-location $initpath
}

With the REST API we can also get the items: Items - Get, for example to download the specific repo branch with a zip:

Get https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repo id}/Items?versionDescriptor.Bversion=master&resolveLfs=true&format=zip&api-version=5.0-preview.1
1
rawpineappleOP•

Thanks for your help. This is what I was after.

1
totkeks•

Whatever your local build machine runs as OS, set your git credentials in the build pipeline and then just clone.

No need to pull or use branches. Because in a build pipeline you start from scratch each time anyway.

But maybe there is a misunderstanding of your requirements.

1
rawpineappleOP•

Thank-you all for your help.

1