46
loading...
This website collects cookies to deliver better user experience
mkdocs
as static site generator, so let's install it:pip install mkdocs
mkdocs new GITHUB_USERNAME
cd GITHUB_USERNAME
./
-- docs/
-- mkdocs.yml
mkdocs serve
then open http://127.0.0.1:8000 in your browser. And voilà! You have a personal documentation local website. Any .md
file added in docs/
will appear as a new web page. Try to edit docs/index.md
for example.mkdocs.yml
contains your project configuration. You can customize your site name, navigation layout and so on. A simple example would be:site_name: your site name
site_url: https://GITHUB_USERNAME.github.io
site_author: you
site_description: a short description of your website
nav:
- About: index.md
theme:
name: readthedocs
highlightjs: true
hljs_languages:
- yaml
mkdocs
makes it easy to push your project build files to your GitHub page through the mkdocs gh-deploy
command. mkdocs gh-deploy
is that it wipes out all the files in GITHUB_USERNAME.github.io/
to generate the website files (html, css, js).mkdocs gh-deploy
from within the docs source project itself.mkdocs
project previously created)
Note that a repository named as your username is a special repository on GitHub. Its README.md
will appear on your public profile. So you can customize that as well.
Git repo | Description |
---|---|
GITHUB_USERNAME.git (or anything else you chose to name it) | Source repo with markdown files |
GITHUB_USERNAME.github.io.git | GitHub page repo with static web files |
git init
echo site/ > .gitignore
git add .
git commit -m "first commit"
git branch -M main
git remote add origin SOURCE_REPO # replace SOURCE_REPO with your source repo HTTPS or SSH adddress
git push -u origin main
# still in your source folder
mkdir GITHUB_USERNAME.github.io
cd GITHUB_USERNAME.github.io
git init
git commit --allow-empty -m "first commit"
git branch -M main
git remote add origin GH_PAGE_REPO # replace GH_PAGE_REPO with your source repo HTTPS or SSH adddress
git push -u origin main
cd ..
GITHUB_USERNAME/ Source repo
-- GITHUB_USERNAME.github.io/ GitHub page repo
-- docs/
-- .gitignore
-- mkdocs.yml
GITHUB_USERNAME.github.io/
since there's no need for it to exist independently:rm -rvf ./GITHUB_USERNAME.github.io
git submodule add -b main GH_PAGE_REPO # replace GH_PAGE_REPO with your source repo HTTPS or SSH adddress
git add GITHUB_USERNAME.github.io
git commit -m "add github page submodule"
git push
cd GITHUB_USERNAME.github.io
mkdocs gh-deploy --config-file ../mkdocs.yml --remote-branch main
cd ..
Note that you might have to wait a few seconds for your changes to be deployed.
mkdocs gh-deploy
command in itself isn't very user friendly nor easy to remember so we'll add a simple Makefile
to make (word choice on purpose) it easier to deploy changes.Makefile
in your source folder with the following:SHELL := /bin/bash
GH_PAGE := GITHUB_USERNAME.github.io
.PHONY: deploy
deploy:
cd $(GH_PAGE) && mkdocs gh-deploy --config-file ../mkdocs.yml --remote-branch main
Ensure to paste Makefile
indentation as Tabs or else you might get an error in the lines of [...] *** missing separator. Stop.
make deploy
whenever you want to publish something.Makefile
:.PHONY: update-build-version
update-build-version:
git submodule update --remote --merge
git add $(GH_PAGE)
git commit -m "ci: update build version"
.PHONY: publish
publish: deploy update-build-version
git push
make publish
will handle the deploy and update your submodule version.mkdocs.yml
git commit -am "commit message"
git push
to your source repomake publish
to your GitHub pagemkdocs build
and use site/
as my GitHub page?site/
artifacts, or you could even use a different branch for the GitHub page altogether. IMHO I'd much rather track the actual source code and deploy the minimal amount of files in the public project, and having two completely unrelated branches in the same repository is kind of an overly obscure solution. Hence the submodule approach. But that is my biased opinion, use whichever solution works best for you!