Skip to content

Instantly share code, notes, and snippets.

@pplante
Forked from stas/post-receive.sh
Created October 22, 2012 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pplante/3929632 to your computer and use it in GitHub Desktop.
Save pplante/3929632 to your computer and use it in GitHub Desktop.
Heroku like deployment git hooks
#!/bin/bash
# (Re)spawn the application
function spawn () {
local ps_name=$1
local spawn_cmd=$2
local pids=`ps -C $ps_name -o pid=`
if [ ${#pids} -ne 0 ]
then
echo "Sending TERM to $pids..."
killall -s 15 $ps_name -w
fi
echo "Starting the app..."
cd ..
GIT_DIR='.git'
git reset --hard HEAD
( exec setsid $spawn_cmd > ~/foreman.log 2>&1 & ) &
}
# Load RVM
[[ -s "/usr/local/rvm/scripts/rvm" ]] && . "/usr/local/rvm/scripts/rvm"
spawn "foreman" "foreman start -e env"
#!/bin/bash
# <oldrev> <newrev> <refname>
# Deploy new git commits
bundle_cmd="bundle install --path ~/.bundle --without development --binstubs ~/bin"
function bundle () {
local rev="$1"
local gemfile_changed=0
# Find HEAD
if [ -f 'refs/heads/master' ]
then
head=`cat refs/heads/master`
fi
# Update gemfile
local gemfile_info=`git diff-tree --name-only $rev $head | grep Gemfile`
# If gemfile was changed or head doesn't exist
if [ ${#gemfile_info} -ne 0 ] || [ ${#head} -eq 0 ]
then
echo "Gemfile updated..."
local gemfile_info=`git ls-tree --full-name $rev Gemfile`
local gemfile_sha=${gemfile_info%Gemfile*}
local gemfile_sha=${gemfile_sha: -41}
git cat-file blob $gemfile_sha > ../Gemfile
gemfile_changed=1
fi
# Run bundler with the new gemfile
if [ $gemfile_changed -ne 0 ]
then
echo "Running bundler..."
cd ..
exec $bundle_cmd
cd .git
rm -f ../Gemfile
fi
}
# Load RVM
[[ -s "/usr/local/rvm/scripts/rvm" ]] && . "/usr/local/rvm/scripts/rvm"
while read oldrev newrev ref
do
if [ "${ref: -6}" == "master" ]
then
echo "Master branch changes. Starting the deployment..."
bundle $newrev $oldrev
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment