MkDocs github/ gh-pages deploy trick
Disclaimer:
mkdocs builddoesn't allow the build of the/sitewithin the same directory, hence the traditional gh-pages deployment settings will fail and it won't deploy your site. (if its a org or user based pages)Hence the follwoing trick should work.
If the output of your MkDocs build is in the /site folder, you need to push the contents of this folder to the gh-pages branch of your GitHub repository. However, you should not push the /site the directory itself but its contents. Here's how you can do it:
Ensure Your Local Repository is Updated:
Make sure your local repository is up to date with the main branch.
Build your MkDocs site using
mkdocs build. This will update the/sitedirectory.
Move to the
/siteDirectory:Navigate to the
/sitedirectory in your local repository.
Create a Temporary Git Repository:
Since the
/sitethe directory is not a Git repository, you need to initialize it temporarily:cd site git init
Commit the Changes:
Add and commit the changes in the
/sitedirectory:git add . git commit -m "Deploy MkDocs Site"
Push to
gh-pagesBranch:Push the contents of the
/sitedirectory to thegh-pagesbranch of your remote repository. Replaceyour-usernamewith your GitHub username.git push -f https://github.com/mumbaipy/website.git master:gh-pages
Clean Up:
Once you've successfully pushed to
gh-pages, you can delete the temporary Git repository in the/sitedirectory:rm -rf .git
Configure GitHub Pages:
Go to your repository (
https://github.com/mumbaipy/website).Click on 'Settings' and scroll down to the 'GitHub Pages' section.
Set the source to the
gh-pagesbranch.
Access Your Site:
Your MkDocs site should now be available at
https://mumbaipy.github.io/website/.
This process ensures that the built HTML files in your /site directory are pushed to the gh-pages branch, allowing GitHub Pages to host your MkDocs site. Remember to repeat these steps every time you update your site.
Last updated
Was this helpful?