Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Created June 2, 2010 15:44
Show Gist options
  • Save ryanflorence/422545 to your computer and use it in GitHub Desktop.
Save ryanflorence/422545 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'fileutils'
require 'yaml'
puts "# Global Deploy Script start"
branch = ARGV[0]
repository = ARGV[1]
puts "Branch: #{branch}"
puts "Repository: #{repository}"
# read config file
config = YAML::load(File.open("/Users/rpflo/Desktop/githooks/remote/servers.yml"))
# get the remote path
unless remote_path = config[repository]['servers'][branch]
puts "No server matches this branch, deploying nothing"
puts "# Please check the servers.yml file if you disagree"
puts "# But don't get mad when I say 'I told you so'"
puts "# Deploy script end"
Process.exit
end
webroot = config[repository]['webroot']
puts "Remote path: #{remote_path}"
now = Time.now.strftime("%Y-%m-%d-%H%M%S")
tmp_dir_name = "/Users/rpflo/Desktop/githooks/remote/tmp-#{now}"
Dir.mkdir tmp_dir_name
# archive the webroot directory and scp it
archive_name = "archive-#{now}.tar.gz"
archive_path = "#{tmp_dir_name}/archive-#{now}.tar.gz"
archive_output = `git archive --prefix=#{webroot}/ #{branch}:#{webroot} | gzip >#{archive_path}`
archive_scp_ouput = `scp -r #{archive_path} #{remote_path}/archives/#{archive_name}`
puts "Archive deployed"
# extract the archive into the tmp directory
extract_output = `tar -xf #{archive_path} -C #{tmp_dir_name}`
# scp webroot to the server
webroot_scp_output = `scp -r #{tmp_dir_name}/#{webroot} #{remote_path}`
puts "Webroot deployed"
# Cleanup
FileUtils.rm_rf tmp_dir_name
puts "# Deploy script end"
#!/usr/bin/ruby
# Read the STDIN to detect which branch we pushed
puts "# Post-receive hook start"
while gets
# each line of input is in form of "<oldrev> <newrev> <refname>"
branch = $_.split('/').pop.chomp
puts "Branch: #{branch}"
end
# read the STDIN
#branch = ARGV
puts "Refs: #{branch}"
dir = `pwd`
repository = /([^\/]*?)\.git$/.match(dir)[1]
puts "Repository: #{repository}"
# run deploy script, should pass in the actual ref so we can reuse the dev script
deploy_output = `ruby /Users/rpflo/Desktop/githooks/remote/scripts/deploy #{branch} #{repository}`
puts deploy_output
myapp:
webroot: public
servers:
develop: rpflorence@raflorence.net:/home/rpflorence/public_html/dev.raflorence.net
stage: rpflorence@raflorence.net:/home/rpflorence/public_html/stage.raflorence.net
#develop: rpflo@10.0.1.5:/Users/rpflo/Desktop/githooks/www/dev.myapp.com
#stage: rpflo@10.0.1.5:/Users/rpfloDesktop/githooks/www/stage.myapp.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment