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”.

  • Visual Studio sometimes detects configuration issue on jenkins CI server
  • PHP build for Jenkins failing with 'Cannot run program “phploc”'
  • Google App Engine Jenkins Integration
  • Repositories URL in Jenkins + Git
  • Bamboo build stuck on “Waiting to be built…” and Cloning into 'Common'
  • Jenkins - One job for all branches or One job per branch?
  • 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.

  • Git Pull vs Git fetch Which one is Preferable?
  • Making files in submodule read-only
  • Best way to use npm and git?
  • How to ignore files and folders with pull requests to github in distinct git clones installations?
  • Using git to manage combinations of branches in same directory
  • git version control files on repositories without pushing/pulling
  • 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.