Last active
June 9, 2016 19:54
-
-
Save klmr/338f2eee6991a3fc15e4666a46257ff8 to your computer and use it in GitHub Desktop.
Deploy a generated GitHub page (`_site` subfolder)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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