6.3.1 Why CLI and CI/CD Integration Matter
When diagrams live only in Markdown, they are useful for documentation.
When diagrams are rendered automatically in pipelines, they become:
Version-controlled assets
Always up-to-date architecture visuals
Integrating Mermaid into CLI tools and CI/CD pipelines enables:
Automatic SVG/PNG generation
Documentation site builds
Architecture snapshots per release
Zero manual diagram exports
This is where Mermaid transitions from a documentation helper to an engineering workflow tool.
6.3.2 Installing Mermaid CLI
Mermaid provides a command-line interface called mmdc.
Install globally:
Verify installation:
If successful, you can now render diagrams from terminal.
6.3.3 Creating a Mermaid File
Create a file:
Add content:
Save the file.
6.3.4 Rendering to SVG
Generate SVG output:
SVG is recommended because:
6.3.5 Rendering to PNG
PNG is useful for:
Static documentation exports
6.3.6 Using a Configuration File
Create config.json:
Render using config:
This ensures consistent styling across builds.
6.3.7 Batch Rendering Multiple Diagrams
Example directory:
Batch script (Linux/macOS):
Now every .mmd file produces an .svg.
6.3.8 Integrating with GitHub Actions
Example workflow:
This workflow:
Fully automated.
6.3.9 Integrating with GitLab CI
Example .gitlab-ci.yml:
Artifacts store generated diagrams.
6.3.10 Integrating with Documentation Builds
For static site generators:
Example: Pre-build step
This ensures:
Documentation always contains fresh diagrams
No stale visuals in production
6.3.11 Versioning Strategy
Recommended practice:
Store:
.mmd source files in repository
Generated .svg optionally committed
Or generated during build only
Two common strategies:
Strategy A — Commit SVGs
Pros:
Cons:
Strategy B — Generate in CI Only
Pros:
Cons:
Choose based on project scale.
6.3.12 Rendering for Release Snapshots
You can generate diagrams per release version.
Example:
This allows:
Architecture history tracking
Visual diffing between versions
Audit-friendly documentation
For large diagrams:
Split into modular .mmd files
Avoid extremely complex graphs
Render once during build, not per page load
SVG rendering is efficient, but complexity impacts performance.
6.3.14 Security Considerations in CI
If diagrams are generated from:
Ensure:
Controlled execution environment
No arbitrary code injection
Mermaid CLI runs in Node — treat it as executable code.
6.3.15 Real-World Example — Architecture as Code
Project structure:
Workflow:
Developer modifies system.mmd
Commit triggers GitHub Action
Documentation auto-updated
Release contains architecture snapshot
This is Diagram-as-Code fully operationalized.
6.3.16 Hands-On Exercise
Create:
Three .mmd files (flowchart, sequence, class diagram)
Write a shell script to render all files
Add a basic GitHub Action to automate rendering
Goal:
Build a reproducible diagram pipeline.
6.3.17 Common Mistakes
Forgetting to install CLI globally
Incorrect file paths in CI
Using inconsistent theme configuration
Generating diagrams manually instead of automating
Automation eliminates human error.
6.3.18 Best Practices
Store .mmd as source of truth
Use SVG as primary output
Keep themes centralized via config
Version major architecture updates
Think of Mermaid files as code artifacts.
6.3.19 Key Takeaways
CLI and CI/CD integration enables:
Automated diagram generation
Release-based architecture snapshots
Scalable diagram maintenance
Mermaid becomes powerful when:
Automated in build pipelines
At this stage, you have mastered:
You now understand Mermaid not just as a diagramming tool —
but as a fully integrated, version-controlled, automation-ready engineering asset.