Publish to Github Pages

Blogging the Publishing of the Blog to Github


In this blog post we will be taking the Hugo website we created and publishing it via GitHub Pages.

To accomplish this we will be creating two GitHub repos:

  • hugo.${GIT_USERNAME} - to contain the hugo source for the blog
  • ${GIT_USERNAME} - to container the served static html for the blog

${GIT_USERNAME} will be mounted a submodule in hugo.${GIT_USERNAME} ’s public/ subdirectory.

Publish Hugo Site via Github


Save Hugo Source to Github

Create a repo for Hugo source

Note: Do not use ${GIT_USERNAME} for this, we will need it for the output of Hugo later.

Use hugo.${GIT_USERNAME}

Create Github Repo

Github New Repo

Add Github Repo as a remote

Note: hugo.${GIT_USERNAME} was created in the Inception blog post, see there for details as to how to set it up.

cd hugo.${GIT_USERNAME}
git remote add origin${GIT_USERNAME}/hugo.${GIT_USERNAME}

Push Hugo Source to Github

git push -u origin master

Save Hugo Generated Site to Github

Create a repo for Hugo Generated Site

You will need to name this repo ${GIT_USERNAME}

Create Github Repo Github New Repo

Add ${GIT_USERNAME} as submodule in public/

Now you can add, and push the submodule for the public/ directory.

git submodule add${GIT_USERNAME}/${GIT_USERNAME} public
git add .
git commit -s -m "Added submodule to subdir public/"
git push

Hugo generate site

$ hugo
                   | EN  
  Pages            | 10  
  Paginator pages  |  0  
  Non-page files   |  0  
  Static files     | 13  
  Processed images |  0  
  Aliases          |  1  
  Sitemaps         |  1  
  Cleaned          |  0 

Hugo write the static html it generates into the public/ subdirectory.

Pushing public/

cd public/
git add .
git commit -s -m "Hugo site generation from${GIT_USERNAME}/hugo.${GIT_USERNAME}$(cd ../;git rev-parse --short HEAD)"
git push

Updating reference in hugo.${GIT_USERNAME}

To finish up we need to make sure that hugo.${GIT_USERNAME} has the proper reference to the generated public/ site:

git add .
git commit -s -m "Add post on publishing to Github"

Dangerous Bends: A Warning about git clone and submodules

Because we are using submodules, you need to make sure that you use

git clone --recurse-submodules hugo.${GIT_USERNAME}

when cloning. Failure to do so will not clone your submodules (including your theme) which will lead to no end of troubles.