Skip to content

Instantly share code, notes, and snippets.

@jeremymarc
Forked from tedsparc/github-autodeploy.rb
Created September 19, 2012 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeremymarc/3750296 to your computer and use it in GitHub Desktop.
Save jeremymarc/3750296 to your computer and use it in GitHub Desktop.
Github post-receive hook for auto-deployment of a Web site document root
require "rubygems"
require "sinatra"
require "json"
# Configure this with the directory path for the Web server's clone of the Git repo
git_dir = '/var/www/origin.git'
# Configure the mappings between Git branches and Web document roots
branch_to_working_directory = {
'www' => '/var/www/www.example.com',
'staging' => '/var/www/staging.example.com/',
'dev' => '/var/www/dev.example.com/'
}
post '/' do
push = JSON.parse(params[:payload])
ref = push['ref'] || raise("ref required in payload")
branch = ref.match(/([^\/]+)$/)[0]
work_dir = branch_to_working_directory[branch]
warn "Got Github hook for ref #{ref}, branch #{branch}, work_dir #{work_dir}"
system "git --git-dir=#{git_dir} --work-tree=#{work_dir} add ."
system "git --git-dir=#{git_dir} --work-tree=#{work_dir} fetch"
system "git --git-dir=#{git_dir} --work-tree=#{work_dir} reset --hard -q origin/#{branch}"
''
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment