Skip to content

Instantly share code, notes, and snippets.

@klmr
Last active June 9, 2016 19:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save klmr/338f2eee6991a3fc15e4666a46257ff8 to your computer and use it in GitHub Desktop.
Save klmr/338f2eee6991a3fc15e4666a46257ff8 to your computer and use it in GitHub Desktop.
Deploy a generated GitHub page (`_site` subfolder)
#!/usr/bin/env bash
# Configuration
# The remote target branch name needs to be
# 1. DIFFERENT from the local development branch
# 2. Set as the default branch name on GitHub
remote_target_branch=master
generated_contents_dir=_site
get-working-branch() {
git status | head -n 1 | awk '{ print $NF }'
}
cleanup() {
git checkout "$local_working_branch"
}
# Ensure that the working branch is put back the way it was.
trap cleanup EXIT
# In the following
#
# * we create a new branch with the remote target’s name,
# * add the generated contents (`_site` directory),
# * commit the contents, and
# * remove everything but the generated contents, such that the `_site`
# directory becomes the root directory (similar to `mv _site .`).
# * Finally, we push the commit to the remote.
remote_name="$(git remote | head -n 1)"
local_working_branch="$(get-working-branch)"
# Ensure that the target branch is cleared, if it already existed.
git branch --delete --force "$remote_target_branch"
git checkout -b "$remote_target_branch"
git add --force "$generated_contents_dir"
git commit --message "New version $(date)"
git filter-branch --subdirectory-filter "$generated_contents_dir/" --force
git push "$remote_name" "$remote_target_branch" --force
# vim: ft=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment