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:
Setting up GitHub pages for a repository
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:
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.
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 todocs/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 thedocs/
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.