Skip to content

Instantly share code, notes, and snippets.

@bonzini
Created September 19, 2019 15:47
Show Gist options
  • Save bonzini/1abbbdec739e77503945a3605e0e6442 to your computer and use it in GitHub Desktop.
Save bonzini/1abbbdec739e77503945a3605e0e6442 to your computer and use it in GitHub Desktop.
Git update hook that deploys a jekyll website on push
#! /bin/sh
# A simple update hook for git that deploys a jekyll website to a web server
# on the remote side.
#
# Author: Paolo Bonzini
source scl_source enable rh-ruby25
# -----------------------------------------------------------------------------
# $DEPLOY_ROOT is the path to the work area for this script. All users
# that can push to the repository must have write access to $DEPLOY_ROOT as
# well; the simplest thing to do create a group for all such users, and make
# the $DEPLOY_ROOT setgid for that group.
#
# The following subdirectories are used under $DEPLOY_ROOT:
# - $DEPLOY_ROOT/source: checkout of the git repository
# - $DEPLOY_ROOT/root: web server root
# - $DEPLOY_ROOT/old: backup of the previous contents of $DEPLOY_ROOT/root
# - $DEPLOY_ROOT/new: jekyll destination directory, then moved to
# DEPLOY_ROOT/root
# -----------------------------------------------------------------------------
DEPLOY_ROOT=/var/www/qemu-project.org
# Do not do anything unless the master branch is being updated
if test "$1" != refs/heads/master; then
exit 0
fi
umask 002
# Take a "lock" on $DEPLOY_ROOT/new, preventing concurrent updates.
if mkdir $DEPLOY_ROOT/new; then
trap 'rm -rf $DEPLOY_ROOT/new' EXIT
else
echo Another deployment might be in progress, please try again later.
exit 1
fi
set -e
trap 'echo Deployment failed.' ERR
# The current directory is the git tree being updated. Since it is
# a bare tree, use --work-tree to check out its contents.
mkdir -p $DEPLOY_ROOT/source
echo Checking out sources... $3
git read-tree $3
git --work-tree=$DEPLOY_ROOT/source checkout-index -f -a
test -f $DEPLOY_ROOT/source/Gemfile.lock && mv $DEPLOY_ROOT/source/Gemfile.lock $DEPLOY_ROOT/new
git --work-tree=$DEPLOY_ROOT/source clean -xdf
test -f $DEPLOY_ROOT/new/Gemfile.lock && mv $DEPLOY_ROOT/new/Gemfile.lock $DEPLOY_ROOT/source
# And now run jekyll in there, atomically replacing the contents of
# $DEPLOY_ROOT/root if successful.
cd $DEPLOY_ROOT/source
bundle install --path $HOME/.gem
if bundle exec jekyll build -d $DEPLOY_ROOT/new; then
rm -rf $DEPLOY_ROOT/old
if test -d $DEPLOY_ROOT/root; then
mv $DEPLOY_ROOT/root $DEPLOY_ROOT/old
fi
mv $DEPLOY_ROOT/new $DEPLOY_ROOT/root
echo Deployment succeeded.
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment