This site is a work-in-progress. Some of the information is incomplete and may not work as described. See the homepage for details.

What follows is a guide to building a shell script that will build your Jekyll website on your computer and push the built files to your repo’s deployment or production branch. You can see a fully-built script in my dotfiles where I deploy to GitHub Pages.

Do not run the script in the link above or begin the steps below until you have a development and production branch.

Define your development and production branches.

DEV="development"
PROD="production"

When I deploy to GitHub Pages, I use this:

DEV="master"
PROD="gh-pages"

Check if you are on the development branch and abort if not.

if [[ $(git rev-parse --abbrev-ref HEAD) != "$DEV" ]]; then
  echo "You should be on the $DEV branch."
  exit 1
fi

Check for uncommitted changes and stash if found.

if [[ -n $(git status -s) ]]; then
  git stash --include-untracked
  STASH=true
fi

Remove the temp directory from previous builds.

rm -rf $HOME/.tmp/jekyll

Create a temp directory to hold build output.

mkdir -p $HOME/.tmp/jekyll

Build Jekyll and send the output to the temp directory.

JEKYLL_ENV=production bundle exec jekyll build --destination $HOME/.tmp/jekyll

Checkout the $PROD branch.

git checkout $PROD

Get directory (so we can copy files into it). Delete all files in the $PROD branch (except dot files) to ensure only newly built files get added to the live site. Then copy the files from the temp directory into your git repo directory.

DIRECTORY=${PWD}
rm -rf "${DIRECTORY:?}/"*
cp -r $HOME/.tmp/jekyll/. $DIRECTORY

Add your files to the git index and commit to upstream.

git add . && git commit -m "Deploy changes" --verbose && git push

Go back to the development branch and pop your stash if you had one.

git checkout "$DEV"

if [[ $(git rev-parse --abbrev-ref HEAD) == "$DEV" && $STASH == true ]]; then
  git stash pop
fi