Adding a git post-receive hook to fire off Hudson CI server

UPDATE: guard clause throws LocalJumpError. Changed to simple if block
Context: I needed our hudson CI builds to auto-fire when a developer pushes their code changes to our canonical/upstream git repositories.
In your .git/hooks/post-receive file place the file code (FULL CODE):

#!/usr/bin/env ruby
#   
while (input = STDIN.read) != ''
   rev_old, rev_new, ref = input.split(" ")
   if ref == "refs/heads/master"

       url="http://yourhudsondomain.com/job/job_name_here/build?delay=0sec"

       puts "Run Hudson build for job_name_here application"
       `wget #{url} > /dev/null 2>&1`
   end
end

EXPLAINING EACH LINE:
I avoid writing bash scripts in default bash if I can. This is for a Ruby on Rails app, so I decided to write it in Ruby.

#!/usr/bin/env ruby

The while loop checks to make sure there are values in STDIN (by default they are rev_old, rev_new, and ref).

#   
while (input = STDIN.read) != ''
end

I put an if statement block:

if ref == "refs/heads/master"
end

to make sure I only fire the build if it is the master branch.
Set the URL for the app you want to build:

url="http://yourhudsondomain.com/job/job_name_here/build?delay=0sec"

Add a little text to notify the user what is going on (always a good idea):

puts "Run Hudson build for job_name_here application"

And finally all the url with wget and feeding any response to the black hole that is /dev/null

:

`wget #{url} > /dev/null 2>&1`

IMPORTANT:
Don’t forget to run:

chmod a+x .git/hooks/post-receive

so that the file can be run
Some Gotchas:
1. If you are going to a url that has http basic authentication like my actual script does, you can pass the username and password like you normally would in an HTTP call:

url="http://username:password@yourhudsondomain.com/job/job_name_here/build?delay=0sec"

2. If you are doing an https call with a self-signed certificate, wget will complain. You can add the –no-check-certificate argument to wget to bypass certificate validation against local certificate caches:

`wget --no-check-certificate #{url} > /dev/null 2>&1`

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Jason Meridth

Continuously learning software developer trying to not let best be the enemy of better
This entry was posted in continous integration, git, hudson. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Comments are closed.