Skip to content

Instantly share code, notes, and snippets.

@harto
Created February 22, 2014 08:10
Show Gist options
  • Save harto/9150355 to your computer and use it in GitHub Desktop.
Save harto/9150355 to your computer and use it in GitHub Desktop.
A general-purpose deployment script for GitHub Pages
#!/usr/bin/env bash
set -x
set -e
# Deploys a directory (within a git repo) to github.io by jamming it into the
# `gh-pages` branch.
#
# Note: it's assumed that you don't care about tracking history for `gh-pages`,
# as this script clobbers the branch every time.
if [ ! -d "$1" ]; then
echo "Usage: $0 <directory>"
exit 1
fi
BUILD=$(cd "$1"; pwd) # resolve absolute path
SHA=$(git rev-parse HEAD)
ORIGIN=$(git config --get remote.origin.url)
WORKING=".deploy-$SHA"
# Clone repo into a tmp directory, where we can mess around with impunity
git clone . $WORKING
cd $WORKING
# Prepare pristine gh-pages branch
git branch -D gh-pages >/dev/null 2>&1 || true
git checkout --orphan gh-pages
git rm -rf .
# Commit deployable files
cp $BUILD/* .
git add -A
git commit -m "Build $SHA"
# Clobber remote gh-pages
git push -f $ORIGIN gh-pages
# Clean up
cd -
rm -rf $WORKING
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment