Documentation as Code
Store documentation in the repository and treat it like software: plain text, version control, pull request review, automated checks, diagrams as code, and versioned publishing.
Introduction
Documentation in a wiki drifts. Nobody reviews edits, nothing links a page to the code change that made it wrong, and no test fails when an instruction stops working. Meanwhile the code goes through review, version control, and a pipeline.
Documentation as code applies the same practices to documents. It does not make writing easier, but it makes accuracy maintainable, which is the harder problem.
Learning Objectives
- Explain the principles of documentation as code and their benefits and limits.
- Organize documentation in the repository with a clear structure.
- Integrate documentation into the pull request workflow.
- Automate checks: build, links, style, examples, and specifications.
- Write diagrams as code.
- Version and publish documentation alongside releases.
- Use AI to review documentation changes and to propose updates.
1. Principles
- Plain text formats (usually Markdown) that diff, merge, and review well.
- Version control with history, blame, and branches, the same as code.
- Review by pull request before changes are published.
- Docs live with the code they describe (or in a clearly linked repository).
- Automated build and checks run on every change.
- Automated publishing from the main branch, with versions per release.
- Single source of truth: generate what can be generated; write by hand only what needs a human.
Limits to be honest about: contributors who do not use Git need an easy path (a web editor or a form that opens a pull request), and some content (screenshots, long-form manuals for non-technical users) needs extra tooling. The principle is the same; the tooling adapts.
2. Repository Layout
A simple structure that mirrors the audiences:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
README.md Front door (lesson 8)
CONTRIBUTING.md How to contribute code and docs
docs/
index.md Documentation home and navigation
_meta/ Style guide, glossary, templates, prompts
getting-started/ Tutorials for developers
guides/ How-to guides
api/
openapi.yaml API contract (source of truth)
guides/ Authentication, errors, webhooks
architecture/
overview.md Context, containers, components
runtime-views.md Key flows
decisions/ Architecture Decision Records
reference/ Configuration, CSV format, error codes
user/ End-user documentation
operations/ Runbooks (developed later in the series)
release-notes/ Per-version notes and known issues
mkdocs.yml Site configuration (any generator works)
Choose a structure for readers, not for the organization chart. Keep paths stable once published, and add redirects when you move a page, so that links in tickets and chat keep working.
3. Choosing Tooling
Pick a static site generator that suits your team (examples: MkDocs, Docusaurus, Sphinx, Hugo, Jekyll). What matters is that it:
- Builds from plain text files, locally and in CI.
- Provides search, navigation, and versioning.
- Can fail the build on broken references.
- Renders diagrams and code with syntax highlighting.
- Produces output you can host cheaply and securely.
Resist the temptation to evaluate tools for weeks. The content and the process matter more.
4. Docs in the Pull Request Workflow
The most effective habit is documentation changes ship in the same pull request as the code change. The reviewer sees both, and the docs cannot lag behind.
Add a prompt to the pull request template:
1
2
3
4
5
6
7
8
## Documentation
- [ ] No documentation impact (explain why)
- [ ] API contract updated (`docs/api/openapi.yaml`)
- [ ] Code docstrings updated
- [ ] Guides, reference, or user docs updated
- [ ] New or changed decision recorded as an ADR
- [ ] Release notes entry added
Use ownership rules so that the right people review:
1
2
3
4
5
# CODEOWNERS (illustrative)
/docs/api/ @signaldesk/api-team
/docs/architecture/ @signaldesk/tech-leads
/docs/user/ @signaldesk/product
/docs/_meta/ @signaldesk/docs-maintainers
Reviewers check documentation for the same things as code: correct, clear, necessary, tested.
5. Automated Checks
Treat the documentation pipeline as another test stage. Fast, deterministic checks run on every pull request.
| Check | Catches | Typical tool type |
|---|---|---|
| Site build with warnings as errors | Broken includes, bad navigation, missing pages | The generator itself |
| Link check (internal) | Dead links and anchors | Link checker |
| External link check | Rotted external links (run on a schedule, not on every PR) | Link checker |
| Markdown lint | Inconsistent formatting | Markdown linter |
| Prose lint | Banned terms, passive voice, glossary violations | Style linter with custom rules |
| Spell check | Typos (with a project dictionary) | Spell checker |
| Spec validation | Invalid or inconsistent OpenAPI | Spec linter |
| Example tests | Code samples that no longer run | Doctests, snippet runners |
| Command checks | Commands in docs that no longer exist | Scripted runs in a clean environment |
| Front matter check | Pages missing an owner or review date (lesson 10) | Small script |
A local entry point keeps developers and CI aligned:
1
2
3
4
5
6
7
docs-check:
markdownlint docs/ README.md
vale docs/
redocly lint docs/api/openapi.yaml
python -m pytest docs/examples --quiet
mkdocs build --strict
lychee --offline docs/
The names above are examples; use the tools your ecosystem provides. The pipeline stages should mirror the ideas from the quality course: fast checks on every commit, slower ones on a schedule, and a clear failure message.
Test your documentation examples. An example that is never executed will break silently. Put runnable snippets in files that tests run, and include them in the documentation from there, instead of copying them into pages by hand.
1
2
3
4
5
# docs/examples/test_quickstart.py
def test_quickstart_import_flow(api_client, sample_csv):
response = api_client.post_import(workspace="demo", file=sample_csv)
assert response.status_code == 202
assert api_client.wait_for(response.json()["id"]).status == "completed"
6. Diagrams as Code
Diagrams drawn in a graphical tool become outdated because nobody can diff them. Text-based diagram notations can be reviewed in pull requests and regenerated.
1
2
3
4
5
6
7
flowchart LR
U[User] --> W[Web app]
W --> A[API]
A --> Q[(Job queue)]
Q --> J[Import worker]
A --> D[(Database)]
J --> D
Guidelines:
- One purpose per diagram; label the arrows with what flows.
- Keep diagrams small; split rather than shrink text.
- Store the source, not only an exported image. If you export images, generate them in the build.
- Always include a short text description for accessibility and for search.
Lesson 6 develops architecture diagrams in detail.
7. Generated and Hand-Written Content
Generate what the system already knows; write what only people know.
| Generate | Source | Write by hand |
|---|---|---|
| API reference | OpenAPI contract | Quickstart, concepts, guides |
| Code reference | Docstrings and type hints | Module overviews, rationale |
| Configuration reference | Config schema or settings class | When to change a setting |
| Error code catalog | Error definitions in code | How to resolve each error |
| CLI help | Command definitions | Task-based guides |
| Dependency list | Lock file | Why a dependency was chosen |
Generated reference stays current by construction. Hand-written content is where review and AI-assisted drafting effort belong.
8. Versioning and Publishing
- Docs version with the software. A reader of version 0.1 should not see instructions for 0.2. Publish a site per released version, plus a “latest” for the main branch.
- Publish automatically from the main branch after checks pass; preview each pull request if your tooling allows it.
- Mark unreleased features clearly, or keep them on the main-branch version only.
- Keep release notes in the repository, updated in the same pull request as the change, and assemble them at release time.
- Never publish secrets or internal-only details to a public site. Separate internal and external documentation deliberately and check what each build includes.
9. Using AI in a Docs-as-Code Workflow
AI fits naturally in the pull request:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Act as a documentation reviewer for a pull request.
Inputs:
- The code diff: [paste]
- The documentation diff (may be empty): [paste]
- Existing documentation pages that mention the changed behavior:
[paste]
- Style guide and glossary: [paste]
Tasks:
1. List the user-visible or developer-visible behavior changes in
the code diff.
2. For each, say whether the documentation diff covers it, and
which existing pages are now outdated.
3. Draft the specific edits needed, marked as suggestions.
4. Flag any change that needs an ADR or a release note entry.
5. Flag documentation edits that make claims the code diff does
not support.
Do not rewrite pages wholesale. Propose minimal edits.
Use AI suggestions as comments for the author, not as automatic commits. An automated bot that rewrites documentation without review reintroduces the fiction problem at scale.
10. Practical Exercise
For SignalDesk (or your project):
- Create the
docs/structure from section 2 and move or link the existing documentation into it. - Set up a site build that fails on warnings, and run it locally.
- Add the pull request documentation checklist and
CODEOWNERSentries. - Add at least five automated checks: build, internal links, Markdown lint, spec validation, and one example test. Wire them to a single
docs-checkcommand and to your pipeline. - Convert one existing diagram to text-based source.
- Configure publishing from the main branch, with a decision about versioned docs.
- Deliberately break a link, an example, and a spec; confirm each break is caught.
- Run the pull request review prompt on a recent real change and evaluate the suggestions.
Verification
You are ready to continue when:
- All documentation lives in the repository with a layout organized by reader.
- Pull requests prompt for documentation impact and route reviews to owners.
- One command runs all documentation checks locally, and CI runs the same command.
- You have seen each category of check fail for the right reason.
- At least one example is executed by tests rather than pasted by hand.
- Publishing is automated, and the versioning approach is decided.
Key Takeaways
- Documentation as code makes accuracy maintainable through review, version control, and automation.
- Ship documentation changes in the same pull request as the code that causes them.
- Test what can be tested: build, links, specs, and especially code examples.
- Generate reference content from sources of truth; hand-write explanation and guidance.
- Use AI to review documentation changes and suggest edits, with people deciding.
What’s Next?
With the infrastructure in place, we begin writing the first major documentation product: the API documentation, where accuracy matters most and readers are least forgiving.