Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 816
Rob Avatar asked May 09 '16 18:05

Rob


People also ask

Can GitHub be used for CI CD?

With GitHub Actions, you can trigger CI/CD workflows and pipelines of webhooks from these apps (even something simple, like a chat app message, if you've integrated your chat app into your GitHub repository, of course).

What is continuous integration Git?

Continuous integration (CI) is the practice of automating the integration of code changes from multiple contributors into a single software project. It's a primary DevOps best practice, allowing developers to frequently merge code changes into a central repository where builds and tests then run.

Is git a CI tool?

Version Control System support The core pillar of a CI / CD system is the support and integration of the underlying Version Control System (VCS). The most popular VCS's are Git, Subversion, Mercurial and Perforce.

How do I link GitHub to CircleCI?

Go to your project settings in the CircleCI app, select SSH Keys, and Add SSH key. In the "Hostname" field, enter `github.com`and add the private key you created in step 1. Then click Add SSH Key.


2 Answers

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.

like image 59
Rob Avatar answered Sep 29 '22 17:09

Rob


Assuming you set up your .gitignore correctly, you should be able to just do

git fetch
git reset --hard origin/branch
git clean -xfd

That should produce a clean build.

like image 20
yelsayed Avatar answered Sep 29 '22 17:09

yelsayed