Skip to content

Instantly share code, notes, and snippets.

@roberocity
Created May 9, 2012 19:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roberocity/2648267 to your computer and use it in GitHub Desktop.
Save roberocity/2648267 to your computer and use it in GitHub Desktop.
Git-based deployment for Sinatra
require 'sinatra/base'
require 'time'
class GitHook < Sinatra::Base
def self.parse_git
# Parse hash and date from the git log command
sha1, date = `git log HEAD~1..HEAD --pretty=format:%h^%ci`.strip.split('^')
set :commit_hash, sha1
set :commit_date, Time.parse(date)
end
set (:autopull) { production? }
parse_git
# before do
# cache_control :public, :must_revalidate
# etag settings.commit_hash
# last_modified settings.commit_date
# end
post '/pull/and/restart/app' do
content_type :txt
msg = ""
settings.parse_git
root_path = File.expand_path(File.join(File.dirname(__FILE__), '..'))
# app.settings.reset!
# load app.settings.app_file
if settings.autopull?
msg << "checking for update and restarting.\n"
`git pull 2>&1`
`bundle install`
else
msg << "restarting only.\n"
end
web_pid = `cat #{root_path}/tmp/site.pid`.strip
`kill -s HUP #{web_pid}`
# mobile_pid = `cat #{root_path}/tmp/mobile.pid`.strip
# `kill -s HUP #{mobile_pid}`
msg << "done\n"
msg
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment