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

  • Changing branches in a way that ensures a clean/correct working tree and submodules (Git)
  • Downstream Jenkins project gets wrong upstream run parameter
  • Connect Github to secured Jenkins via HTTP Post-Commit Hook
  • How to change home directory of Jenkins?
  • Passing data between build steps in Jenkins
  • C++ Jenkins QA Stack / Tools
  • 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.

  • Subdirectory ignored in .gitignore whitelist
  • Git init initializes on different directory instead of the current
  • How do I partially git cherry-pick without ruining a future merge?
  • check out a “pull request” on Git to review/test a PR
  • How to clone a repo in Windows which contains an invalid symbol for windows file system
  • How can I display dates when local updates were made to a git repo?
  • 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.