Skip to content

Instantly share code, notes, and snippets.

@amiel
Created October 28, 2011 19:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amiel/1323159 to your computer and use it in GitHub Desktop.
Save amiel/1323159 to your computer and use it in GitHub Desktop.
post-commit git hook for simple deploys

Put the ruby file in .git/hooks/post-commit

Create a deploy script as .deploy_*

Example:

# File: .deploy_production
scp -R html example.com:public_html

Then use "deploy *" in your commit message.

Example:

git commit -a -m'Finish styling. Deploy production'

Don't forget to make sure that both .git/hooks/post-commit and .deploy_* have execution permissions

#!/usr/bin/env ruby -W -rpathname
Root = Pathname.new(__FILE__).join('../../..').expand_path
class Deployer
attr_reader :environments
def initialize
@environments = {}
end
def process
scan_files
scan_commit_message
end
def deploy(enviroment)
Dir.chdir Root do
system "bash -c ./#{ environments[enviroment] }"
end
end
private
def scan_files
Dir.chdir Root do
Dir['.deploy_*'].each do |file|
env_name = file.match(/.deploy_([^\.]+)/)[1]
@environments[env_name] = file
end
end
end
def environments_regex
@environments.keys.join('|')
end
def scan_commit_message
message = `git log -1 HEAD --format=format:%B`
if environments.empty? && message.match(/deploy\s+(\w+)/i)
puts "It looks like you are trying to deploy, but you have no deploy scripts"
puts "Try creating .deploy_#{ $1 }"
end
if message.match(/deploy\s+(#{ environments_regex })/i)
deploy $1
end
end
end
if Dir.exist? Root.join('.git')
Deployer.new.process
else
puts "Expected to be in .git/hooks directory. Giving up."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment