Docs-as-Code Pipeline with Previews, Review Gates, and Links
I'll guide you through creating a complete docs-as-code pipeline using GitHub, MkDocs (with Material theme for modern docs), and GitHub Actions. This setup treats your documentation like code:
- Version control: Markdown files in Git.
- Previews: Automatic preview sites for every Pull Request (PR) at
https://YOUR_USERNAME.github.io/YOUR_REPO/preview/pr-123/.
- Review gates: CI checks (build + lint), required PR approvals, and status checks enforced via branch protection.
- Links: PR comments with direct preview URLs; production deploys to root (
/) on merge.
- Production deploy: Automatic build & deploy to GitHub Pages on
main merges.
- Bonus: Markdown linting with Vale for quality gates.
This is GitHub-native (no external services), free, and scalable. Previews persist until manually cleaned (optional cleanup script included).
Prerequisites
- A GitHub repository (e.g.,
yourusername/your-docs-repo).
- Enable GitHub Pages later (Step 4).
- Basic Markdown knowledge.
Step 1: Set Up Your Docs-as-Code Repo
Clone your repo and add these files:
mkdocs.yml (root)
site_name: Your Docs
site_url: https://yourusername.github.io/your-docs-repo/
theme:
name: material
features:
- content.code.copy
- navigation.indexes
- navigation.tabs
- navigation.top
- search.suggest
- search.highlight
nav:
- Home: index.md
- About: about.md
plugins:
- search
markdown_extensions:
- admonition
- pymdownx.details
- pymdownx.superfences
- pymdownx.tabbed:
alternate_style: true
repo_url: https://github.com/yourusername/your-docs-repo
Sample docs
Requirements (requirements.txt)
mkdocs>=1.5
mkdocs-material>=9.0
vale # For linting (install via pip or brew)
Commit and push to main.
Step 2: GitHub Actions Workflows
Create .github/workflows/ folder with these YAML files.
.github/workflows/docs-preview.yml (PR Previews + Links)
name: Docs Preview
on:
pull_request:
types: [opened, synchronize, reopened, closed]
permissions:
contents: read
pull-requests: write # For PR comments
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- run: pip install -r requirements.txt
- name: Lint with Vale
uses: errata-ai/vale-action@v1.15.0
with:
files: '["docs/"]'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Optional: auto-fixes
preview:
runs-on: ubuntu-latest
needs: lint
if: github.event.pull_request.state == 'open'
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- run: pip install -r requirements.txt
- run: mkdocs build --strict
- name: Deploy Preview to GitHub Pages
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./site
publish_branch: gh-pages
destination_dir: preview/pr-${{ github.event.number }}
user_name: 'github-actions[bot]'
user_email: 'github-actions[bot]@users.noreply.github.com'
commit_message: 'Preview for PR #${{ github.event.number }}: ${{ github.event.pull_request.title }}'
- name: Comment Preview Link on PR
uses: actions/github-script@v7
with:
script: |
const previewUrl = `https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/preview/pr-${{ github.event.number }}/`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `🔍 **Preview ready!** [View docs preview](${previewUrl})\n\n${{ github.event.pull_request.title }}`
});
cleanup:
runs-on: ubuntu-latest
if: github.event.pull_request.state == 'closed'
steps:
- name: Cleanup Preview
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_branch: gh-pages
publish_dir: . # Deploy empty to delete dir
destination_dir: preview/pr-${{ github.event.number }}
allow_empty: true
force_orphan: true
user_name: 'github-actions[bot]'
user_email: 'github-actions[bot]@users.noreply.github.com'
commit_message: 'Cleanup PR #${{ github.event.number }}'
.github/workflows/docs-deploy.yml (Production Deploy)
name: Docs Deploy
on:
push:
branches: [main]
permissions:
contents: write
pages: write
id-token: write
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- run: pip install -r requirements.txt
- run: mkdocs build --strict
- name: Deploy to GitHub Pages (Production)
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./site
publish_branch: gh-pages
# No destination_dir = deploys to ROOT (/)
user_name: 'github-actions[bot]'
user_email: 'github-actions[bot]@users.noreply.github.com'
commit_message: 'Deploy main: ${{ github.sha }}'
Step 3: Test the Pipeline
- Create a branch:
git checkout -b feature/add-page
- Add
docs/new-page.md, commit, open PR to main.
- Watch Actions tab:
- Lint passes.
- Preview deploys.
- PR gets comment with link:
https://yourusername.github.io/your-docs-repo/preview/pr-123/
- Make changes → "synchronize" → auto-updates preview.
Step 4: Enable GitHub Pages + Links
- Repo Settings > Pages > Source: "Deploy from a branch" > Branch:
gh-pages / / (root).
- Visit
https://yourusername.github.io/your-docs-repo/ (prod after first merge).
- Previews:
https://yourusername.github.io/your-docs-repo/preview/pr-123/ (auto-linked in PR).
Step 5: Review Gates (Branch Protection)
Enforce quality:
- Repo Settings > Branches > Add rule for
main:
- Require a pull request before merging: ✅
- Require approvals: 1+ (or more)
- Require status checks to pass before merging:
docs-preview / lint
docs-preview / preview
- Restrict pushes (optional).
- Now, PRs cannot merge without:
- Lint passing.
- Preview building.
- Approvals.
Enhancements
Troubleshooting
- Build fails? Check
mkdocs build --strict locally.
- No preview link? Verify GITHUB_TOKEN permissions.
- 404 on preview? Wait 1-2 min for Pages propagation.
This pipeline is production-ready! Merge a PR to deploy prod. Ping me for tweaks. 🕵️♂️