Diagrams as code
Make diagrams from the files they describe (Terraform, OpenAPI, GraphQL, Protocol Buffers, Docker Compose, Dockerfiles, Kubernetes, Helm, Kustomize, nginx, the AWS CDK, Serverless Framework services, Backstage catalogs, Structurizr workspaces, AsyncAPI documents, Prisma, Drizzle, SQL, dbt projects, JavaScript workspaces, GitHub Actions, GitLab CI/CD, and Azure Pipelines pipelines, CSV and JSON, folder listings, and git logs) and keep them in step: a pull request whose diagrams no longer match fails, and a workflow writes them again. Hand changes live in an overrides file, so they survive every run. It is for anyone who keeps architecture or schema diagrams in a repository. A working repository to copy from is plotwork-official/plotwork-examples (opens in a new tab).
- GitHub Actions
- Azure Pipelines
- GitLab
TL;DR
| Question | Answer |
|---|---|
| Where do I say what to draw? | In sources in plotwork.config.json: for each diagram, the files (from), their format, and where it goes (out), a .diagram file or a fence of a Markdown file. |
| What makes the diagrams? | npx @buun_group/plotwork generate on your machine, or the GitHub Action with command: generate. Both run the same importers as the import_source tool, locally. |
| Does it work outside GitHub? | Yes. The Azure Pipelines task and the GitLab CI/CD component run the same drift and generate. The Azure task opens the pull request in Azure Repos; the GitLab job keeps a patch. |
| How do I change a generated diagram? | In its overrides file (infra.overrides.json beside infra.diagram): labels, icons, tones, types, groups, hidden boxes, extra edges and lines, header options. Never in the generated file, which the next run writes over. |
| I already edited the file by hand. | Run plotwork generate --capture: it writes your edits into the overrides file, then makes the diagram again. A check names a hand edit apart from drift, and the Action's write mode keeps it for you. |
| What fails a pull request? | A diagram that differs by meaning from what its files make now. The check names what changed: nodes and edges added, removed, and changed. A diagram that means the same and whose pictures alone differ, such as pictures another version of Plotwork drew, passes with a notice. |
| How are the diagrams written back? | The Action in write mode opens or edits one pull request (the default), or commits to the branch with on-drift: commit. |
| Can it publish too? | Yes, with publish: true and an API key in a secret: a stable link for a personal key, or the organisation library for an organisation key. |
| Is there a whole example? | plotwork-examples (opens in a new tab): a small company's Terraform, OpenAPI, Compose, Kubernetes, SQL, workflow, and CSV files, with a source for each, an overrides file, a Markdown fence, and the check, regenerate, and smoke workflows. |
| Will the pictures look right on GitHub in dark mode? | Yes. By default each diagram gets one SVG that follows the reader's light or dark setting, so a plain link or <img> shows it right, and the repository holds about 43% fewer bytes of pictures than with two files. "scheme": "light-and-dark" writes a light and a dark file instead. |
| Does anything leave the machine? | Not by default. Only publishing sends anything, and then only the generated diagram source, never the files it was made from. |
1. Name the sources
Each entry of sources is one diagram. Paths start at the folder that holds the settings file.
{ "$schema": "https://docs.plotwork.dev/schemas/plotwork.config.json", "sources": [ { "name": "infra", "from": "infra/**/*.tf", "format": "terraform", "out": "docs/infra.diagram" }, { "name": "api", "from": "openapi.yaml", "format": "openapi", "view": "map", "out": "docs/api.md#endpoints" }, { "name": "services", "from": [ "compose.yaml", "compose.override.yaml" ], "format": "compose", "out": "docs/architecture.md#services" }, { "name": "schema", "from": "prisma/schema.prisma", "format": "prisma", "out": "docs/schema.diagram", "options": { "tables": [ "User", "Org*" ] } } ]}| Field | Meaning |
|---|---|
from | A file, a folder, or a glob, or a list of them. One that starts with ! leaves out what it matches. .git, node_modules, and .terraform are never read. |
format | sql, prisma, drizzle, openapi, graphql, protobuf, terraform, terraform-plan, bicep, arm, pulumi, cloudformation, cdk, serverless, compose, dockerfile, kubernetes, helm, kustomize, nginx, backstage, structurizr, asyncapi, github-actions, gitlab-ci, azure-pipelines, dbt, workspaces, csv, json, tree, or git-log, as Importing from code and data describes. |
out | A .diagram file, or a Markdown file and a fence id: docs/architecture.md#services. |
name | A short name for --source and the summary. The out path when left out. |
view, title | The picture of a format drawn more than one way (endpoints, map, sequence for OpenAPI; git, timeline for a git log; context, container, component for a Structurizr workspace), and the title. |
options | The importer's options: tables, paths, tags, operationId, chart, groupBy (owner for a Backstage catalog), varFiles, and root, the root module or CDK stack of several to draw (envs/prod), each a diagram of its own when left out, or the system or container a Structurizr view is about. |
overrides | The overrides file. Left out: infra.diagram has infra.overrides.json, and the fence services of docs/architecture.md has docs/architecture.services.overrides.json. |
pictures | For a .diagram out, also write its SVG beside it (one, or a light and a dark one, by scheme). On when left out. |
scheme | auto writes one infra.svg that holds both schemes; light or dark writes one infra.svg in that scheme; light-and-dark writes infra.svg and infra.dark.svg. defaults.scheme, then auto, when left out. For a Markdown out it applies to every fence of that file. |
publish | Where the Action publishes it (section 6). |
Two sources that write one file, or a view the format does not draw, are S101 on their line of the settings file. A from that matches no file is S103.
A Markdown out needs a fence that says where the diagram goes. It may be empty; the first run fills it and writes the picture block under it, as sync does:
## Services
```diagram id=services```A git-log source reads a file. Write it in an earlier step, such as git log --oneline --graph -n 40 > docs/history.txt, or the check fails on every new commit.
One picture for both themes
By default ("scheme": "auto"), each diagram gets one SVG that holds both schemes and switches inside the file with a prefers-color-scheme media query, so the file itself, a link to it, and a plain  or <img> all show the scheme the reader uses. The two schemes are painted from one layout, so the picture is the same as two files would be. It is also smaller: on the twenty example diagrams of the engine, one SVG each is 506 KB against 894 KB for a light and a dark file, 43% less (46% less compressed), and half the files.
auto (the default) | light-and-dark | |
|---|---|---|
| Files per diagram | One infra.svg | infra.svg and infra.dark.svg |
| Size on the example diagrams | 506 KB | 894 KB |
A plain link or <img> in dark mode | Dark | Light (white cards) |
| What picks the scheme | The reader's device or browser setting | GitHub's own theme setting, through a <picture> element |
| A Markdown fence's picture block | One <img> | A <picture> with a dark <source> |
The one difference: an SVG follows the device or browser setting, not GitHub's. A reader who sets GitHub to dark and the system to light sees the light scheme. The two-file <picture> form follows GitHub's own theme setting, so set "scheme": "light-and-dark" in defaults, or on one source, when that matters more than the size. sync reads the same setting for the fences it draws.
Moving from two files to one
A repository that wrote two files before, with light-and-dark set or with an earlier release whose default it was, moves to one SVG on its next run once scheme is unset or auto:
- Run
plotwork generateandplotwork sync(or the Action withmode: writeandcommand: sync). Each picture is drawn again as one SVG, each picture block becomes one<img>, and each.dark.svgthat Plotwork drew beside it is removed. The output names every file it removed, and the Action's job summary lists them under Removed. - Commit the changed and removed files together. A check that runs before that commit passes with a notice that the pictures were drawn again, since the diagrams mean the same; after it, the check passes on the one picture and does not ask for the removed dark files.
A dark file is removed only when Plotwork provably drew it: its name is the stem of the diagram's own picture with .dark.svg, and the file is an SVG the renderer wrote, with its dark theme, its accessible title and description ids, and its title first. A file under that name that someone made by hand is never touched. A diagram whose two schemes cannot share one file keeps both files.
2. Generate
npx @buun_group/plotwork generate| Command | What it does |
|---|---|
generate | Makes every diagram and writes the ones that changed: the .diagram file and its SVGs, or the fence and its picture block. |
generate --check | Writes nothing. Exits 1 when a diagram differs from what its files make, and prints what changed. |
generate --source infra | One source only. |
generate --capture | First writes the hand edits of each generated diagram into its overrides file. |
generate --format github | Each problem and each drift as a GitHub annotation. --format json prints the plan, --format sarif a SARIF report for code scanning, and --format junit a JUnit report. |
generate --since origin/main | Only the sources whose files, overrides, or diagram changed since the ref, and every source when the settings file changed. |
A drifted diagram prints its changes by meaning, from the same comparison as compare_diagrams: reordered or reformatted lines are no change, and a renamed id is a rename.
drift: docs/infra.diagram (infra, terraform from infra/**/*.tf) Added 1 node (logs). Removed 1 edge (web -> db). + node logs - edge web -> dbfailed: 4 diagram(s) checked, 1 drifted, 0 error(s)The output is the same for the same files on every machine. A generated file starts with a comment that names its source and its overrides file, and the fingerprint of the diagram written under it:
# Generated by plotwork generate from the source infra (terraform: infra/**/*.tf).# The next run writes over this file. Put changes in docs/infra.overrides.json, or run plotwork generate --capture# after an edit to keep it there, then run plotwork generate.# Fingerprint: 3ffbc927b9838395The fingerprint is a hash of the diagram under the comment, the same on every machine. A Markdown fence carries the same comment inside it. It is how a check tells a hand edit from a change of the files the diagram is made from; see Keep a hand edit.
3. Overrides
An overrides file is JSON beside the diagram. It is applied after every import, so a change to the Terraform and a change to a label never fight.
{ "$schema": "https://docs.plotwork.dev/schemas/plotwork.overrides.json", "header": { "options": { "direction": "right", "legend": "off" } }, "items": { "aws_s3_bucket.assets": { "label": "Assets bucket", "tone": "orange" }, "aws_instance.web": { "group": "compute", "type": "function" }, "aws_internet_gateway.gw": { "hide": true } }, "groups": { "compute": { "label": "Compute", "type": "app-tier", "parent": "aws_vpc.main" } }, "edges": [ { "from": "aws_instance.web", "to": "aws_lb.web", "label": "health", "arrow": "-->" }, { "from": "aws_db_instance.orders", "to": "aws_db_subnet_group.orders", "hide": true } ], "lines": [ { "statement": "node runbook step \"Runbook\" sub=\"Rotated nightly\"", "into": "compute" } ]}| Section | What it changes |
|---|---|
header | title, and header options such as direction, icons, legend, or desc. null takes an option away. |
items | For each box, group, or table, by name: label, sub, icon, tone, type (a node type, or a group's type), group (move it into a group; null is the top level), other options, and hide. |
groups | Groups to add, by the id they get: label, type, sub, icon, parent. Move items into them with group. |
edges | Edges to add (from, to, label, arrow, options), or with hide, the import's edges from one end to the other to take away. |
lines | Any other line, such as a note box, as it is, optionally into a group. |
How a thing is named
An entry names a thing by the name the file gives it, which does not change when the file grows. The id in the diagram can: two resources named web become web and web_2 in the order they are read.
| Format | Name | Example |
|---|---|---|
terraform, terraform-plan | The resource address, after its module and with its copy's key, the same for a plan as for the files | aws_s3_bucket.logs, module.network.azurerm_subnet.this["app"] |
bicep, arm | The symbolic name, after its module | module.data.vault, vnet/subnets/app, queue['orders'] |
pulumi | The logical name, or the URN when two share one | labels |
cloudformation | The logical id | OrdersTable |
cdk | The construct path, without the Resource that wraps it | OrdersApi/Ordering/Handler, OrdersApi/Ordering |
compose | service:, network:, or volume: and its name | service:api |
kubernetes, helm, kustomize (built) | Namespace, kind, and name; a Helm release: or chart: frame | payments/Deployment/api, release:shop |
kustomize (a folder) | frame:, kustomization:, file:, patch:, or generator: and its path | file:base/deployment.yaml |
backstage | The entity reference | component:default/orders-api |
structurizr | The DSL identifier, its path with hierarchical identifiers, or the path of names; group: and its path | banking.api, group:banking/Payments |
asyncapi | app: and the title, channel: and the address, broker: and the protocol | channel:orders.placed.v1 |
github-actions | job: and the job id; on is the trigger | job:test |
gitlab-ci | job:, stage:, or trigger: and the name | job:unit-tests, stage:deploy |
azure-pipelines | stage: and its name; job: and Stage.Job, or the job alone in a file of only jobs | job:Build.UnitTests |
sql, prisma, drizzle | The table name | users |
graphql | type: and the type name | type:Order |
protobuf | message:, enum:, or service: and the full name | message:shop.v1.Order |
serverless | function: and the function name; the logical id for every other resource | function:createOrder, ParcelsTable |
dockerfile | stage: and the stage name or number, after the file when there are several | stage:build, stage:api/Dockerfile:runtime |
nginx | server:, upstream:, address:, static:, or redirect: and what names it | upstream:http:storefront |
dbt | The dbt unique id | model.jaffle_shop.orders |
workspaces | package: and the package name | package:@acme/ui |
| The others | The id in the diagram | orders |
The id in the diagram works for every format too, when it is stable enough for you.
When the importer changes
A new version of the importer can draw the same files differently: a Terraform stack whose local modules are now framed, or whose for_each copies are now drawn one by one. A stack the new rules have nothing to say about (no module, no for_each or count) is drawn exactly as before, byte for byte. When an upgrade changes what a diagram says, generate --check reports it as drift; run plotwork generate once, review the change, and commit it.
When only the pictures differ
A newer engine can draw the same diagram with other pictures: a spacing, a colour, or an icon's art. When the .diagram text (or the fence) means the same and only the pictures, or the Markdown around a fence, differ, generate --check passes and says so in a notice: "The pictures were drawn by another version of Plotwork; the meaning is the same. Run generate in write mode to refresh them." It is no drift in any report: the JSON report gives the source status: "in-step" with redrawn: true, JUnit passes the test case with the notice in its output, SARIF has no result for it, and the drift dashboard shows it in step. A missing picture, or a statement added, removed, renamed, moved, or changed, is still drift. Write mode writes the new pictures, and the Action commits them with its other changes.
This applies to generated diagrams only. sync --check on a Markdown fence still fails on a picture that differs, since its block hash covers the SVG.
When the file changes
An entry whose thing is gone is a warning, S201, on its line of the overrides file: Stale override for aws_s3_bucket.logs: the import no longer draws it. An entry the diagram cannot take, such as an added group whose id is taken, is S202. Neither fails a run; remove or rename the entry. A change that leaves the diagram with an error, such as an unknown node type, is that error, on its line of the generated file.
Keep a hand edit
npx @buun_group/plotwork generate --capture --source infra--capture compares the file you edited with what the import and the current overrides make, writes each difference into the overrides file (labels, options, types, moves, hidden and added boxes, groups, edges, and lines), and then makes the diagram again, so it comes back the same with its comment on top. A source split into several diagrams is captured part by part, each against its own file, into its one overrides file.
A hand edit is never lost unseen. The fingerprint tells two drifts apart:
| The file | What generate --check and the Action's check mode say |
|---|---|
| No longer matches its own fingerprint | Edited by hand: run plotwork generate --capture to keep the edit, or plotwork generate to discard it. The Action annotates the file with "Diagram edited by hand". |
| Matches its fingerprint, and the files make another diagram | Drift: the files it is made from changed, with what changed by meaning. |
| Has no fingerprint (written before fingerprints) | Drift as before, never an edit. |
plotwork generate without --capture writes over a hand edit and prints a warning that names each file it wrote over; the edit is still in your history. The Action's write mode, with on-drift: pr or commit, captures the hand edits into the overrides files first and commits them with the diagrams, and the pull request and the job summary name each diagram whose edits were kept.
4. Fail a pull request on drift
name: diagrams
on: pull_request:
permissions: contents: read
jobs: drift: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7 with: persist-credentials: false - uses: plotwork-official/plotwork-action@62c17dfd42c3e67e29de0f6d0adbad6b4831136a # v1.9.0 with: command: generateThe samples pin the latest release of the Action by its commit. @v1 becomes available at the first stable release.
Each drifted diagram is an error annotation on its file, a diagram whose pictures alone differ is a notice that fails nothing, and the job summary has a table of every diagram: where it comes from, the nodes and edges added and removed, its status, a link to its file, and the engine's table of what changed. A job summary cannot show a picture from the runner, so the changes are written out; the pull request from write mode, and a published diagram, show the pictures.
5. Write the diagrams back
Open a pull request
On a push to the default branch, or on a schedule, write mode makes the diagrams again and pushes them to one branch with one pull request. Each run replaces that branch with one commit on the latest base and edits the open pull request rather than opening another. Once nothing drifts, the next run closes that pull request with a comment and deletes its branch, when the branch's last commit is the action's own.
name: diagrams-regenerate
on: push: branches: [main] schedule: - cron: '0 4 * * *'
permissions: contents: read
jobs: regenerate: runs-on: ubuntu-latest permissions: contents: write pull-requests: write steps: - uses: actions/checkout@v7 - id: plotwork uses: plotwork-official/plotwork-action@62c17dfd42c3e67e29de0f6d0adbad6b4831136a # v1.9.0 with: command: generate mode: write pr-branch: plotwork/diagrams pr-title: 'docs: regenerate diagrams from their sources'| Setting | Why |
|---|---|
contents: write, pull-requests: write | To push the branch and open the pull request. |
| Settings, Actions, General, "Allow GitHub Actions to create and approve pull requests" | GitHub refuses a pull request from the workflow's token without it. |
github-token | A pull request opened with the workflow's own token starts no workflow, so its checks do not run. Pass a GitHub App or personal token to have them run. |
on-drift: pr refuses a pull_request run: such a run is on a merge commit, not on a branch.
Commit to the branch
on: pull_request:
permissions: contents: read
jobs: regenerate: runs-on: ubuntu-latest permissions: contents: write steps: - uses: actions/checkout@v7 with: ref: ${{ github.head_ref }} - uses: plotwork-official/plotwork-action@62c17dfd42c3e67e29de0f6d0adbad6b4831136a # v1.9.0 with: command: generate mode: write on-drift: commit commit-message: 'docs: regenerate diagrams'The commit carries the trailer Plotwork-Generated: true. A run started by that commit does nothing, so a token that does start workflows never loops. A pull request from a fork cannot be pushed to with the workflow's token. on-drift: fail writes the diagrams in the workspace and fails, for a later step of your own.
6. Publish
With publish: true and a key in PLOTWORK_API_KEY, write mode publishes each source that has a publish section.
{ "name": "infra", "from": "infra/**/*.tf", "format": "terraform", "out": "docs/infra.diagram", "publish": { "organization": "acme", "folder": "Platform/Infra" }}publish field | Meaning |
|---|---|
alias | A stable link of a personal key: one address that always shows the latest version. See Hosted diagrams. |
organization | File it in this organisation's library. Left out with no alias, the organisation of brand.organization is used. An organisation key publishes only to its own organisation, and needs neither: the API names the key's organisation and the diagram goes to its library. An organisation's diagrams have no public address, so an alias makes no stable link there, and the run says so. |
folder, name, scheme | Where it is filed, its name (the title when left out), and light, dark, or auto. |
embedSource | Write the diagram source into the hosted picture. Left out, it is written, unless an organisation turned off Keep diagrams editable; false leaves it out. |
required | Fail the run when it cannot be published, such as with no key. Left out, a skipped publish is a notice. |
name: diagrams-publish
on: push: branches: [main]
permissions: contents: read
jobs: publish: runs-on: ubuntu-latest permissions: contents: write pull-requests: write steps: - uses: actions/checkout@v7 - uses: plotwork-official/plotwork-action@62c17dfd42c3e67e29de0f6d0adbad6b4831136a # v1.9.0 with: command: generate mode: write publish: true env: PLOTWORK_API_KEY: ${{ secrets.PLOTWORK_API_KEY }}The key is masked in the log and never printed. The repository's defaults and brand.organization go with the source, so the hosted picture takes the organisation's brand and icon packs when its plan includes them. The links are in the job summary, with the pictures, and in the published output.
7. Inputs and outputs
| Input | Default | Meaning |
|---|---|---|
command | check | generate for this guide. |
mode | check | check fails on drift and writes nothing; write makes the diagrams again. |
on-drift | pr | In write mode: pr, commit, or fail. |
pr-branch | plotwork/diagrams | The branch pr pushes to. The action owns it. |
pr-title | docs: regenerate diagrams from their sources | The pull request's title. |
commit-message | docs: regenerate diagrams from their sources | The commit message, before the trailer. |
publish | false | Publish the sources that have a publish section, in write mode. |
github-token | ${{ github.token }} | The token that pushes and opens the pull request. |
since | A git ref: only the sources whose files changed since it run. Check out with fetch-depth: 0. | |
sarif-file, junit-file | Files to write a SARIF report (for github/codeql-action/upload-sarif) and a JUnit report to. | |
config, working-directory | As for every command. See Keep diagrams in sync in CI. |
| Output | Meaning |
|---|---|
stale | Diagrams that drifted. |
files | The drifted diagrams in check mode; the files written in write mode. |
changes | A JSON array, one entry per drifted diagram: its name, its file, the number of changes, and the sentence that names them. |
pull-request | The address of the pull request opened or edited, or empty. |
published | A JSON array of each source published: name, kind (personal or organization), url, stableLink, svgUrl (an organisation's picture, served to the key), id, and organization. |
errors, warnings | The problems found. |
8. Limits
| Limit | Value |
|---|---|
| Sources in one settings file | 250, in a file of at most 256 KB; a larger repository gives each team a settings file of its own and runs one job per working-directory |
Patterns in one from | 20 |
| Files one source reads | 2,000, at the local import limits, which only guard memory |
| An overrides file | 128 KB, and 500 entries in each section |
| A source too big for one picture | Written as several .diagram files, infra.diagram, infra.2.diagram and on; a Markdown fence holds one diagram, so such a source needs a .diagram out or fewer things chosen with options |
| Parts a source no longer has | Removed with their pictures, but only a file that carries that source's generated comment |
See also
| Page | Why |
|---|---|
| Importing from code and data | What each format draws, and its options |
| Repository settings | The rest of plotwork.config.json: defaults, rules, and brand |
| Keep diagrams in sync in CI | The other commands of the GitHub Action |
| Diagrams in Azure Pipelines | The drift check and regeneration as the Azure DevOps task PlotworkDiagrams@1 |
| Diagrams in GitLab CI/CD | The same as a GitLab component |
| Hosted diagrams | Links, stable links, and what a plan hosts |
| Organisations | Organisation keys and the shared library |
| Terraform in organisations | Publish diagrams and manage the brand and icons on every apply instead |
| Enterprise rollout | A reusable workflow, required checks, code scanning, many teams in one repository, and air-gapped runners |
| plotwork-examples (opens in a new tab) | A working repository with every format, and the smoke test of each release |