DevOps Zone is brought to you in partnership with:

A developer and occasional sysadmin based in Cambridge, UK. Normally found using Python or Ruby (or both) to do good, he's also obsessed with automation and development environments. In what some people call spare time he curates the devopsweekly.com newsletter, organises events for developers and systems administrators in London and hacks on open source projects like vagrant, cloudfountry and tools for django. Gareth is a DZone MVB and is not an employee of DZone and has posted 48 posts at DZone. You can read more from them at their website. View Full User Profile

Git Pre Recieve Hook For Integrity

08.18.2011
| 6119 views |
  • submit to reddit

I’m getting married rather soon so time has been somewhat short (in a good way) for just hacking on stuff, but I’ve finally found a little bit of time to play with something I’ve been mulling over for a while. Namely a continuous deployment workflow using the integrity continous integration server.

I’m hoping to have an incredibly simple but fully operation example available at some point – mainly to act as a good discussion point. For now here’s my current pre-receive hook.

#!/usr/bin/env ruby

require 'rubygems'
require 'grit'
require 'nokogiri'
require 'open-uri'

# This is a pre-receive git hook designed to be part of a continuous
# deployment workflow using the Integrity continuous integration server

INTEGRITY = 'http://localhost:9292'
PROJECT = 'site'

# 1. Check if the build is currently broken
# 2. If it's broken check if the commit messages starts with
# [BUILD] and if so trigger a build
# 3. If it's broken and all messages don't have the prefix
# then stop the push
# 4. If the build is fine then let everything through

doc = Nokogiri::HTML(open("#{INTEGRITY}/#{PROJECT}"))
doc.css('div#last_build h1').each do |heading|
  content = heading.content.split
  @status = content[-1]
end

if @status == "failed"
  puts "Build currently broken, to force use the [BUILD] commit message prefix"
  old_rev, new_rev, ref_name = STDIN.gets.split

  repo = Grit::Repo.new(".")

  commits = repo.commits_between(old_rev, new_rev)

  commits.reverse.each do |c|
    short, long = c.message.split(/\n+/, 2)
    if short.slice(0..7) != "[BUILD]
puts "Cancelling push"
exit 1
end
end
end

 

References
Published at DZone with permission of Gareth Rushgrove, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)