Hugo Setup
Published 29.07.2023 , Last Edited 01.08.2023My deployment is now (almost) entirely automated. I write Markdown files in one repo, push the changes, and my site in another repo is automatically rebuilt - changes appear to my website about a minute after my push.
Repo structure
I have two GitHub repositories I’m using for this blog:
-
cnnrshd/connorshade.com - This repo holds the output of my
hugo new site connorshade.com
command. Everything hugo-related is here: Themes, content, images, the hugo config, etc. It is private since I use it for staging drafts.1~/git/connorshade.com$ tree -d -L 2 2. 3├── archetypes 4├── assets 5├── content 6│ ├── addons 7│ ├── post 8│ └── service-to-shell 9├── data 10├── layouts 11│ ├── _default 12│ └── partials 13├── public 14│ ├── addons 15│ ├── author 16│ ├── categories 17│ ├── post 18│ ├── projects 19│ ├── sections 20│ ├── service-to-shell 21│ └── tags 22├── resources 23│ └── _gen 24├── static 25└── themes 26 └── hugo-bearcub
-
cnnrshd/cnnrshd.github.io - This is the public repo that holds the hugo-generated content. I have it linked as a submodule within
cnnrshd/connorshade.com
at the path “./public”, which is the default output directory forhugo
.
With this workflow I am able to keep private any drafts I want, running hugo
will automatically build any changes, and with some light automation on GitHub Actions I should be able to build and deploy updates any time there is a push to cnnrshd/connorshade.com
.
Theme
I’m using bear cub right now. It’s pretty light weight and doesn’t require npm
to compile some HTML. I tried to use Gruvbox but the idea of installing nvm
to install node
to install npm
to install >100 dependencies to compile a webpage is disgusting, despite how nice the page looks. I also like that bear cub is light weight (for HTML) - my webapp page is currently ~25kB, from a ~7.8kB source Markdown file, and most of the bloat seems to be styling on the code blocks.
Automation
Right now I have:
- A GitHub workflow that monitors for Pushes to
cnnrshd/connorshade.com
- On a push, the repo is checked out, a new hugo site is built, and
cnnrshd/cnnrshd.github.io
is updated
- On a push, the repo is checked out, a new hugo site is built, and
- Act for local testing of GitHub actions - got this idea from Beyond Fireship, who made a video titled How GitHub Actions 10x my productivity, I learned about a tool called act which allows you to run GitHub Actions locally.
GitHub Actions Workflow
Here is the content of my GitHub Actions workflow:
1on:
2 push:
3 workflow_dispatch:
4jobs:
5 build:
6 runs-on: ubuntu-latest
7 permissions:
8 contents: write
9 steps:
10 - name: Checkout with submodules
11 uses: actions/checkout@v3
12 with:
13 submodules: 'recursive'
14 fetch-depth: 0
15 token: ${{ secrets.WEBSITE_TOKEN }}
16 - name: List the files in the repository
17 run: |
18 ls -R
19 - name: Download and Install Hugo
20 run: |
21 wget "https://github.com/gohugoio/hugo/releases/download/v0.116.1/hugo_extended_0.116.1_linux-amd64.deb" -O hugo.deb
22 sudo dpkg -i hugo.deb
23 - name: Checkout master in the submodule
24 run: |
25 cd public
26 git checkout master
27 - name: Delete everything in the public folder # Prevent errors from old files
28 run: |
29 rm -rf public/*
30 - name: Build the website # this should write all changes to the public folder
31 run: |
32 hugo
33 - name: Ensure the CNAME file is in the public folder
34 run: |
35 echo "connorshade.com" > public/CNAME
36 - name: Commit and push changes to submodule
37 run: |
38 cd public
39 git config --global user.email "github-actions[bot]@users.noreply.github.com"
40 git config --global user.name "github-actions[bot]"
41 git add .
42 git commit -m "Automated submodule update"
43 git push origin master
44 - name: Update the submodule reference in the main repository
45 run: |
46 git config --global user.email "github-actions[bot]@users.noreply.github.com"
47 git config --global user.name "github-actions[bot]"
48 git add public
49 git commit -m "Update submodule reference"
50 git push
Most of that should be pretty self-explanatory. There is room for improvements:
- A better command to check
https://github.com/gohugoio/hugo/releases/latest
, look for ahugo_extended*amd64.deb
, download and install that - Right now the Action fails on “Commit and push changes to submodule” if the submodule didn’t change. This isn’t a functionality issue, but it does mean that my Actions show up as red if I do something like: updating a draft post, Change .gitignore, add a comment to the workflow. I’d like to fix this.