How to Automatically Deploy Your MkDocs Site to GitHub Pages (The Easy Way!)

Published on June 6, 2025

If you're tired of manually building and deploying your documentation site every time you make a change, this post is for you. I'm going to show you how to set up automatic deployment of your MkDocs site to GitHub Pages using GitHub Actions – and trust me, once you have this working, you'll wonder why you ever did it manually.

Why Automate Your Documentation Deployment?

Let's be honest: documentation is already hard enough to maintain without having to worry about the deployment process. Every time you fix a typo, add a new section, or update your project details, you shouldn't have to go through a manual build-and-deploy dance.

With GitHub Actions, you can push your changes to your repository and watch your documentation site update automatically. It's like having a personal assistant for your docs – one that never forgets to deploy and never makes mistakes.

What You'll Need

Before we dive in, make sure you have:

  • A GitHub repository with your MkDocs project
  • An mkdocs.yml configuration file in your repo root
  • GitHub Pages enabled for your repository (we'll cover this step)

That's it! No complex server setups, no third-party deployment services – just GitHub doing what it does best.

Setting Up Your MkDocs Configuration

First, let's make sure your mkdocs.yml file is properly configured. This is crucial because it tells MkDocs where your site will live:

site_name: My Project Documentation
site_url: https://<your-username>.github.io/<your-repo>/
site_description: Description of your project
edit_uri: edit/main/docs/

# I highly recommend using a modern theme
theme:
  name: material  # Trust me, it looks great!

Pro tip: Replace <your-username> with your actual GitHub username and <your-repo> with your repository name. The edit_uri setting adds those handy "Edit this page" links to your docs.

The Magic: GitHub Actions Workflow

Here's where the automation magic happens. Create a new file at .github/workflows/deploy.yml in your repository:

name: Deploy MkDocs to GitHub Pages

on:
  push:
    branches: [ main ]
  workflow_dispatch:  # This lets you trigger builds manually

permissions:
  contents: read
  pages: write
  id-token: write

# Prevent multiple deployments from running simultaneously
concurrency:
  group: "pages"
  cancel-in-progress: false

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0  # We need the full history for git info

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.x'

      - name: Cache pip dependencies
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-

      - name: Install MkDocs and dependencies
        run: |
          pip install --upgrade pip
          pip install mkdocs mkdocs-material
          # Uncomment the next line if you have a requirements.txt file:
          # pip install -r requirements.txt

      - name: Build MkDocs site
        run: mkdocs build --clean

      - name: Setup Pages
        uses: actions/configure-pages@v4

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: ./site

  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

This workflow does several smart things:

  • Only runs when you push to your main branch
  • Caches dependencies to speed up builds
  • Uses the latest GitHub Actions for Pages deployment
  • Includes proper permissions and concurrency controls

Configuring GitHub Pages

Now let's tell GitHub where to find your site:

  1. Navigate to your repository on GitHub
  2. Click SettingsPages
  3. Under Source, select GitHub Actions (not the old branch method)
  4. Save your settings

Next, you'll want to configure deployment protection:

  1. Go to SettingsEnvironmentsgithub-pages
  2. Under "Deployment branches," choose your preference:
    • All branches if you want flexibility
    • Protected branches only if you want more control
    • Selected branches to specify exactly which branches can deploy

Deployment Time!

Ready for the moment of truth? Push your changes:

git add .
git commit -m "Add automated MkDocs deployment"
git push origin main

Head over to the Actions tab in your repository and watch the magic happen. You'll see your workflow running, building your site, and deploying it to GitHub Pages.

Your New Documentation Home

Once the deployment completes (usually takes 2-3 minutes), your documentation will be live at:

https://your-username.github.io/your-repo/

Level Up Your Setup

Managing Dependencies Like a Pro

If you're using additional MkDocs plugins or themes, create a requirements.txt file:

mkdocs>=1.5.0
mkdocs-material>=9.0.0
mkdocs-mermaid2-plugin
# Add whatever plugins make your docs awesome

Then update your workflow to use it by uncommenting the requirements installation line.

Custom Domain? No Problem!

Want to use your own domain? Just add a CNAME file to your docs/ directory with your domain name:

docs.myawesomeproject.com

Branch Protection for Quality Control

Consider protecting your main branch to require pull request reviews. This ensures that documentation changes go through the same quality gates as your code.

When Things Go Wrong

Don't panic if something doesn't work immediately. Here are the most common issues:

  • Build failures: Check the Actions logs – they're incredibly detailed
  • Site not updating: Make sure you're pushing to the right branch
  • 404 errors: Double-check your site_url in mkdocs.yml
  • Permission issues: Verify that GitHub Actions has the necessary permissions

Wrapping Up

Setting up automated deployment for your MkDocs site might seem like overkill at first, but trust me – once you experience the joy of push-to-deploy documentation, you'll never go back. Your future self will thank you for the time saved, and your team will appreciate always having up-to-date docs.

The best part? This setup scales with your project. Whether you're documenting a small utility or a complex enterprise application, this workflow will handle it reliably.

Now go forth and document with confidence, knowing that every commit brings your users closer to better documentation!


Have questions about this setup or run into issues? Feel free to reach out or check the GitHub Actions documentation for more advanced configurations.