Notes

load content from a github repo for a hugo website as a module

Edit on GitHub

Hugo
2 minutes

NOTES:

  • There is an existing bug that will not let you initialize the module if you have an existing module.toml. Neither will it let you use any other hugo mod commands.
  • Use the apt package instead of snap. It will make your life easier by eliminating weird permission errors with SSH auth

https://gohugo.io/commands/hugo_mod/

In order to load modules, your site needs to be a module itself.

1hugo mod init github.com/aamnah/fazalashfaq.com
2
3# this will create a file called: go.mod
1hugo mod init github.com/aamnah/fazalashfaq.com --config module.toml
2
3# this will create a file called: go.mod

Define your modules inside config/_default/module.toml

 1# file: config/_default/module.toml
 2[[imports]]
 3  # import the notes repo as a Hugo module
 4  path = "github.com/aamnah/notes.fazalashfaq.com"
 5  disable = false
 6
 7  # mount the imported repo in this location
 8  [[imports.mounts]]
 9  # root of repo can be specified with `.`
10  # if bringing in a subdirectory, mention the subdriectory path in source
11  source = "."
12  target = "content/notes"
13  excludeFiles = ["README.md"]

Add a Hugo module

1hugo mod clean --all
2hugo mod get -u
3hugo mod get -u github.com/USER_OR_ORG/REPO
4hugo mod vendor

Troubleshooting

If you are getting fatal errors related to Git and Username then it is very likely that the module you are trying to import is not public, i.e. you’re importing a module that is a private repo.

12:14:49 AM: go: github.com/user/repo@v0.0.0-20240626152414-39076531a1f3: invalid version: git ls-remote -q origin in /opt/build/cache/hugo_cache/modules/filecache/modules/pkg/mod/cache/vcs/96d4f25fe353b629d6aa0e8760275debc948f906cf940669e8b1a5766c08262c: exit status 128:
22:14:49 AM: 	fatal: could not read Username for 'https://github.com': terminal prompts disabled
32:14:49 AM: Confirm the import path was entered correctly.
42:14:49 AM: If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.

By default, go get uses HTTPS. If GIT_TERMINAL_PROMPT is set to 0 or false it will not prompt your for Git credentials and fail instead. And if you have 2FA activated for your git provider, then you either need to use a PAT (Personal Access Token) to get the repo over HTTPS or use SSH instead. Read this.

1# Enable prompting for credentials
2export GIT_TERMINAL_PROMPT=1
1# Mark some repos as private
2
3export GOPRIVATE=github.com/ORG_OR_USER/* 
4# OR
5go env -w GOPRIVATE=github.com/ORG_OR_USER/* 
1# Use SSH versions of repo urls
2git config --global --add url."git@github.com:".insteadOf "https://github.com/"
 1# Specify an access token to be used
 2FILE="$HOME/.netrc"
 3USERNAME=""
 4TOKEN=""
 5
 6if [ ! -f ${FILE} ]; then
 7  echo -e "${FILE}" does not exist
 8  touch ${FILE}
 9fi 
10
11echo -e "machine github.com login ${USERNAME} password ${TOKEN}" >> ${FILE}