Skip to content

Instantly share code, notes, and snippets.

@flacodirt
Last active December 17, 2015 09:59
Show Gist options
  • Save flacodirt/5591938 to your computer and use it in GitHub Desktop.
Save flacodirt/5591938 to your computer and use it in GitHub Desktop.
Git post-receive to publish website
#!/bin/bash
# @author brockhensley.com
# @url git clone https://gist.github.com/5591938.git
# store the arguments given to the script
read oldrev newrev refname
# Set Parameters
PROJECT=AWOMS
LOGFILE=./post-receive.log
GITDIR=~/homes/$PROJECT/$PROJECT.git
DEPLOYDIR=~/public_html
BACKUPDIR=~/backups
mkdir $BACKUPDIR
# Record push timestamp, branch name, SHAs
time_suffix=`date "+%Y-%m-%d-%H-%M-%S"`
LOGMSG="Received push request at $time_suffix"
echo -e $LOGMSG && echo -e $LOGMSG >> $LOGFILE
LOGMSG=" - Old SHA: $oldrev New SHA: $newrev Branch Name: $refname"
echo -e $LOGMSG && echo -e $LOGMSG >> $LOGFILE
# Backup
cd $DEPLOYDIR
LOGMSG="Creating Backup (timestamp: $time_suffix) ..."
echo -e $LOGMSG && echo -e $LOGMSG >> $LOGFILE
tar czf $BACKUPDIR/$PROJECT.$time_suffix.tgz *
# Remove the existing data
LOGMSG="Removing existing data..."
echo -e $LOGMSG && echo -e $LOGMSG >> $LOGFILE
rm -fr $DEPLOYDIR/*
# Update the deployed copy
LOGMSG="Starting code update" >> $LOGFILE
echo -e $LOGMSG && echo -e $LOGMSG >> $LOGFILE
cd $GITDIR
GIT_WORK_TREE="$DEPLOYDIR" git checkout -f
# Copy config files again since we removed everything
LOGMSG="Copy config files again since we removed everything" >> $LOGFILE
echo -e $LOGMSG && echo -e $LOGMSG >> $LOGFILE
cp ~/homes/$PROJECT/$PROJECT.git/customConfig.php $DEPLOYDIR/config
cp ~/homes/$PROJECT/$PROJECT.git/dbConfig.php $DEPLOYDIR/config
# Done
time_suffix=`date "+%Y-%m-%d-%H-%M-%S"`
LOGMSG="Finished code update at $time_suffix"
echo -e $LOGMSG && echo -e $LOGMSG >> $LOGFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment