TeamCity pass parameter from server to build agent

I want to get commit count on build agent for manual versioning,

version = git rev-list --count "branchname"

git is not available on build agent because I have checkout “Automatically on server”.

  • What exactly do button “Abandon Change” in gerrit?
  • Testing/previewing Github branches on a dev server
  • TeamCity triggers too many builds for a new branch
  • Get hudson to checkout source to a specific directory
  • Handling multiple branches in continuous integration
  • How to set up new Jenkins slave
  • Is there a way to pass version from checkout server to build agent? (without changing VCS checkout mode to build agent)?

    I’m running latest 9.1.6 version TC.

  • (yet another) modify git history to add a very first commit
  • Git flow with Bitbucket pull requests
  • Create multi-word alias in bash?
  • How to git stash pop specific stash in 1.8.3?
  • Multiple features for one branch - what's the point?
  • Files missing after *git svn clone*
  • 4 Solutions collect form web for “TeamCity pass parameter from server to build agent”

    Is there a way to pass version from checkout server to build agent? (without changing VCS checkout mode to build agent)?

    The short answer is that You cant do it.

    What you can try and do is this:

    - Add a version file to your repository,   
    - **before** commiting use a git hook to update this file with the desired number
    - Read the content of the file on your build server and you have it.
    - Use a git hook to call a job on your build server which gets the 
      branch name and the number of commits and store it for later use somewhere

    The main point is that since you cant do it you need to be little bit creative

    Sample hook can be:

    pre-receive hook

    # Get the number of commits you need to store:
    version = git rev-list --count $branchName
    # Now write the desired number to the desired file and let
    # the build read it
    # Output colors
    # personal touch :-)
    echo "${red}"
    echo "                                         "
    echo "                   |ZZzzz                "
    echo "                   |                     "
    echo "                   |                     "
    echo "      |ZZzzz      /^\            |ZZzzz  "
    echo "      |          |~~~|           |       "
    echo "      |        |-     -|        / \      "
    echo "     /^\       |[]+    |       |^^^|     "
    echo "  |^^^^^^^|    |    +[]|       |   |     "
    echo "  |    +[]|/\/\/\/\^/\/\/\/\/|^^^^^^^|   "
    echo "  |+[]+   |~~~~~~~~~~~~~~~~~~|    +[]|   "
    echo "  |       |  []   /^\   []   |+[]+   |   "
    echo "  |   +[]+|  []  || ||  []   |   +[]+|   "
    echo "  |[]+    |      || ||       |[]+    |   "
    echo "  |_______|------------------|_______|   "
    echo "                                         "
    echo "${default}"
    # set the exit code to 0 so the push will occur
    exit 0;

    Basically, no, you cannot do what you want in a way you want it, you cannot just execute some command-line commands on the server when getting changes.

    And why not just properly configure build number format and build counter and use them? It is also possible to set build number dynamically during a build.

    I do very similar things to harvest the current branch name and git hash during the build. Sadly, no, you can’t execute these git commands during the build with checkout mode set to server. You need to change the checkout mode to build agent to ensure the .git folder exists in the work directory. On the up-side, I see no harm in this. It doesn’t copy the remotes, so it’d be very difficult for the build agent to push changes to the main repository.

    To @hexct’s point, a rebase or merge or any number of things could make this count unreliable. Better to tie yourself to git hash than # of commits.

    Why you need git counter at all?

    The simplest way is to follow the principles of TeamCity. Use TeamCity counter instead of git counter. Set build trigger after each commit. And set labeling back to git to see the build versions in git history.

    Git Baby is a git and github fan, let's start git clone.