Skip to content

Instantly share code, notes, and snippets.

@gmolveau
Last active December 8, 2019 16:38
Show Gist options
  • Save gmolveau/860c0200f96f0cb4fb64243d1dcc943d to your computer and use it in GitHub Desktop.
Save gmolveau/860c0200f96f0cb4fb64243d1dcc943d to your computer and use it in GitHub Desktop.
GoHugo publish on gh-pages/docs branch for Github Pages
#!/usr/bin/env sh
REMOTE="origin"
BRANCH="gh-pages"
HUGO_FOLDER="public"
if ! git diff-files --quiet --ignore-submodules --
then
echo "The working directory is dirty. Please commit any pending changes."
exit 1;
fi
echo "Switching to master branch"
echo "If you're using git flow, please make sure master branch is up to date."
git checkout master
echo "Deleting old publication"
rm -rf ${HUGO_FOLDER}
mkdir ${HUGO_FOLDER}
git worktree prune
echo "Checking out ${BRANCH} branch into {HUGO_FOLDER}"
git worktree add -B ${BRANCH} {HUGO_FOLDER} --no-checkout
echo "Generating site"
hugo
echo "Writing CNAME file"
if [ ! -f ./CNAME ]; then
read -p "Enter the production URL (e.g. myblog.idontknow.com): " PRODUCTION_URL
echo "${PRODUCTION_URL}" > {HUGO_FOLDER}/CNAME
else
cat CNAME > {HUGO_FOLDER}/CNAME
fi
echo "Updating ${BRANCH} branch"
cd {HUGO_FOLDER} && git add --all && git commit -m "Publishing (publish.sh)" && cd ..
echo "Pushing to github"
git push ${REMOTE} +${BRANCH}
echo "Finished, cleaning up"
rm -rf {HUGO_FOLDER}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment