Connection error with private repository using GitVersion as a build step in TeamCity

1.5k views Asked by At

I'm configuring CI environment for the project I'm working on and I got stuck. Before I will describe the problem here is tools & tech stack I use:

  • Teamcity 8.1.5 as build server,
  • Git as a source version control,
  • GitFlow as a Git workflow,
  • TFS 2013 as a collaboration project (with Git repository bound to it),
  • GitVersion as a tool for labeling assemblies and releases.

The TFS server supports both: NTLM and Basic Authentication.

The scenario:

I want to use GitVersion tool for auto labeling the sources on each build. That's why as a first build step I added command line step with command:

gitversion.exe "%system.teamcity.build.checkoutDir%" /output "buildserver" 
/u "Domain\Username" /p "Pass"

Repository url is automatically taken by GitVersion from [checkout-dir]\.git\config.

Unfortunately when I run the build I receive an exception:

LibGit2Sharp.LibGit2SharpException: Request failed with status code: 401

Here is the output from Build Log:

[Step 2/5] Starting: C:\ProgramData\chocolatey\bin\gitversion.exe /output buildserver /u Domain\UserName /p password 
[Step 2/5] in directory: C:\BuildAgent\work\Playground 
[Step 2/5] Working directory: C:\BuildAgent\work\Playground [Step 2/5] Applicable build agent found: 'TeamCity'. 
[Step 2/5] One remote found (origin -> 'http://our.tfs.server.lgbs:8080/tfs/project/_git/Playground'). 
[Step 2/5] Fetching from remote 'origin' using the following refspecs:
    +refs/heads/*:refs/remotes/origin/*. 
[Step 2/5] An unexpected error occurred: 
[Step 2/5] LibGit2Sharp.LibGit2SharpException: Request failed with status code: 401 
[Step 2/5]    at LibGit2Sharp.Core.Ensure.HandleError(Int32 result) 
[Step 2/5]    at LibGit2Sharp.Core.Proxy.git_remote_fetch(RemoteSafeHandle remote, Signature signature, String logMessage) 
[Step 2/5]    at LibGit2Sharp.Network.DoFetch(RemoteSafeHandle remoteHandle, FetchOptions options, Signature signature, String logMessage) 
[Step 2/5]    at LibGit2Sharp.Network.Fetch(Remote remote, FetchOptions options, Signature signature, String logMessage) 
[Step 2/5]    at GitVersion.GitHelper.NormalizeGitDirectory(String gitDirectory, Authentication authentication)

And I cannot figure out what is wrong with it.

When I run the same command directly from CMD on Build Agent machine it executes perfectly fine.

What I already tried:

  • verified that the build step command runs under the same identity as the command I tried manually in CMD,
  • added repository credentials to windows credentials store,
  • passed url directly to the GitVersion using /url switch
  • passed username as a part of url: http://Domain\UserName@tfs-server-url\projectname but it failed with different error: Malformed URL "http://Domain\UserName@tfs-server-url\projectname"
  • called git fetch as a build step (to check if the credentials are ok) with success,
  • used Google but I didn't find similar problem.

Has anyone encountered similar problem? What would you suggest to do? It seems that there is no alternative to GitVersion. I'm afraid that the only workaround would be to care about the labeling by hand which is not convenient, especially with GitFlow.

1

There are 1 answers

0
Richard J Foster On

Since I just stumbled onto this question while helping a coworker who was suffering from the same problem:

If you get this unhelpful error when running under TeamCity it is because GitVersion doesn't know which branch it is being asked to build.

Make sure you have set an env.Git_Branch (spelling is important!) environment variable. The variable will typically be initialized to %teamcity.build.vcs.branch.[Repository_Id]%.

For more information, see the documentation at https://gitversion.net/docs/reference/build-servers/teamcity