Skip to content

Instantly share code, notes, and snippets.

@katsuma
Created February 26, 2012 04:38
Show Gist options
  • Save katsuma/1913037 to your computer and use it in GitHub Desktop.
Save katsuma/1913037 to your computer and use it in GitHub Desktop.
cgi script to handle `git push` hook and to deploy project
#!/home/katsumatv/.rvm/rubies/ruby-1.9.3-p125/bin/ruby
ENV['GEM_HOME'] = '/home/katsumatv/.rvm/gems/ruby-1.9.3-p125'
ENV['PATH'] = "#{ENV['PATH']}:/home/katsumatv/.rvm/gems/ruby-1.9.3-p125/bin:/home/katsumatv/bin"
require 'rubygems'
require 'sinatra'
require 'json'
get '/' do
status 403
end
get '/deploy' do
status 403
end
post '/deploy' do
user = 'katsumatv'
server = 'katsuma.tv'
workspace = "/home/katsumatv/var/cache"
allowed_apps = { 'ton.katsuma.tv' => 'www/ton' }
payload = params[:payload] ? JSON.parse(params[:payload]) : nil
status 403 && return if payload.nil?
app = payload["repository"]["name"]
status 403 && return unless allowed_apps.keys.include? app
target_dir = allowed_apps[app]
`cd #{workspace}/#{app} && git pull origin master && echo "#{Time.now.to_s}" >> #{workspace}/#{app}/public/revision`
`rsync -avz #{workspace}/#{app}/public/ #{user}@#{server}:#{target_dir}`
`cd #{workspace}/#{app} && git reset --hard`
"done"
end
set :run => false
Rack::Handler::CGI.run Sinatra::Application
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment