Skip to content

Instantly share code, notes, and snippets.

@ih2502mk
Last active August 29, 2015 14:15
Show Gist options
  • Save ih2502mk/e54fb39dec03c13db4df to your computer and use it in GitHub Desktop.
Save ih2502mk/e54fb39dec03c13db4df to your computer and use it in GitHub Desktop.
post-receive hook
#!/bin/bash
now=`date +"%F_%I-%M-%S"`
project=`pwd | awk '{n=split($0,a,"/"); split(a[n],b,"."); print b[1]}'` # Don't ask...
sandbox=$HOME/tmp/$project
if [ ! -d "$sandbox" ]; then
mkdir -p $sandbox
fi
backups=$HOME/backups
if [ ! -d "$backups" ]; then
mkdir -p $sandbox
fi
workspace=$HOME/pilots/$project
if [ ! -d "$workspace" ]; then
mkdir -p $workspace
fi
while read oldrev newrev ref
do
branch=`echo $ref | cut -d/ -f3`
if [ "master" == "$branch" ]; then
echo -e "Backup current version of $project in $backups/$project-$now.tar.gz.\n"
tar -czf $backups/$project-$now.tar.gz $workspace
echo -e "Build project in $sandbox.\n"
# Clean up sandbox
rm -rf $sandbox/*
# Checkout latest changes into sandbox
git --work-tree=$sandbox checkout -f $branch
# Build
cd $sandbox
npm install --production --loglevel error
# Stop running project
pm2 --mini-list stop $project
# Clean project folder contents
rm -rf $workspace/*
# Move new code to project folder
cp -R $sandbox/* $workspace/
# Go to project folder and strt project
cd $workspace
pm2 --mini-list start ./server/server.js --name $project
echo 'Changes pushed to staging environment.'
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment