Using git commands in a TeamCity Build Step

25,453

Solution 1

Try changing the VCS checkout mode to "Always checkout files on agent" as documented here. That should give the build script access to the .git folder.

Solution 2

@Mike Two's answer is correct, one needs to configure the VCS root to use Checkout mode Automatically on Agent -not- on the server. The TC docs do not mention that this required setting is hidden by default as 'advanced'. So here is a picture of what to look for.

Advanced Settings

FYI, My specific VS2013 Solution step error was from MSBuild, as below. I am using the latest TeamCity Enterprise 9.0 EAP (build 31963). [PreBuildEvent] Exec [14:21:55][Exec] git rev-parse --short=9 HEAD > "C:\TeamCity\buildAgent\work\e5e910561e800525\MYPROJECT\bin\Debug\version.txt" [14:21:55][Exec] fatal: Not a git repository (or any of the parent directories): .git [14:21:55][Exec] C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets(1131, 5): error MSB3073: The command "git rev-parse --short=9 HEAD > "C:\TeamCity\buildAgent\work\e5e910561e800525\Higgens\bin\Debug\version.txt"" exited with code 128. [14:21:55]

//edit; I didn't know how to add this to a comment.

Solution 3

Setting teamcity.git.use.shallow.clone=false and teamcity.git.fetchAllHeads=true may be what's missing, at least on the latest version of TC.

Share:
25,453

Related videos on Youtube

Jake Stevenson
Author by

Jake Stevenson

Updated on May 08, 2021

Comments

  • Jake Stevenson
    Jake Stevenson almost 3 years

    One of my TeamCity v7.0 build projects relies on using a diff between the current development branch and the master branch to determine what needs to be run. I've written a small script that uses something like:

    git diff origin/master..origin/QA --name-only --diff-filter=AM | DoSomethingWithThoseFiles
    

    Unfortunately, the build log indicates that git diff is not working. When I go into the buildAgent/work directory for this project, I see that there is no .git folder, so it cannot perform the git operations required.

    I've put teamcity.git.use.local.mirrors=true in the buildAgent.properties file as mentioned at TW-15873, but that does not seem to help in any way.

    Is there anything I can do to get the list of files changed between two branches in a script for my build step?

    • user24601
      user24601 over 8 years
      Has there been a solution for this? I'm also having this difficulty using TeamCity 8.1. I am using VCS checkout mode "Automatically on Agent" but that does not seem to help. The .git directory is still missing.
  • Jake Stevenson
    Jake Stevenson over 11 years
    That got me part of the way, but doing the diff between origin/Master and the current branch does not work. Instead, it seems like it only recognizes a SINGLE branch local or remote! I think TC is doing something funky-- cloning a clone or something unusual. "git fetch" throws an error that buildAgent/system/git/git-14CE2659.git does not appear to be a git repository, even though the current repo is in buildAgent/work/blah.
  • xofz
    xofz almost 11 years
    Thank you! This answered a question I had: stackoverflow.com/questions/17555931/…
  • Klas Mellbourn
    Klas Mellbourn over 8 years
    Unfortunately, it seems that if you need ssh authenticated access, e.g. in order to push changes to the remote git repo, it doesn't work since TeamCity erases the ssh keys after fetching. confluence.jetbrains.com/display/TCD9/SSH+Keys+Management
  • Fabian Pas
    Fabian Pas over 7 years
    I also had to set up my Git executable path explicitly for this to work.