Script to publish Hugo sites for Github
While deploying my recent hugo website to github, I re-learnt that, you can only publish gh-pages from master
branch for user or organization repository.
So had to change my workflow to remove all old static files from master
and copy generated contents from hugo.
It becomes tedious to remember not to delete CNAME
or hugo
folder from my repo everytime I had to copy the generated content. So I wrote a bash
script to automate the tasks for me.
Works great, now I do not have to remember the critical files and just ./publish.sh
from my master
branch.
Be very careful as it run rm -rf
at line #5. So place the publish.sh
only in your repo’s root folder.
publish.sh
#!/usr/bin
echo "[ 1/3 ] Going to remove previously generated files"
# to delete files that are generated (.git and .ignore as they are hidden)
rm -rf `ls | grep -v "CNAME\|hugo\|README.md\|publish.sh"`
# switch to hugo directory and run hugo to build genereate content in hugo/docs and move generated files back to ./
echo "[ 2/3 ] To build static contents and copy"
cd hugo && hugo && mv docs/* ../ && cd ..
echo "COMPLETE"
# git commit changes as "publish"
echo "[ 3/3 ] Commit changes and push to github"
git add .
git commit -m "publish"
git push origin --all
echo "COMPLETE"
Latest version of the script can be found at https://github.com/palaniraja/palaniraja.github.com/blob/master/publish.sh