Created
May 9, 2012 19:39
-
-
Save roberocity/2648267 to your computer and use it in GitHub Desktop.
Git-based deployment for Sinatra
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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