fredag 1 november 2013

Allow developers to always checkout code that compiles and passes tests with Git(hub) and Jenkins

Introduction

A new branch can be added into GitHub called latest.
Only an automated user (used by jenkins) is pushing into this branch(from Jenkins). This means that, at least,
Jenkins(and the build scripts/tasks) is verifying for any compilation errors, and other potential kpi's (like passing unit tests, test coverage etc).

Without meeting the specified requirements, code will not be pushed to Github's latest branch.

The interesting part for the developers is that makes pulling from the latest branch much safer than from the master branch.

Development machine

The advised workflow as a developer is to:
  • Push to master
  • Pull from latest

Push to master

Nothing new here, just plain old  
git push

Pull from latest

Git needs to be configured to pulling from the latest branch instead of the default one (master). IEither edit the .git/config file by hand, or run the following commands in the root of the local git repository:
git config branch.master.remote origin
git config branch.master.merge refs/heads/latest

Then do
git pull

Git repo

Create *-latest branch associated with a branch that is updated via Jenkins and permits developers to pull into their own local repos the latest (working) changes.
  1. Get on the branch that you want to create the continuous integration on (using foo as example branch):
    git checkout foo
  2. Get the latest commits from that branch:
    git pull origin foo
  3. Create the associated *-latest branch based on the current branch:
    git checkout -b Release1-latest
  4. Push to the main repo:
    git push origin Release1-latest

Build Server (Jenkins)

Configure jenkins to pull from the master branch - e.g:
And then configure the git publisher plugin to push to latest:

Wrapping up

Whenever you do a git pull, you're actually pulling from latest instead of the default master branch. If you push code to GitHub and the Jenkins build fails, i.e. can't compile or unit test fails or somtehing else, other developers can't submit until the failing push is fixed. This is due to commit version conficts since latest is used as the base and master has a later commit version. Latest and master will have the same commit version after a successful Jenkins build.

References

This page has more details on the process of changing the branch from where to pull from: http://stackoverflow.com/questions/658885/how-do-you-get-git-to-always-pull-from-a-specific-branch

Thanks to

Peter Jansson & Raul Brito for teaching me this, and to Ulrik Sandberg for asking annoyingly necessary questions for me to have to document it.