Skip to content

Instantly share code, notes, and snippets.

@botanicus
Created February 14, 2011 14:41
Show Gist options
  • Select an option

  • Save botanicus/825958 to your computer and use it in GitHub Desktop.

Select an option

Save botanicus/825958 to your computer and use it in GitHub Desktop.
Puller: it updates repository on the server after push to GitHub. Useful for example for CI via CIJoe. Just add your_server/puller to Post Receive URLS (in GH administration for the repo).
.rvmrc
.bundle
Gemfile.lock
*.log

About

Puller is a simple hook which fetch changes from the GitHub repository after the repository was updated.

Setup

  1. Simply start the app on your server.
  2. And add you server URL to the Post-Receive URLs on GitHub in administration of the project you wish to mirror on your server.
  3. There’s no step 3!

Links

#!/usr/bin/env rackup
# encoding: utf-8
# TODO: the logger doesn't work!
require "json"
class Puller
def initialize
@log = File.open(File.expand_path("../puller.log", __FILE__), "a")
#@log = STDERR
info("Puller initialised.")
at_exit { @log.close }
end
def call(env)
request = Rack::Request.new(env)
if request.post?
data = JSON.parse(request.params["payload"])
if File.directory?("/repos/#{data["repository"]["name"]}")
info("Updating #{data["repository"]} ... ")
Dir.chdir("/repos/#{data["repository"]["name"]}") do
sh "git fetch && git reset origin/master --hard"
end
else
info("Cloning #{data["repository"]} ... ")
Dir.chdir("/repos") do
#sh "git clone #{data["repository"]["url"]}.git --recursive" # this needs newer Git than I have in my Debian
sh "git clone #{data["repository"]["url"]}.git"
Dir.chdir(data["repository"]["name"]) do
sh "git submodule update --init; true"
end
end
end
return [204, Hash.new, Array.new]
elsif request.get?
body = <<-EOF
This is <a href="https://gist.github.com/825958">Puller</a>.
EOF
return [200, {"Content-Length" => body.bytesize.to_s, "Content-Type" => "text/html"}, [body]]
end
rescue Exception => exception
error("#{exception.class}: #{exception.message}\n- #{exception.backtrace.join("\n- ")}")
end
private
def info(message)
@log.puts("[INFO] #{message}")
end
def error(message)
@log.puts("[ERROR] #{message}")
end
def sh(command)
@log.puts(command)
@log.puts(%x(#{command}))
@log.puts("Exit status: #$?")
end
end
map "/puller" do
run Puller.new
end
# encoding: utf-8
source "http://gemcutter.org"
gem "rack"
#!/bin/sh
ROOT=/webs/services/puller
PIDFILE=/var/run/puller.pid
start () {
$ROOT/config.ru --port=1000 --env=production --daemonize --pid $PIDFILE
}
stop () {
kill -9 $(cat $PIDFILE)
}
case "$1" in
start|"")
start ;;
restart)
stop && start ;;
stop)
stop ;;
*)
echo "Usage: $0 [start|restart|stop]" >&2
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment