Skip to content

Instantly share code, notes, and snippets.

@josue
Created July 13, 2010 02:13
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 josue/473355 to your computer and use it in GitHub Desktop.
Save josue/473355 to your computer and use it in GitHub Desktop.
#!/bin/bash
# ========== Your Server Config ==========
# your SSH credentials ...
SSH_User="user"
SSH_Host="server.com"
# directory where the tar.gz file will be uploaded then extracted from.
# recommened it be root directory so when you untar it falls nicely into place...
WWW_Dir="html/"
# the branch you would like to perform a git push from & perform the git log from.
# will be performed as: git push 'origin master'
Git_Branch=""
# ========= all your base are belong to us ========
Git_Root=$(readlink -f ./$(git rev-parse --show-cdup))
F_tar="publish_latest.tar"
F_gzip="$F_tar.gz"
counter=0
cd $Git_Root
Git_Clean=$(git status | grep -q "working directory clean" && echo "Y" || echo "N")
[ "$Git_Clean" = "N" ] && echo "\n* WARNING * ... you must 'git commit -a' all your changes first before updated the server.\n" && exit 0
echo
echo "Checking for LATEST files commited ONLY since last push ..."
echo
[ -f "$Git_Root/$F_gzip" ] && echo > $Git_Root/$F_gzip
for FILE in `git log origin/master..HEAD --name-only --diff-filter=ACM --pretty="%d" | sed '/^$/d' | awk '!x[$0]++'`
do
[ -f $FILE ] && tar -uvf $Git_Root/$F_tar $FILE && counter=`expr $counter + 1`
done
if [ "$counter" -gt "0" ]
then
gzip -f $Git_Root/$F_tar
echo
echo "copying to remote server ($SSH_Host) ..."
echo
scp $Git_Root/$F_gzip $SSH_User@$SSH_Host:$WWW_Dir
echo
echo "untarring archive on remote server and updating files ... finally will remove tar from remote..."
echo
ssh $SSH_User@$SSH_Host "cd $WWW_Dir && tar xvfz $F_gzip && rm -rf $F_gzip"
echo
echo "removing local archive copy..."
rm -f $Git_Root/$F_gzip
echo
echo -n "---- Want to Git Push changes to remote repo (Y or N): "
read PUSH
[ "$PUSH" = "Y" ] || [ "$PUSH" = "y" ] && git push $Git_Branch
echo
echo "done updating $SSH_Host server. check it out ..."
else
echo
echo "Already up-to-date."
fi
echo
echo "Go on code monkey, keep on innovating :)"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment