Skip to content

Instantly share code, notes, and snippets.

@lottspot
Last active December 23, 2015 17:19
Show Gist options
  • Save lottspot/6668337 to your computer and use it in GitHub Desktop.
Save lottspot/6668337 to your computer and use it in GitHub Desktop.
A post-receive deployment hook for octopress repositories.

Depends on:

  • Octopress git repo
  • tar
  • rsync
  • rake

Fill in the variables appropriately and copy to hooks/post-receive in your bare git repo

  • git_branch: git branch which holds the source files for the live site
  • file_bashrc: path to a ,bashrc (or really any bash file) which provides rbenv and adds a PATH directory containing rake
  • dir_tmp: what directory to create a temporary copy of the $git_branch index inside of. THIS DIRECTORY WILL BE DESTROYED BY THE cleamTmp CALL AT THE END OF THE SCRIPT. It will also be automatically created if it does not yet exist.
  • dir_src: The directory which Octopress compiles the static html files into. By default, the tree subdirectory 'public/'
  • dir_live: Webroot directory

Copy configured script to hooks/post-receive in the bare repository on the remote end.

#!/bin/bash
stdin=$(cat)
git_branch="master"
file_bashrc="$HOME/.bashrc"
dir_tmp="/tmp/octopress/"
dir_src="$dir_tmp/public/"
dir_live="/var/www/"
main() {
source $file_bashrc
if isBranch
then
echo "Pushing changes live from branch $git_branch to $dir_live..."
buildTmp || (echo "Error ocurred building $dir_tmp" >&2 && exit 1)
checkoutBranch || (echo "Error occurred checing out $git_branch into $dir_tmp" >&2 && exit 1)
deployFiles || (echo "Error ocurred deploying files" >&2 && exit 1)
cleanTmp || (echo "Warning: Error ocurred cleaning up $dir_tmp" >&2)
echo "Successfully deployed site files to $dir_live"
return 0
else
echo "No changes on branch $git received. No live push needed."
return 0
fi
}
isBranch() {
branch=$(echo $stdin | perl -ne "print if ( \$_ =~ /refs\/heads\/$git_branch/ )")
if [[ -n "$branch" ]]
then
return 0
else
return 1
fi
}
buildTmp() {
if [[ -d $dir_tmp ]]
then
return 0
else
mkdir -p $dir_tmp
return $?
fi
}
checkoutBranch() {
git archive $git_branch | tar -xf - -C $dir_tmp
return $?
}
deployFiles() {
cd $dir_tmp
rake generate
cd - 2>&1 >/dev/null
test $? -ne 0 && return 1
rsync -ru $dir_src $dir_live
return $?
}
cleanTmp(){
rm -rf $dir_tmp
return $?
}
main $*
@alx
Copy link

alx commented Jan 17, 2014

I've had an issue with the 'git archive | tar' command in checkoutBranch() method, not sure why.

I've updated this method in my fork using the git archive documentation.

@alx
Copy link

alx commented Jan 17, 2014

Another note, use #/bin/bash -l with the -l argument.

I had issue loading rvm from $HOME/.bashrc without this argument.

Thanks a lot for your script :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment