Search code examples
gitpowershellazure-devopsalm

git fetch tags on Visual studio online build agent


I'm using Visual Studio Online with Git and have set up continuous build with a build agent running on a VM that I control.

I added a PowerShell script step to build msi file on versioned commits, meaning they have a tag. The problem is that tags is not automatically fetched when the repo is synced in the first step (Visual studio).

I thought I could fix this by just adding "git fetch --tags" to the beginning of the PowerShell script, but this prints this error:

[error]bash: /dev/tty: No such device or address
[error]error: failed to execute prompt script (exit code 1)
[error]fatal: could not read Username for 'https://myaccount.visualstudio.com': Invalid argument

When I set up the build agent it created an access token, but that is obviously not used and it prompts for username/password.

Anybody know how to solve this?


Solution

  • There is Git Build Tools, an extension for VSTS wich provides two new build tasks to rewrite the remote URL with the OAuth token provided by VSTS (you'll need to enable this on the build definition). With this approach you don't need a specific service user account and credential manager on the build agent.

    • Install the Git Build Tools extension in your VSTS extension.
    • First you need to make sure that you have the Allow Scripts to Access OAuth Token option in the build definition options set.
    • Then you can add the Enable Git Remote Access task to rewrite the remote of the Git repository on the agent to allow access to the upstream repository on Visual Studio Team Services.
    • After running the operations requiring access to the remote repository make sure the remote is restored to its default value using the Restore Git Remote task.

    You can find full source code here and further documentation in the wiki.