Add docs to a project
You already have an open-source repository with application code—a Go service, a
Rust CLI, a Python package, or a database extension—and you want real
documentation to live right beside the code so pull requests update code and
docs together. Putting a self-contained OINK site inside docs/ keeps Hugo
isolated from your main build while publishing cleanly on every push.
For a real production example of this layout, see AGW, which hosts its bilingual documentation and landing page inside a subfolder of its application repository.
Repository layout
Keep the Hugo configuration, module pin, and content completely inside docs/:
my-project/
my-project/
- cmd/your project's own source code
- internal/
- go.modyour project's own Go module (if it is a Go project)
- README.md
- .gitignoreexcludes docs/ build outputs without hiding app files
.github/
workflows/
- ci.yamlexisting application tests
- docs.yamlbuilds and deploys docs/ only when docs/ changes
docs/self-contained OINK sub-project
- hugo.ymlsite config with github_subdir: docs
- go.modpins github.com/pgsty/oink@vX.Y.Z
- go.sumverifies the pinned OINK release
content/
- _index.md
docs/
- _index.md
- install.md
data/
home/
- en.yaml
assets/
- icons/logo.svg
static/
- favicon.svg
Giving docs/ its own go.mod is deliberate: even if your project itself is
written in Go and already has a go.mod at the repository root, the two module
graphs stay completely separate so the documentation theme never enters your
application’s dependency tree.
Initialize the docs/ sub-project
Install Git, Go and Hugo per Prerequisites, then run these steps from your project’s repository root:
-
Create
docs/and pin OINKTerminalEnding the module path in
/docskeeps it distinct from the root module. Commit bothdocs/go.modanddocs/go.sumso CI resolves the exact same theme tag you tested locally. -
Write
docs/hugo.ymlwithgithub_subdirCreate
docs/hugo.yml. Compared to a standalone site, the crucial extra setting isparams.github_subdir: docs: without it, every Edit this page and history link points to/content/...at the repository root and returns 404 on GitHub.docs/hugo.yml -
Add your first page and preview
Write
docs/content/docs/_index.mdanddocs/content/docs/install.md(see From scratch for minimal examples), then preview from the repository root with-s docs(orcd docs && hugo serve):Terminal (from repository root)Open http://localhost:1313/docs/ and verify that the Edit this page entry in the page-actions menu beside the title opens
docs/content/docs/...in your repository.
Manage .gitignore cleanly
Hugo writes generated HTML, processed images, lock files and module caches into
the site directory. Add explicit docs/-prefixed ignore rules to your
repository’s .gitignore before running your first build, so generated output
never bloats Git history or creates merge conflicts—and so you never
accidentally ignore a public/ or resources/ directory used by your main
application:
Know which files stay out of Git and which ones CI requires:
| Path | Track in Git? | Why |
|---|---|---|
docs/public/ |
Ignore | Built HTML/CSS/JS output; regenerated cleanly by CI on every deploy |
docs/resources/ |
Ignore | Cached processed images (_gen/); stale copies bloat diffs |
docs/.hugo_build.lock |
Ignore | Ephemeral Hugo build lock file |
docs/.hugo_cache/ |
Ignore | Local or CI Hugo cache directory |
go.work, go.work.sum |
Ignore | Machine-local Go workspace paths break other contributors and CI |
docs/hugo.yml, docs/go.mod, docs/go.sum |
Commit | Site configuration and exact checksum-verified OINK release |
docs/content/, docs/data/, docs/assets/, docs/static/ |
Commit | Authored Markdown, landing data, logos, CSS and static files |
Run a production build once and check git status --short to verify that zero
generated files show up as untracked:
Auto-build on GitHub Pages
Add one GitHub Actions workflow at .github/workflows/docs.yaml. Two details
differ from a standalone docs repository: every build step runs inside
working-directory: docs (with cache-dependency-path: docs/go.sum and
artifact path docs/public), and the paths: filter ensures code-only commits
do not waste runner minutes rebuilding unchanged docs.
How the build logic hangs together:
paths: ['docs/**', '.github/workflows/docs.yaml']triggers automatic deploys only when documentation or the workflow itself changes;workflow_dispatchstill lets you trigger a deploy manually at any time.defaults.run.working-directory: docsmakesgo mod downloadandhugooperate strictly ondocs/go.modanddocs/hugo.ymlwithout touching a root-levelgo.mod.--baseURL "${{ steps.pages.outputs.base_url }}/"injects the exact URL GitHub Pages serves—including the/<PROJECT>/subpath—so search indexes, stylesheets and canonical links never 404.--printPathWarningswithout--panicOnWarningkeeps OINK’s warnings readable in the log without aborting on non-fatal diagnostics.
In GitHub, open Settings → Pages → Build and deployment → Source, choose
GitHub Actions, and push to main.
Auto-build on Cloudflare Pages
Cloudflare Pages supports two deployment models for a docs/ sub-project. Pick
one per Cloudflare Pages project—never connect both Git integration and
Direct Upload to the same project:
Building in GitHub Actions and uploading docs/public via Wrangler lets you
run the latest Hugo release (even when Cloudflare’s default build image ships
an older Hugo), plus native paths: filtering so application-only pushes skip
doc deploys.
Set up Cloudflare once:
- In the Cloudflare dashboard, create a Direct Upload Pages project.
- Add repository secrets
CLOUDFLARE_ACCOUNT_IDandCLOUDFLARE_API_TOKEN(scoped to Account → Cloudflare Pages → Edit). - Set optional repository variables
CLOUDFLARE_PROJECT_NAME(defaults to the repository name) andCLOUDFLARE_SITE_URLonce you attach a custom domain such ashttps://docs.example.com/.
If you prefer Cloudflare to run Hugo directly with no GitHub Actions workflow, connect the repository in Workers & Pages → Create → Pages → Connect to Git and configure these build settings:
| Setting | Value | Why |
|---|---|---|
| Root directory (path) | docs |
Runs hugo inside docs/ so it reads docs/hugo.yml and docs/go.mod |
| Build command | hugo --cleanDestinationDir --gc --minify --environment production --printPathWarnings |
Matches OINK’s production build flags |
| Build output directory | public |
Relative to Root directory (docs), publishing docs/public |
| Include watch paths | docs/* |
Skips doc builds on commits that only touch application source code |
HUGO_VERSION |
0.170.1 (or current release) |
Overrides Cloudflare’s older default Hugo (OINK is tested on 0.170+) |
GOWORK / HUGO_MODULE_WORKSPACE |
off |
Prevents workspaces from overriding docs/go.mod |
For branch preview builds, override the preview build command with
--environment staging --baseURL "$CF_PAGES_URL" so Cloudflare preview URLs
emit noindex and never compete with your production domain in search engines.
Verify before you merge
- Run
hugo -s docs --cleanDestinationDir --gc --minify --environment production --printPathWarningsand confirm the log ends withTotal in …and no unexpected warnings. - Run
git status --short:docs/public/,docs/resources/anddocs/.hugo_build.lockmust not appear. - Commit
docs/hugo.yml,docs/go.mod,docs/go.sum,docs/content/,.gitignoreand your workflow file. - After the workflow turns green, open the live URL and check search, dark
mode, canonical links, and Edit this page (it must open the file under
docs/content/).
Related
- Deploy — custom domains,
baseURL, CSP headers, preview indexing, and rollback - Browse locally — run
hugo serve -s docswhile writing code and docs side by side - From scratch — standalone OINK setup and offline / submodule install methods
- Configuration — full
hugo.ymlreference, includinggithub_subdir