Skip to content

Instantly share code, notes, and snippets.

@amiel

amiel/README.md

Created Oct 28, 2011
Embed
What would you like to do?
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
You can’t perform that action at this time.