Deploying Sphinx documentation to GitHub Pages
Objectives
Create a basic workflow which you can take home and adapt for your project.
GitHub Pages
Serve websites from a GitHub repository.
It is no problem to serve using your own URL
https://myproject.orginstead ofhttps://myuser.github.io/myproject.
GitHub Actions
Automatically runs code when your repository changes.
We will let it run
sphinx-buildand make the result available to GitHub Pages.
Our goal: putting it all together
Host source code with documentation sources on a public Git repository.
Each time we
git pushto the repository, a GitHub action triggers to rebuild the documentation.The documentation is pushed to a separate branch called ‘gh-pages’.
Demonstration - Deploy Sphinx documentation to GitHub/GitLab Pages
GH-Pages-1: Deploy Sphinx documentation to GitHub or GitLab Pages
In this exercise we will create an example repository on a software forge (GitHub or GitLab) and deploy it to the corresponding “Page” service (GitHub pages or GitLab pages)
Preliminary: Verify the “Pages” feature is available on your forge
On github.com, the feature is typically available.
On GitLab servers, it depends on the configuration.
On
gitlab.com, the feature is typically available.On
codebase.helmholtz.cloud, the feature is typically availableOn other instances, you have to check. You can verify that in a project you have on that GitLab instance. Go to the page of a project, on Settings -> General -> Visibility, project features, permissions and check if the Pages can be activated. If not, then your GitLab instance might not allow you to host pages, and you will need another instance or service to host your static website.
Step 1:
Generate the repository to be used as the starting point. Repositories can be generated from templates, created from imports, or created as empty and filled with a push from you own machine.
In the case of GitHub, the most convenient way is to generate the repository from a template.
Go to the documentation-example project template on GitHub and create a copy to your namespace.
Give it a name, for instance “documentation-example”.
You don’t need to “Include all branches”
Click on “Create a repository”.
Oh GitLab, althoug a “template” feature is present, its use is more constrained, so we will instead import the template repository from GitHub.
Go to your account on the GitLab instance you plan to use, click on the
+icon at the top of the page on the right, and click on “New Project/Repository”.Click on the “Import Project” tile, then on “Repository by URL”
In the “Git repository URL” field, paste
https://github.com/coderefinery/documentation-example, and click the “Check connection” button, which is immediately on the right.You also need to choose:
a project name, for instance “documentation-example”
an appropriate group or namespace. Your personal namespace is fine.
a visibility level: choose “public”, otherwise the pipelines you create will not be able to run on “public” infrastructure.
After that, click on “Create Project”. You will see that GitLab is basically cloning the GitHub project.
Step 2: Browse the new repository.
The documentation part of the project is in
doc/(many projects do it this way).The source code for your project could then go under
src/.
Step 3: Automate the generation of the documentation
Add the GitHub Action to your new Git repository.
Add a new file at
.github/workflows/documentation.yml(either through terminal or web interface), containing:
1name: documentation
2
3on: [push, pull_request, workflow_dispatch]
4
5permissions:
6 contents: write
7
8jobs:
9 docs:
10 runs-on: ubuntu-latest
11 steps:
12 - uses: actions/checkout@v4
13 - uses: actions/setup-python@v5
14 - name: Install dependencies
15 run: |
16 pip install sphinx sphinx_rtd_theme myst_parser
17 - name: Sphinx build
18 run: |
19 sphinx-build doc _build
20 - name: Deploy to GitHub Pages
21 uses: peaceiris/actions-gh-pages@v3
22 if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
23 with:
24 publish_branch: gh-pages
25 github_token: ${{ secrets.GITHUB_TOKEN }}
26 publish_dir: _build/
27 force_orphan: true
You don’t need to understand all of the above – you should mainly pay attention the highlighted lines which are shell commands (we know this because they are part of a
run: |section). The first usespipto install the dependencies and the second runssphinx-buildto actually build the documentation (as we saw in the previous episode).After the file has been committed (and pushed), check the action at
https://github.com/USER/documentation-example/actions(replaceUSERwith your GitHub username).
On GitLab, automation is typically managed by the .gitlab-ci.yml file
at the top directory of your repository.
Create that file (if not existing), and add the definition of a new job in it:
1create-pages:
2 image: python:latest
3 script:
4 - pip install sphinx sphinx_rtd_theme myst_parser
5 - sphinx-build doc public
6 pages: true
Step 4: Enable Pages feature and verify it’s running
Go to “Settings” -> “Pages”.
Under “Build and deployment”
In the Source section: choose “Deploy from a branch” in the dropdown menu
In the Branch section: choose “gh-pages” and “/ (root)” in the dropdown menus and click the Save button.
You should now be able to verify the pages deployment in the “Actions” list (this is how it looks like for this lesson material).
With the default settings, on a GitLab server with the Pages feature enabled everything should work. To check:
Go to Build -> Pipelines. There should be a running (or complete) pipeline at the top of the list, with two stages.
if you see only one stage, the “Pages” feature might not be active for your repository or for the whole instance
if you see no pipelines, then the CI/CD feature might be disabled, or you might have misnamed the
.gitlab-ci.ymlfile. (the project features can be checked in Settings->General->*Visibility, project features, permissions)
Step 5: Verify the result
Your site should now be live at
https://USER.github.io/documentation-example/ (replace USER).
Your site should be live. To find out the exact URL, go to the main page of your project and on the right (under the “Project Information” header) click on the “GitLab Pages” link).
Step 6 (optional): Verify refreshing the documentation
Commit some changes to your documentation
Verify that the documentation website refreshes after your changes (can take few seconds or a minute)
Exercise - Sphinx documentation on GitHub Pages
GH-Pages-2: Putting it all together
Follow the above instructions to create a new repository with a Sphinx documentation project;
Try adding one or more of the following to your Sphinx project:
API documentation (see exercise in part 1 on API references) which requires the
sphinx-autodoc2package.a Jupyter notebook (see exercise in part 1 on Jupyter notebooks) which requires the
myst-nbpackage.change the theme (see the end of the quickstart in part 1). You can browse themes and find their package names on the Sphinx themes gallery.
Important
The computer on which the GitHub actions run is not your local machine, and might not have the libraries you need to build the project. Make sure you update the dependencies (installed with
pipin the demonstration) appropriately.Important
Make sure the correct file paths are used. This will require adjusting paths from the example from the previous episode to the new layout. Note many paths, including e.g. the
autodoc2_packagespreference are now relative to thedoc/directory.
What do you need to change in the workflow file?
Solution
API documentation
In the workflow/pipeline definition file (
.github/workflows/documentation.ymlor.gitlab-ci.yml), when callingpip, add the packagesphinx-autodoc2to the list of packages to install.Follow the instructions in Sphinx-3 changing paths so that:
multiply.pyissrc/multiply.pyand is specified as../src/multiply.pyin theautodoc2_packagespreference inconf.pyconf.pyisdoc/conf.pyindex.mdisdoc/index.md.
Commit and push your changes, verify the action has run successfully, and view the built site in your browser.
a Jupyter notebook
In the workflow/pipeline definition file (
.github/workflows/documentation.ymlor.gitlab-ci.yml), when callingpip, add the packagemyst-nbto the list of packages to install.Follow the instructions in Sphinx-4 changing paths so that:
flower.mdisdoc/flower.mdconf.pyisdoc/conf.pyindex.mdisdoc/index.md.
Commit and push your changes, verify the action has run successfully, and view the built site in your browser.
change the theme
In the workflow/pipeline definition file (
.github/workflows/documentation.ymlor.gitlab-ci.yml), when callingpip, and add the theme package if necessary.In
doc/conf.pychangehtml_theme = 'sphinx_rtd_theme'to the name of your chosen theme.Commit and push your changes, verify the action has run successfully, and view the built site in your browser.
Alternatives to GitHub and GitLab pages
Read the Docs is the most common alternative to hosting in GitHub Pages.
Sphinx builds HTML files (this is what static site generators do), and you can host them anywhere, for example your university’s web space or own web server.
Migrating your own documentation to Sphinx
First convert your documentation to Markdown using Pandoc.
Create a file
index.rstwhich lists all other Markdown files and provides the table of contents.Add a
conf.pyfile. You can generate a starting point forconf.pyandindex.rstwithsphinx-quickstart, or you can take the examples in this lesson as inspiration.Test building the documentation locally with
sphinx-build.Once this works, follow the above steps to build and deploy to GitHub Pages or some other web space.
Keypoints
Sphinx makes simple HTML (and more) files, so it is easy to find a place to host them.
Github Pages + Github Actions provides a convenient way to make sites and host them on the web.