Installing and Deploying MkDocs Material with GitHub Pages
This guide explains how to install and deploy MkDocs Material for creating beautiful documentation sites. It also includes steps to automate deployment to GitHub Pages.
What's Included in the Site
The provided MkDocs Material configuration is designed to support a comprehensive documentation site with the following features:
-
Navigation:
- Multi-level navigation with tabs and collapsible sections.
- Support for documentation, guides, blog posts, API references, and release notes.
-
Customization:
- Custom logo and favicon.
- Light and dark themes with a toggle switch.
- Customizable color palettes to match your branding.
-
Enhanced User Experience:
- Instant navigation with prefetching for fast transitions.
- Sticky top navigation for ease of access.
- Copy button for code snippets to improve developer productivity.
-
Plugins:
awesome-pages: Automatically organizes navigation based on folder structure.minify: Optimizes site assets for faster loading.blog: Supports structured and visually appealing blog posts.search: Integrated full-text search for quick access to content.
-
Content Types:
- Documentation sections for getting started, installation guides, and tutorials.
- Blog posts with structured navigation and metadata.
-
Technical Enhancements:
- Cookie consent settings to comply with privacy policies.
- CSP (Content Security Policy) meta tag for enhanced security.
- Custom CSS and JavaScript to further tailor the site's appearance and functionality.
Repository Features
The repository includes:
- A complete
mkdocs.ymlconfiguration with navigation, theme, and plugin settings. - A
docs/directory with pre-structured files and folders for easy customization. - Workflow configuration (
.github/workflows/deploy.yml) to automate deployment to GitHub Pages. - Sample blog posts, API documentation, and guides to demonstrate how to structure content.
- Custom assets for branding, including a logo and favicon.
By cloning this repository, you can start with a fully functional MkDocs Material site and focus on adding your content instead of setting up the structure.
Step 1: Prerequisites
Before starting, ensure you have the following:
-
Python 3.x installed (verify with
python --version). -
pip installed (verify with
pip --version). -
A GitHub repository to host your documentation.
-
Git configured locally with a GitHub personal access token if needed.
Step 2: Install MkDocs Material
-
Create a Virtual Environment (optional but recommended):
python -m venv venvsource venv/bin/activate # On Windows, use venv\Scripts\activate -
Install MkDocs Material:
pip install mkdocs-material -
Verify Installation:
mkdocs --version
Step 3: Create the MkDocs Project
-
Create a New MkDocs Project:
mkdocs new my-projectcd my-project -
Edit the
mkdocs.ymlFile: Replace the default configuration with the following:site_name: Your Site Namesite_url: Your Site URLtheme:name: materiallogo: assets/your_logo.pngfavicon: assets/your_logo.pngcustom_dir: overridespalette:- media: "(prefers-color-scheme: light)"primary: blue greyaccent: amberscheme: defaulttoggle:icon: material/weather-nightname: Switch to dark mode- media: "(prefers-color-scheme: dark)"primary: blue greyaccent: amberscheme: slatetoggle:icon: material/weather-sunnyname: Switch to light modefeatures:- content.code.copy- navigation.instant- navigation.tabs- navigation.path- navigation.top- navigation.footer- header.autohide# example of navigationnav:- Home: index.md- Liberty:- Getting Started: liberty/getting-started.md- Installation:- Architecture: liberty/technical/architecture.md- Docker Installation Guide: liberty/technical/installation.md- Installation Tools Deployment Guide: liberty/technical/tools-deployment.md- Liberty Deployment Guide: liberty/technical/liberty-deployment.md- Create Linux Services: liberty/technical/linux-services.md- Enable SSL with Traefik: liberty/technical/post-ssl.md- Blog:- blog/index.mdplugins:- search- awesome-pages- minify- blogextra:social:- icon: fontawesome/brands/githublink: your social link for github- icon: fontawesome/brands/linkedinlink: your social link for linkedinmeta:- name: Content-Security-Policyvalue: frame-ancestors 'self' https://giscus.app;consent:title: Cookie consentactions:- accept- manage- rejectdescription: >-We use cookies to recognize your repeated visits and preferences,as well as to measure the effectiveness of our documentation.markdown_extensions:- attr_list- pymdownx.highlight:anchor_linenums: truelinenums: trueline_spans: __spanpygments_lang_class: true- pymdownx.inlinehilite- pymdownx.snippets- pymdownx.superfencesextra_css:- css/custom.cssextra_javascript:- js/extra.jscopyright: >Copyright © 2024 Nomana-IT –<a href="#__consent">Change cookie settings</a> -
Add Your Documentation Files: Organize your files under the
docs/folder as per the navigation structure defined in themkdocs.yml.
Step 4: Deploy to GitHub Pages
-
Set Up GitHub Actions: Add the following configuration in
.github/workflows/deploy.yml:name: cion:push:branches:- master- mainpermissions:contents: writejobs:deploy:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v4- name: Configure Git Credentialsrun: |git config user.name github-actions[bot]git config user.email 41898282+github-actions[bot]@users.noreply.github.com- uses: actions/setup-python@v5with:python-version: 3.x- run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV- uses: actions/cache@v4with:key: mkdocs-material-${{ env.cache_id }}path: .cacherestore-keys: |mkdocs-material-- run: pip install mkdocs-material mkdocs-awesome-pages-plugin mkdocs-minify-plugin- run: mkdocs gh-deploy --force -
Push Your Changes: Commit and push your project to the
mainbranch of your GitHub repository:git add .git commit -m "Initial documentation setup"git push origin main -
Access Your Site: After GitHub Actions finish deploying, your site will be live at:
https://<your-github-username>.github.io/<repository-name>/
Conclusion
Your MkDocs Material documentation is now installed and deployed with GitHub Pages! This workflow ensures automated deployment and a professional look for your documentation site.
