Rob Rob - 2 months ago 5
Git Question

How to use git on a continuous integration build server

We have a build server which is designed to check-out a version of the code from git and build it. Generally the server will check-out and build the

develop
branch, but it can be controlled by a GUI to perform a build of any specific branch or tag.

Our git archive is large, so we only want to perform a
git clone
once. So my question is: what sequence of git commands should be issued in order to bring the current working directory up-to-date with respect to the remote git archive.

My initial naive attempt just performed
git checkout <branch>
followed by
git pull
. But this did not take into consideration all the artifacts created by the prior build which needed to be deleted as well as some automatic code modifications made by the build process e.g. to the version numbers in assembly files.

So what I think we need is the command sequence to


  1. Get rid of any modifications to the local directory

  2. Update the local repository to include ALL commits from the remote server

  3. Checkout the named branch or tag



Please bear in mind that the named branch or tag may not already be known in the local repository. For example if a new
release/xxx
branch is created on the remote server, this will not be known a priori to the local build machine. This is another one of the issues my naïve approach stumbled on.

And finally, it's possible that the git server may occasionally have it's history corrected. I'm sure this will be a rare event, but it would be desirable if the integration server didn't need any adjustment following a history rewrite.

Many thanks

Rob Rob
Answer

This is the mechanism we are currently using. The git clone is performed just once as part of the setup, and then the following is performed for every build.

I have removed error handling for clarity.


# Undo any modifications made to the working tree by the build process

git reset --hard

# Remove any untracked build artifacts created by the build process

git clean -fdx

# Fetch all commits and new branches/tags from the git server.

git fetch

# Set the index and working directory to the required tag or branch.
# Notes:
# * If the last build was for the same branch, then this will not
#   update the working directory.
# * The %TAG_OR_BRANCH% variable is passed in via the GUI.  This will
#   be in the form of a branch name (develop|release/vX.X.X) or a tag (vX.X.X)

git checkout %TAG_OR_BRANCH%

# Bring the index and working tree up-to-date.
# Notes:
# * the --ff-only flag is probably redundant, but we want to make it
#   clear that no merging is expected.

git pull --ff-only

We have created new tags/branches on a developer machine, pushed these to the server, and this script correctly fetches them and builds the results. We experimented with adding the origin/ prefix to the branch or tag names, but this approach did not work for us (the tag names were not recognised).

I think the main difference between this script and the much simpler answer suggested by @Yasser is that the git HEAD is pointing at the correct place and the command git status gives a sensible answer. Whether or not this is important - I'm not sure.

Comments