Skip to content

Instantly share code, notes, and snippets.

@kkoch986
Created February 21, 2014 04:04
Show Gist options
  • Save kkoch986/9128522 to your computer and use it in GitHub Desktop.
Save kkoch986/9128522 to your computer and use it in GitHub Desktop.
Git post-receive hook for automatically deploying a new version of my sites
#!/bin/bash
# CHANGE THIS TO MATCH YOUR PROJECT
root_dir=/var/www/example.com;
while read oldrev newrev refname
do
branch=$(git rev-parse --symbolic --abbrev-ref $refname);
branch_dir=$root_dir/$branch/$newrev;
echo "[CI] Commit recieved on branch $branch";
echo "[CI] CREATE DIR " $branch_dir && mkdir $branch_dir;
git --work-tree=$branch_dir checkout -f $branch;
# now move the checked out code into a versioned folder
version=$(node /var/git/version.js $branch_dir/package.json);
version_dir=$root_dir/$branch/$version
echo "[CI] REMOVE (if exists) " $version_dir && rm -rf $version_dir
echo "[CI] Move " $branch_dir " to " $version_dir && mv $branch_dir $version_dir;
# now run the grunt script there
echo "[CI] npm install";
cd $version_dir && npm install;
echo "[CI] grunt";
grunt --base=$version_dir --gruntfile $version_dir/Gruntfile.js $branch;
# link in the new version
echo "[CI] Linking version $version as CURRENT";
unlink $root_dir/$branch/current;
ln -s $version_dir $root_dir/$branch/current;
# Stop the old and start the new
echo "[CI] Stopping old version.";
forever stop $root_dir/$branch/current/app.js;
echo "[CI] Starting new version.";
forever start $root_dir/$branch/current/app.js;
echo "[CI] Success";
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment