Trigger Integrity builds with a cron job

Updated 21 Dec 2009: modified code to work with integrity v. 0.2.9

Integrity is a great little continuous integration server, even if it is a little rough around the edges.

Out of the box Integrity expects that you will trigger all your builds using github’s post receive hooks - although you need to be aware of this bug which means you have to miss the .git of the end of the project’s git repository URI when setting this up.

I however prefer to poll my project’s repositories for changes so that they do not need to be aware of my Integrity server.

If you are using Integrity v0.2.9 or greater then this can be achieved by adding the following rake task to the Rakefile in your Integrity installation.

desc "Will build the latest commit for any project that has already been built \
  and the latest commit has not already been built"
task :build_new_commits do
  require "init"
  Integrity.log("Checking for new commits at #{Time.now}")
  Integrity::Project.all.each do |project|
    # Don't build if project is just being set up, or a build of 'HEAD' is
    # already outstanding or the latest commit has already been built.
    unless project.blank? ||
        project.last_build.commit.identifier == 'HEAD' ||
        (head = Integrity::Repository.new(project.uri, project.branch, 'HEAD').head) == project.last_build.commit.identifier
      project.build(head)
    end
  end
end

Then set up a cron job by adding the something like the following entry to your crontab. This will to automatically check every 2 minutes to see if there are any outstanding commits of your project to build. You will need to replace the /path/to/ section in each of the paths with appropriate values for your system.

*/2 * * * * cd /path/to/integrity && \
/path/to/ruby ./bin/rake build_new_commits >> /path/to/cron.log 2>$

Alternatively, if you are using an earlier version of integrity

Or, if you want to add build triggers to git repositories not on github then take a look at morethanseven’s post commit hook.