Hosting Sphinx on GitHub Pages#

While you can install Sphinx on your laptop and build documentation locally, ultimately you will want to share docs via a web server. To do this, we will go over:

  1. Setting up GitHub pages for a repository

  2. Using GitHub Workflows to automate building documentation

1. Set up GitHub Pages#

GitHub Pages allow users to view web pages generated by our documentation. While there already a page set up for the sphinx-documentation-demo project, you will need to do the following steps for a new project:

1a. Navigate to Page settings#

../_images/guide_install_repo_settings.png

Click the Settings tab in the project you want to add to GitHub Pages.#

../_images/guide_install_repo_page_tab.png

Click the Pages tab (in the left sidebar) in the Settings page.#

1b. Select branch to use with GitHub Pages#

Warning

The GitHub repo must be public in order for it to be accessible via GitHub Pages.

../_images/guide_install_page_branch.png

When documentation is successfully deployed, you will see a link that looks like A. For a new repo, this will not be active. You will need to set the branch (see B). This demo uses a separate branch called gh-pages to deploy documentation.#

2. Using GitHub Workflow to Build Documentation#

GitHub can automatically discover workflows located in the .github/workflows directory within the project directory. We will be using a workflow to build documentation whenever a new commit is pushed to the repository.

2a. Example Workflow#

We will be reviewing the workflow that sphinx-documentation-demo uses.

You can find the workflow in sphinx-documentation-demo/.github/workflows/documentation.yml

 1name: documentation
 2
 3on: [push, pull_request, workflow_dispatch]
 4
 5permissions:
 6contents: write
 7
 8jobs:
 9docs:
10    runs-on: ubuntu-latest
11    steps:
12    - uses: actions/checkout@v3
13    - uses: actions/setup-python@v3
14    - name: Install dependencies
15        run: |
16        pip install sphinx pydata-sphinx-theme sphinx-design sphinx-copybutton sphinx-autoapi
17    - name: Sphinx build
18        run: |
19        sphinx-build docs _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
  • Line 16. Install dependencies
    • This tells GitHub to install sphinx and its dependencies to the ubuntu image.

    • Note: After you add a new sphinx extension to pyproject.toml and to docs/conf.py, you’ll also need to add the dependency to the list on line 16.

  • Line 19. Build Docs
    • This runs sphinx-build on the docs/ directory (in the repo) and creates the _build/ directory.

  • Line 24. Publish to branch
    • After building the docs, this line tells GitHub to publish the changes to the gh-pages branch.

    • Note: this needs to be the same branch that GitHub Pages is set to publish with.

  • Line 26. Specify directory to publish
    • This informs GitHub Pages where the static HTML documentation (built by sphinx-build) is located.

3. Verifying GitHub Workflow Actions#

Using the previously mentioned workflow, a GitHub Action will be triggered whenever a commit is pushed to the repo. You can monitor running jobs from your GitHub Repository.

../_images/guide_install_github_actions_tab.png

Click on the Actions tab.#

../_images/guide_install_actions_build.png

Current and previous workflow runs are listed in the Actions tab. The color of the icon indicates the current status of the run. You may notice a pages build and deployment job running. This is reponsible for building the docs.#

../_images/guide_install_actions_error.png

A red icon indicates that the run has failed. You can click on a run to get debug info for each step. Featured in this figure is a very common error: sphinx is missing an extension because it was not installed in the documentation.yml workflow.#

To fix this, sphinx-copybutton had to be added to line 16 in documentation.yml (see 2a. Example Workflow)