Versioning#
As your project evolves and new features are added, you may be supporting different versions of your code. For this reason, it is a good idea to keep an archive of different versions of your documentation, so your users can refer to it depending on the software version they are using.
Let’s see how you can achieve that.
Using sphinx-multiversion
#
We will be using sphinx-multiversion
, a
popular extension that makes versioning your documentation easy. This will replace the traditional
sphinx-build
command (which was executed under the hood when we ran make html
!).
In your virtual environment, install:
pip install sphinx-multiversion
and add the extension to conf.py
:
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.githubpages",
"sphinx.ext.napoleon",
"sphinx_multiversion",
]
Generating different versions for your docs#
Let’s test this out.
We’ll create a tag, v0.0.1
, for our current version and push it to our remote repository:
git tag -a v0.0.1 -m "first tag"
git push origin v0.0.1
Next, make a small change in index.rst
.
Welcome to Sphinxy's documentation!
===================================
Explore Sphinxy's documentation.
Now, push your changes to main
:
git add docs/index.rst
git commit -m "Update docs"
git push origin main
Now re-run sphinx-multiversion
:
# from the project root
sphinx-multiversion docs ./docs/_build/html
You should now see that a new HTML folder was generated: ./docs/_build/html/v0.0.1
. Preview both
index.html
locally: ./docs/_build/html/main/index.html
and
./docs/_build/html/v0.0.1/index.html
. As you would expect, the modification will appear on tag
main
’s index.html
, but not on v0.0.1
’s.
You’ll also notice the new sidebar section we added titled Versions
, with links to all available
versions of your documentation.
Finally, let’s push the latest contents of the html/
folder to gh-pages
:
cd ./docs/_build/html
git branch # ensure you are on the gh-pages branch
git add .
git commit -m "Add versioning support"
git push -f origin gh-pages
Once the pages-build-deployment
workflow has completed, refresh your GitHub Pages URL. You’ll
notice that you’ll get a 404 File not found
error. Why is that?
Choosing a default version#
GitHub Pages looks for an index.html
file at the root level of the gh-pages
branch. Since we
have been using sphinx-multiversion
we’re storing index.html
files under their respective git
branch or tag folders now and GitHub could not find a page to load.
Modify your GitHub Pages URL to append a branch or a tag name name:
https://<username>.github.io/sphinxy/main
You’ll be redirected to that version’s index page and everything looks as expected.
Of course, this is not convenient. We want to automatically redirect the user to the latest
version; in our case the version on the main
branch. In order to achieve that, we need to add an
new file index.html
at the root of the gh-pages
branch that has this redirection logic (see the
docs).
First, we create a new file:
cd ./docs/_build/html
touch index.html
Now, open up this file, and add the following logic:
<!DOCTYPE html>
<html>
<head>
<title>Redirecting to main branch</title>
<meta charset="utf-8" />
<meta http-equiv="refresh" content="0; url=./main/index.html" />
<link rel="canonical" href="https://<username>.github.io/sphinxy/main/index.html" />
</head>
</html>
Let’s commit and push it to the gh-pages
branch:
# from the docs/_build/html dir
git add index.html
git commit -m "Add index.html to redirect to main branch"
git push origin gh-pages
You should now be automatically redirected to browse the documentation of your main
branch on
https://<username>.github.io/sphinxy
!