|
| 1 | +# Deploying Sphinx documentation to GitHub Pages |
| 2 | + |
| 3 | +```{objectives} |
| 4 | +- Create a basic workflow which you can take home and adapt for your project. |
| 5 | +``` |
| 6 | + |
| 7 | +## [GitHub Pages](https://pages.github.com/) |
| 8 | + |
| 9 | +- Serve websites from a GitHub repository. |
| 10 | +- It is no problem to serve using your own URL `https://myproject.org` instead of `https://myuser.github.io/myproject`. |
| 11 | + |
| 12 | + |
| 13 | +## [GitHub Actions](https://github.com/features/actions/) |
| 14 | + |
| 15 | +- Automatically runs code when your repository changes. |
| 16 | +- We will let it run `sphinx-build` and make the result available to GitHub Pages. |
| 17 | + |
| 18 | + |
| 19 | +## Our goal: putting it all together |
| 20 | + |
| 21 | +- Host source code with documentation sources on a public Git repository. |
| 22 | +- Each time we `git push` to the repository, a GitHub action triggers to |
| 23 | + rebuild the documentation. |
| 24 | +- The documentation is pushed to a separate branch called 'gh-pages'. |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## Demonstration - Deploy Sphinx documentation to GitHub Pages |
| 29 | + |
| 30 | +````{exercise} GH-Pages-1: Deploy Sphinx documentation to GitHub Pages |
| 31 | +In this exercise we will create an example repository on GitHub and |
| 32 | +deploy it to GitHub Pages. |
| 33 | + |
| 34 | +**Step 1**: Go to the [documentation-example](https://github.com/coderefinery/documentation-example/generate) project template |
| 35 | +on GitHub and create a copy to your namespace. |
| 36 | +- Give it a name, for instance "documentation-example". |
| 37 | +- You don't need to "Include all branches" |
| 38 | +- Click on "Create a repository". |
| 39 | + |
| 40 | +**Step 2**: Browse the new repository. |
| 41 | +- It will look very familar to the previous episode. |
| 42 | +- However, we have moved the documentation part under `doc/` (many projects do it this |
| 43 | + way). But it is still a Sphinx documentation project. |
| 44 | +- The source code for your project could then go under `src/`. |
| 45 | + |
| 46 | + |
| 47 | +**Step 3**: Add the GitHub Action to your new Git repository. |
| 48 | +- Add a new file at `.github/workflows/documentation.yml` (either through terminal or web interface), containing: |
| 49 | +```{code-block} yaml |
| 50 | +:linenos: |
| 51 | +:emphasize-lines: 16,19 |
| 52 | +name: documentation |
| 53 | + |
| 54 | +on: [push, pull_request, workflow_dispatch] |
| 55 | + |
| 56 | +permissions: |
| 57 | + contents: write |
| 58 | + |
| 59 | +jobs: |
| 60 | + docs: |
| 61 | + runs-on: ubuntu-latest |
| 62 | + steps: |
| 63 | + - uses: actions/checkout@v4 |
| 64 | + - uses: actions/setup-python@v5 |
| 65 | + - name: Install dependencies |
| 66 | + run: | |
| 67 | + pip install sphinx sphinx_rtd_theme myst_parser |
| 68 | + - name: Sphinx build |
| 69 | + run: | |
| 70 | + sphinx-build doc _build |
| 71 | + - name: Deploy to GitHub Pages |
| 72 | + uses: peaceiris/actions-gh-pages@v3 |
| 73 | + if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} |
| 74 | + with: |
| 75 | + publish_branch: gh-pages |
| 76 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 77 | + publish_dir: _build/ |
| 78 | + force_orphan: true |
| 79 | +``` |
| 80 | +- You don't need to understand all of the above |
| 81 | + -- you should mainly pay attention the highlighted lines |
| 82 | + which are shell commands (we know this because they are part of a `run: |` section). |
| 83 | + The first uses `pip` to install the dependencies and the second runs `sphinx-build` |
| 84 | + to actually build the documentation (as we saw in the previous episode). |
| 85 | +- After the file has been committed (and pushed), |
| 86 | + check the action at `https://github.com/USER/documentation-example/actions` |
| 87 | + (replace `USER` with your GitHub username). |
| 88 | + |
| 89 | +**Step 4**: Enable GitHub Pages |
| 90 | +- On GitHub go to "Settings" -> "Pages". |
| 91 | +- Under "Build and deployment" |
| 92 | + - In the **Source** section: choose "Deploy from a branch" in the dropdown menu |
| 93 | + - In the **Branch** section: choose "gh-pages" and "/ (root)" in the dropdown menus |
| 94 | + and click the **Save** button. |
| 95 | +- You should now be able to verify the pages deployment in the "Actions" list |
| 96 | + ([this is how it looks like](https://github.com/coderefinery/documentation/actions) |
| 97 | + for this lesson material). |
| 98 | + |
| 99 | +**Step 5**: Verify the result |
| 100 | +- Your site should now be live on `https://USER.github.io/documentation-example/` (replace USER). |
| 101 | + |
| 102 | +**Step 6 (optional)**: Verify refreshing the documentation |
| 103 | +- Commit some changes to your documentation |
| 104 | +- Verify that the documentation website refreshes after your changes (can take few seconds or a minute) |
| 105 | +```` |
| 106 | + |
| 107 | +## Exercise - Sphinx documentation on GitHub Pages |
| 108 | +````{exercise} GH-Pages-2: Putting it all together |
| 109 | + |
| 110 | +1. Follow the above instructions to create a new repository with a Sphinx documentation project; |
| 111 | +2. Try adding one or more of the following to your Sphinx project: |
| 112 | + 1. API documentation (see [previous exercise](#api-exercise) on API references) which requires the `sphinx-autodoc2` package. |
| 113 | + 2. a Jupyter notebook (see [previous exercise](#jupyter-exercise) on Jupyter notebooks) which requires the `myst-nb` package. |
| 114 | + 3. change the theme (see the end of [the quickstart](#quickstart)). You can browse themes and find their package names on the [Sphinx themes gallery](https://sphinx-themes.org/#themes). |
| 115 | + |
| 116 | + ```{important} |
| 117 | + The computer on which the GitHub actions run is *not* your local machine, |
| 118 | + and might not have the libraries you need to build the project. |
| 119 | + Make sure you update the dependencies (installed with `pip` in the demonstration) appropriately. |
| 120 | + ``` |
| 121 | + ```{important} |
| 122 | + 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_packages` preference are now relative to the `doc/` directory. |
| 123 | + ``` |
| 124 | +What do you need to change in the workflow file? |
| 125 | + |
| 126 | +```{solution} Solution |
| 127 | +1. **API documentation** |
| 128 | + 1. Change line 16 of `.github/workflows/documentation.yml` from `pip install sphinx sphinx_rtd_theme myst_parser` to `pip install sphinx sphinx_rtd_theme myst_parser sphinx-autodoc2`. |
| 129 | + 2. Follow the instructions in [Sphinx-3](#api-exercise) changing paths so that: |
| 130 | + 1. `multiply.py` is `src/multiply.py` and is specified as `../src/multiply.py` in the `autodoc2_packages` preference in `conf.py` |
| 131 | + 2. `conf.py` is `doc/conf.py` |
| 132 | + 3. `index.md` is `doc/index.md`. |
| 133 | + 3. Commit and push your changes, verify the action has run successfully, and view the built site in your browser. |
| 134 | +2. **a Jupyter notebook** |
| 135 | + 1. Change line 16 of `.github/workflows/documentation.yml` from `pip install sphinx sphinx_rtd_theme myst_parser` to `pip install sphinx sphinx_rtd_theme myst_parser myst-nb`. |
| 136 | + 2. Follow the instructions in [Sphinx-4](#jupyter-exercise) changing paths so that: |
| 137 | + 1. `flower.md` is `docs/flower.md` |
| 138 | + 2. `conf.py` is `doc/conf.py` |
| 139 | + 3. `index.md` is `doc/index.md`. |
| 140 | + 3. Commit and push your changes, verify the action has run successfully, and view the built site in your browser. |
| 141 | +3. **change the theme** |
| 142 | + 1. Change line 16 of `.github/workflows/documentation.yml` and add the theme package if necessary. |
| 143 | + 2. In `docs/config.py` change `html_theme = 'sphinx_rtd_theme'` to the name of your chosen theme. |
| 144 | + 3. Commit and push your changes, verify the action has run successfully, and view the built site in your browser. |
| 145 | +``` |
| 146 | +```` |
| 147 | + |
| 148 | +## Alternatives to GitHub Pages |
| 149 | + |
| 150 | +- [GitLab Pages](https://docs.gitlab.com/ee/user/project/pages/) |
| 151 | + and [GitLab CI](https://docs.gitlab.com/ee/ci/) can create a very similar workflow. |
| 152 | + |
| 153 | +- [Read the Docs](https://readthedocs.org) is the most common alternative to |
| 154 | + hosting in GitHub Pages. |
| 155 | + |
| 156 | +- Sphinx builds HTML files (this is what static site generators do), and you |
| 157 | + can host them anywhere, for example your university's web space or own web server. |
| 158 | + |
| 159 | + |
| 160 | +## Migrating your own documentation to Sphinx |
| 161 | + |
| 162 | +- First convert your documentation to Markdown using [Pandoc](https://pandoc.org). |
| 163 | +- Create a file `index.rst` which lists all other Markdown files and provides the |
| 164 | + table of contents. |
| 165 | +- Add a `conf.py` file. You can generate a starting point for `conf.py` and |
| 166 | + `index.rst` with `sphinx-quickstart`, or you can take the examples in this |
| 167 | + lesson as inspiration. |
| 168 | +- Test building the documentation locally with `sphinx-build`. |
| 169 | +- Once this works, follow the above steps to build and deploy to GitHub Pages or some other web space. |
| 170 | + |
| 171 | +--- |
| 172 | + |
| 173 | +```{keypoints} |
| 174 | +- Sphinx makes simple HTML (and more) files, so it is easy to find a place to host them. |
| 175 | +- Github Pages + Github Actions provides a convenient way to make |
| 176 | + sites and host them on the web. |
| 177 | +``` |
0 commit comments