Set Up Hugo Blog
Table of Contents
Setting Up a Hugo-based Blog on GitHub Pages
I will be showing you how I set up a Hugo blog, being published to GitHub Pages using GitHub Actions for free.
I chose GitHub Pages because it offers the ability to host static websites for free with minimal configuration, and I have a new interest in GitHub Actions. I noted that the “modern” method to deploy a website on GitHub is with Actions and I couldn’t pass up the opportunity. While going through the process of deploying this blog, I found that no single resource had all the information I needed to get everything stood up. I figured, “hey I’m making a blog why not write what I wish I had” and that’s how this post came to be. The process is fairly simple from start to finish once you know where all the moving parts are located.
Follow along and lets deploy a static webpage to GitHub Pages, complete with Custom Domain that is secured via SSL.
Install Hugo on your Computer
I will be performing these actions from my MacBook, the basic ideas should be compatible across any platform beyond the initial install.
I installed Hugo via Homebrew:
| |
Create a Hugo site locally
This will setup the files required by Hugo to operate. It will create a directory named after your site.
Let’s create our site:
| |
Next, I will install the “Hugo Blog Awesome” theme as a git submodule. You can skip this or set up a different theme, but make sure to use submodules for any theme you choose (this is required so that GitHub Actions works properly).
First, initialize git and add the theme as a submodule:
| |
I opened the Hugo site directory in VS Code. You then edit the hugo.toml file to include this line for your theme:
theme = "hugo-blog-awesome"
Test Run
Let’s test it out locally before pushing it to GitHub. First we need to create a new post so we can show content.
| |
This is going to create a page under the content subdirectory, we’ll go add the content “Hello, World!” to the page.
| |
Here is output as I get the first page to show. Don’t forget to remove the draft tag like I did.
| |
Push it to GitHub
I created a public repository directly on GitHub. I then changed the name of my local directory to match so it’s easy to find in the future.
| |
Now we need to set up our local directory to be linked with our public repo. You do this by adding the remote origin address. GitHub will give you directions in your empty repo if you need more assistance.
| |
Setup GitHub Actions to Deploy
To configure GitHub Actions we need to define the workflow file. I am creating mine like this:
.github/workflows/hugo.yaml
I will show the details I put in my workflow file, I tried to add comments to be as descriptive of each step as possible when I felt the names didn’t fully describe the intent.
| |
Update GitHub Pages Settings
In your GitHub account under the public repo you can go into the settings to configure GitHub Pages. We’re going to set our source to be GitHub Actions since we’ll be deploying with GitHub Actions. There are other methods that I won’t go into details on in this post.
- Go to your repository on GitHub
- Click Settings → Pages
- Under “Source”, select GitHub Actions
Deploy your website
We’ve done all the work. Now you need to commit your changes and push them up into your repository.
| |
And just like that, GitHub Actions should automatically build and deploy your site to GitHub Pages. You can watch the deployment in the “Actions” tab of your repository. Once that deployment completes your site should be accessible via https://yourusername.github.io/yourreponame.
Key Points to Remember
- Your repository must be public for free GitHub Pages, you can have a private repo in the paid version
- Use git submodules for themes, not just
git clone— maintains updateable theme connection - Set Pages source to “GitHub Actions”, this is required for GitHub Actions to deploy the website
- The workflow uses Hugo extended version which is required for most modern themes
Optional Steps
I am going to be using a custom domain for my GitHub Pages so I thought I would share the requirements to do that. If you want to use your own domain instead of the default GitHub Pages URL, I will set it to always use SSL without having to provide my own certificates.
Configure DNS (Using Cloudflare)
My domain registrar is Cloudflare. I am sure these steps are similar among other registrars, but I will only be showing how I accomplished this task with Cloudflare specifically.
Add a CNAME record for your domain to point your desired subdomain to GitHub.
| |
This will make it so that when you go to blog.yourdomain.com the DNS record will respond with the GitHub DNS entry.
Configure GitHub Pages Custom Domain
- Go to your repository Settings → Pages
- In the “Custom domain” field, enter your domain:
blog.yourdomain.com - Click Save
Add CNAME file to Hugo
Create a CNAME file in your Hugo project. This is required by GitHub Pages for CNAME to be used (which is what we configured in Cloudflare).
| |
Update your hugo.toml
Change the baseURL in your hugo.toml to match the CNAME as well. This is used to determine absolute URLs in your website.
| |
Push the changes to GitHub
| |
Wait for SSL and enable HTTPS
- Wait 5-10 minutes for DNS propagation, this can take a long time
- Wait 15-30 minutes for GitHub to generate an SSL certificate, this doesn’t start until GitHub validates DNS
- Go back to Settings → Pages
- Once you see a green checkmark next to your domain, check “Enforce HTTPS”, this will redirect HTTP to HTTPS
Your site should now be live at https://blog.yourdomain.com with a valid SSL certificate.