Other Posts

Krazy Kevin


crazy enough to be creative



blog

résumé

Hugo x Github Pages

It has been 2 months since I switched from Hexo to Hugo. Everything went smoothly. Until yesterday, I made a small mistake. I messed up my entire blog Git repository, then I have to reconfigure the part of deploying Hugo website to Github Pages…

To be honest, the initial configuration is simple but I cannot recall it at all. That’s why I finally decide to write down this quick note, for the forgetful people, like me.

Hugo has a good doc to explain how to host on Github Pages. Based on it, my configuration is following:

Add Worktree

Basally, I created 2 repositories for my blog. One is the theme I used, it’s called uno. The other one is for both posts and generated static files. Its name is krazycavin.github.io, a name predefined by Github Pages. I decide the put them in one repository because it’s easy to maintenance. I separated them into 2 different branches:

  • Branch hugo: posts, assets…
  • Branch master: public/ folder with all the generated static files

Then I need to link the public/ to master branch:

rm -rf public
rm -rf ./worktrees
git worktree add -B master public origin/master

Create deploy script

Here is my deploy script deploy.sh. The theme I’m using is uno:


#!/bin/sh

# Create worktree
# git worktree add -B master public origin/master

# Build the project.
hugo -t uno

# Go To Public folder
cd public

# Add changes to git.
git add -A

# Remove original js files
cd js
ls -1 --color=none | grep -v "min.js" | xargs rm -f

# Remove original css files
cd ../css
ls -1 --color=none | grep -v "min.css" | xargs rm -f
cd ..

# Commit changes.
msg="rebuilding site `date`"
if [ $# -eq 1 ]
    then msg="$1"
fi
git commit -m "$msg"

# Push source and build repos.
git push origin master

# Come back
cd ..

Every time after running the script deploy.sh, it will generate new static files and push them to master branch. The changes will be published immediately by Github Pages.

That’s all, a piece of 🍰