I’ve been building websites with Hugo for a few years now.
After much tweaking and experimenting
I have settled on a stable workflow,
regardless of the site’s theme or subject.
That workflow
(which I might talk about in the future)
involves running a hell of a lot of commands on the command line.
I could (and have done)
CTRLR my way through the command-line history
and bring up the command I need,
but that’s a little too much. ¯\_(ツ)_/¯
Instead, I’ve landed on a Makefile that collects recipes
for my most frequently used commands across projects.
It’s at the end of this post.
Rationale behind the commands
Most commands are self-explanatory
and there’s actually a help command in the file itself,
so I’ll explain only a few things.
Every Hugo project I have is a Git repository
with at least two branches main and public.
I use git worktrees
to have both branches checked out at all times,
with the public branch checked out on a directory ../public,
relative to the directory where the main branch is checked out.
That’s the reason of the default value of DEPLOY_DIR.
When I’m developing I have a hugo server running
to watch for live changes in localhost.
Once I’m done with that I build the site in Hugo’s default location.
Then I sync the public files into the public worktree.
Finally, I commit and push that dir to GitHub,
where I have configured a GitHub Pages setup.
I collected those commands into the Makefile recipes below.
That way my workflow is reduced to a few easy-to-remember commands:
# ==============================================================================
# Hugo project Makefile
# ==============================================================================
# git tracked dir to deploy site
DEPLOY_DIR ?= ../public
.PHONY: help
.PHONY: dev dev-drafts dev-nofr
.PHONY: build sync deploy preview
.PHONY: metrics deps-install deps-check clean clean-cache reset-vendor
help: @echo "" @echo "Hugo project Make commands" @echo "==========================" @echo "" @echo "Development:" @echo " make dev start web server with dev env" @echo " make dev-drafts include drafts and future content" @echo " make dev-nofr disable fast render" @echo "" @echo "Production:" @echo " make build build production" @echo " make sync sync to deploy dir" @echo " make deploy commit and push from deploy dir" @echo " make preview preview production build in local server" @echo "" @echo "Utilities" @echo " make metrics build with hugo metrics" @echo " make deps-install install npm dependencies from package-lock.json" @echo " make deps-check check installed dependencies" @echo " make clean remove public dir" @echo " make clean-cache remove cache" @echo " make reset-vendor remove _vendor dir and download again" @echo ""dev: hugo server --logLevel info
dev-drafts: hugo server --buildDrafts --buildFuture --logLevel info
# useful for investigating rendering issues
dev-nofr: hugo server --disableFastRender --logLevel info
build: hugo --minify --gc --cleanDestinationDir
sync: @git -C "$(DEPLOY_DIR)" rev-parse --is-inside-work-tree >/dev/null 2>&1||{ echo "ERROR: Not a valid Git worktree: $(DEPLOY_DIR)"; exit 1; } rsync --info=progress2 -a --delete --exclude="/.git""./public/""$(DEPLOY_DIR)/"deploy: git -C "$(DEPLOY_DIR)" add -A
git -C "$(DEPLOY_DIR)" commit -m "publish" git -C "$(DEPLOY_DIR)" push origin
preview: hugo server --environment production \
--minify --gc \
--disableLiveReload \
--port=1314# I use Hugo's default 1313 for dev environmentmetrics: hugo --minify --gc \
--templateMetrics \
--templateMetricsHints \
--printMemoryUsage \
--printPathWarnings \
--printUnusedTemplates
# in case I got any dependency on Node.js (which often I don't)
deps-install: npm ci
deps-check: npm ls --depth=0# few utilities to force a fresh environment
clean: rm -rf "public"clean-cache: rm -rf "resources/_gen"reset-vendor: rm -rf _vendor
hugo mod vendor