Hugo Environment Variable Magic

2025-10-22 (updated 2025-10-25)
Notes | 1 min read
#hugo #ssg

You can overwrite hugo configuration values with environment variables!

This is useful for things like setting the baseURL dynamically in CI.

export HUGO_BASEURL="$CI_ENVIRONMENT_URL"
hugo

It also supports setting indented values, for example imagine this hugo configuration:

params:
  some:
    value: hello

Which can be overwritten like this:

export HUGOxPARAMSxSOMExVALUE="bye"
hugo

But, more importantly, this can be used to inject dynamic values at hugo build time to be used later on in templates.

An example would be injecting information about the latest commit in the repository in addition to the normal git info available in hugo:

export HUGOxPARAMSxREPOxLAST_COMMITxSUBJECT=$(git log -1 --format=%s)
export HUGOxPARAMSxREPOxLAST_COMMITxDATE=$(git log -1 --format=%cs)
export HUGOxPARAMSxREPOxLAST_COMMITxHASH=$(git log -1 --format=%H)

Which can then be used in templates like this:

{{ with .Site.Params.repo.last_commit }}
  <a href="https://gitlab.com/mkamner/marco.ninja/-/commit/{{ .hash }}">{{ .subject }} ({{ .date }})</a>
{{ end }}