| 1 |
Appendix: Deploying a Sphinx project online |
| 2 |
=========================================== |
| 3 |
|
| 4 |
When you are ready to show your documentation project to the world, there are |
| 5 |
many options available to do so. Since the HTML generated by Sphinx is static, |
| 6 |
you can decouple the process of building your HTML documentation from hosting |
| 7 |
such files in the platform of your choice. You will not need a sophisticated |
| 8 |
server running Python: virtually every web hosting service will suffice. |
| 9 |
|
| 10 |
Therefore, the challenge is less how or where to serve the static HTML, but |
| 11 |
rather how to pick a workflow that automatically updates the deployed |
| 12 |
documentation every time there is a change in the source files. |
| 13 |
|
| 14 |
The following sections describe some of the available options to deploy |
| 15 |
your online documentation, and give some background information. If you want |
| 16 |
to go directly to the practical part, you can skip to :ref:`publishing-sources`. |
| 17 |
|
| 18 |
Sphinx-friendly deployment options |
| 19 |
---------------------------------- |
| 20 |
|
| 21 |
There are several possible options you have to host your Sphinx documentation. |
| 22 |
Some of them are: |
| 23 |
|
| 24 |
**Read the Docs** |
| 25 |
`Read the Docs`_ is an online service specialized in hosting technical |
| 26 |
documentation written in Sphinx, as well as MkDocs. They have a |
| 27 |
number of extra features, such as versioned documentation, traffic and |
| 28 |
search analytics, custom domains, user-defined redirects, and more. |
| 29 |
|
| 30 |
**GitHub Pages** |
| 31 |
`GitHub Pages`_ is a simple static web hosting tightly integrated with |
| 32 |
`GitHub`_: static HTML is served from one of the branches of a project, |
| 33 |
and usually sources are stored in another branch so that the output |
| 34 |
can be updated every time the sources change (for example using `GitHub |
| 35 |
Actions`_). It is free to use and supports custom domains. |
| 36 |
|
| 37 |
**GitLab Pages** |
| 38 |
`GitLab Pages`_ is a similar concept to GitHub Pages, integrated with |
| 39 |
`GitLab`_ and usually automated with `GitLab CI`_ instead. |
| 40 |
|
| 41 |
**Netlify** |
| 42 |
`Netlify`_ is a sophisticated hosting for static sites enhanced by |
| 43 |
client-side web technologies like JavaScript (so-called `"Jamstack"`_). |
| 44 |
They offer support for headless content management systems and |
| 45 |
serverless computing. |
| 46 |
|
| 47 |
**Your own server** |
| 48 |
You can always use your own web server to host Sphinx HTML documentation. |
| 49 |
It is the option that gives more flexibility, but also more complexity. |
| 50 |
|
| 51 |
All these options have zero cost, with the option of paying for extra features. |
| 52 |
|
| 53 |
.. _Read the Docs: https://readthedocs.org/ |
| 54 |
.. _GitHub Pages: https://pages.github.com/ |
| 55 |
.. _GitHub: https://github.com/ |
| 56 |
.. _GitHub Actions: https://github.com/features/actions |
| 57 |
.. _GitLab Pages: https://about.gitlab.com/stages-devops-lifecycle/pages/ |
| 58 |
.. _GitLab: https://gitlab.com/ |
| 59 |
.. _GitLab CI: https://about.gitlab.com/stages-devops-lifecycle/continuous-integration/ |
| 60 |
.. _Netlify: https://www.netlify.com/ |
| 61 |
.. _"Jamstack": https://jamstack.org/ |
| 62 |
|
| 63 |
Embracing the "Docs as Code" philosophy |
| 64 |
--------------------------------------- |
| 65 |
|
| 66 |
The free offerings of most of the options listed above require your |
| 67 |
documentation sources to be publicly available. Moreover, these services |
| 68 |
expect you to use a `Version Control System`_, a technology that tracks the |
| 69 |
evolution of a collection of files as a series of snapshots ("commits"). |
| 70 |
The practice of writing documentation in plain text files with the same tools |
| 71 |
as the ones used for software development is commonly known as `"Docs as Code"`_. |
| 72 |
|
| 73 |
The most popular Version Control System nowadays is Git_, a free and open |
| 74 |
source tool that is the backbone of services like GitHub and GitLab. |
| 75 |
Since both Read the Docs and Netlify have integrations with GitHub and GitLab, |
| 76 |
and both GitHub and GitLab have an integrated Pages product, the most effective |
| 77 |
way of automatically build your documentation online is to upload your sources |
| 78 |
to either of these Git hosting services. |
| 79 |
|
| 80 |
.. _Version Control System: https://en.wikipedia.org/wiki/Version_control |
| 81 |
.. _"Docs as Code": https://www.writethedocs.org/guide/docs-as-code/ |
| 82 |
.. _Git: https://git-scm.com/ |
| 83 |
|
| 84 |
.. _publishing-sources: |
| 85 |
|
| 86 |
Publishing your documentation sources |
| 87 |
------------------------------------- |
| 88 |
|
| 89 |
GitHub |
| 90 |
~~~~~~ |
| 91 |
|
| 92 |
The quickest way to upload an existing project to GitHub is to: |
| 93 |
|
| 94 |
1. `Sign up for a GitHub account <https://github.com/signup>`_. |
| 95 |
2. `Create a new repository <https://github.com/new>`_. |
| 96 |
3. Open `the "Upload files" page`_ of your new repository. |
| 97 |
4. Select the files on your operating system file browser (in your case |
| 98 |
``README.rst``, ``lumache.py``, the makefiles under the ``docs`` directory, |
| 99 |
and everything under ``docs/source``) and drag them to the GitHub interface |
| 100 |
to upload them all. |
| 101 |
5. Click on the :guilabel:`Commit changes` button. |
| 102 |
|
| 103 |
.. _the "Upload files" page: https://docs.github.com/en/repositories/working-with-files/managing-files/adding-a-file-to-a-repository |
| 104 |
|
| 105 |
.. note:: |
| 106 |
|
| 107 |
Make sure you don't upload the ``docs/build`` directory, as it contains the |
| 108 |
output generated by Sphinx and it will change every time you change the |
| 109 |
sources, complicating your workflow. |
| 110 |
|
| 111 |
These steps do not require access to the command line or installing any |
| 112 |
additional software. To learn more, read `this quickstart tutorial`_ or |
| 113 |
consult the `official GitHub documentation`_ |
| 114 |
|
| 115 |
.. _this quickstart tutorial: https://docs.github.com/en/get-started/quickstart |
| 116 |
.. _official GitHub documentation: https://docs.github.com/en/get-started |
| 117 |
|
| 118 |
GitLab |
| 119 |
~~~~~~ |
| 120 |
|
| 121 |
Similarly to GitHub, the fastest way to upload your project to GitLab is |
| 122 |
using the web interface: |
| 123 |
|
| 124 |
1. `Sign up for a GitLab account <https://gitlab.com/users/sign_up>`_. |
| 125 |
2. `Create a new blank project <https://gitlab.com/projects/new>`_. |
| 126 |
3. Upload the project files (in your case ``README.rst``, ``lumache.py``, the |
| 127 |
makefiles under the ``docs`` directory, and everything under |
| 128 |
``docs/source``) one by one using the :guilabel:`Upload File` button [#f1]_. |
| 129 |
|
| 130 |
Again, these steps do not require additional software on your computer. To |
| 131 |
learn more, you can: |
| 132 |
|
| 133 |
- Follow `this tutorial`_ to install Git on your machine. |
| 134 |
- Browse the `GitLab User documentation`_ to understand the possibilities of |
| 135 |
the platform. |
| 136 |
|
| 137 |
.. _this tutorial: https://docs.gitlab.com/ee/gitlab-basics/start-using-git.html |
| 138 |
.. _GitLab User documentation: https://docs.gitlab.com/ee/user/index.html |
| 139 |
|
| 140 |
.. note:: |
| 141 |
|
| 142 |
Make sure you don't upload the ``docs/build`` directory, as it contains the |
| 143 |
output generated by Sphinx and it will change every time you change the |
| 144 |
sources, complicating your workflow. |
| 145 |
|
| 146 |
.. [#f1] At the time of writing, `uploading whole directories to GitLab using |
| 147 |
only the web |
| 148 |
interface <https://gitlab.com/gitlab-org/gitlab/-/issues/228490>`_ is |
| 149 |
not yet implemented. |
| 150 |
|
| 151 |
Publishing your HTML documentation |
| 152 |
---------------------------------- |
| 153 |
|
| 154 |
Read the Docs |
| 155 |
~~~~~~~~~~~~~ |
| 156 |
|
| 157 |
`Read the Docs`_ offers integration with both GitHub and GitLab. The quickest |
| •158 |
way of getting started is to follow `the RTD tutorial`_, which is loosely |
| 159 |
based on this one. |
| 160 |
You can publish your sources on GitHub as explained :ref:`in the previous |
| 161 |
section <publishing-sources>`, then skip directly to |
| •162 |
`Creating a Read the Docs account`_. |
| 163 |
If you choose GitLab instead, the process is similar. |
| 164 |
|
| •165 |
.. _the RTD tutorial: https://docs.readthedocs.io/en/stable/tutorial/index.html |
| 166 |
.. _Creating a Read the Docs account: https://docs.readthedocs.io/en/stable/tutorial/index.html#creating-a-read-the-docs-account |
| 167 |
|
| 168 |
GitHub Pages |
| 169 |
~~~~~~~~~~~~ |
| 170 |
|
| 171 |
`GitHub Pages`_ requires you to :ref:`publish your |
| 172 |
sources <publishing-sources>` on `GitHub`_. After that, you will need an |
| 173 |
automated process that performs the ``make html`` step every time the sources |
| 174 |
change. That can be achieved using `GitHub Actions`_. |
| 175 |
|
| 176 |
After you have published your sources on GitHub, create a file named |
| 177 |
``.github/workflows/sphinx.yml`` in your repository with the following |
| 178 |
contents: |
| 179 |
|
| 180 |
.. code-block:: yaml |
| 181 |
:caption: .github/workflows/sphinx.yml |
| 182 |
|
| 183 |
name: "Sphinx: Render docs" |
| 184 |
|
| 185 |
on: push |
| 186 |
|
| 187 |
jobs: |
| 188 |
build: |
| 189 |
runs-on: ubuntu-latest |
| 190 |
permissions: |
| 191 |
contents: write |
| 192 |
steps: |
| 193 |
- uses: actions/checkout@v4 |
| 194 |
with: |
| 195 |
persist-credentials: false |
| 196 |
- name: Build HTML |
| 197 |
uses: ammaraskar/sphinx-action@master |
| 198 |
- name: Upload artifacts |
| 199 |
uses: actions/upload-artifact@v4 |
| 200 |
with: |
| 201 |
name: html-docs |
| 202 |
path: docs/build/html/ |
| 203 |
- name: Deploy |
| 204 |
uses: peaceiris/actions-gh-pages@v3 |
| 205 |
if: github.ref == 'refs/heads/main' |
| 206 |
with: |
| 207 |
github_token: ${{ secrets.GITHUB_TOKEN }} |
| 208 |
publish_dir: docs/build/html |
| 209 |
|
| 210 |
This contains a GitHub Actions workflow with a single job of four steps: |
| 211 |
|
| 212 |
1. Checkout the code. |
| 213 |
2. Build the HTML documentation using Sphinx. |
| 214 |
3. Attach the HTML output the artifacts to the GitHub Actions job, for easier |
| 215 |
inspection. |
| 216 |
4. If the change happens on the default branch, take the contents of |
| 217 |
``docs/build/html`` and push it to the ``gh-pages`` branch. |
| 218 |
|
| 219 |
Next, you need to specify the dependencies for the ``make html`` step to be |
| 220 |
successful. For that, create a file ``docs/requirements.txt`` and add the |
| 221 |
following contents: |
| 222 |
|
| 223 |
.. code-block:: |
| 224 |
:caption: docs/requirements.txt |
| 225 |
|
| 226 |
furo==2021.11.16 |
| 227 |
|
| 228 |
And finally, you are ready to `publish GitHub Pages from a branch`_. For |
| 229 |
that, go to :guilabel:`Settings`, then :guilabel:`Pages` on the left sidebar, |
| 230 |
select the ``Deploy from a branch`` item in the "Source" dropdown menu, |
| 231 |
select the ``gh-page`` branch in the "Branch" dropdown menu, and click |
| 232 |
:guilabel:`Save`. After a few minutes, you should be able to see your HTML at |
| 233 |
the designated URL. |
| 234 |
|
| 235 |
.. _publish GitHub Pages from a branch: https://docs.github.com/en/pages/getting-started-with-github-pages/configuring-a-publishing-source-for-your-github-pages-site#publishing-from-a-branch |
| 236 |
|
| 237 |
GitLab Pages |
| 238 |
~~~~~~~~~~~~ |
| 239 |
|
| 240 |
`GitLab Pages`_, on the other hand, requires you to :ref:`publish your |
| 241 |
sources <publishing-sources>` on `GitLab`_. When you are ready, you can |
| 242 |
automate the process of running ``make html`` using `GitLab CI`_. |
| 243 |
|
| 244 |
After you have published your sources on GitLab, create a file named |
| 245 |
``.gitlab-ci.yml`` in your repository with these contents: |
| 246 |
|
| 247 |
.. code-block:: yaml |
| 248 |
:caption: .gitlab-ci.yml |
| 249 |
|
| 250 |
stages: |
| 251 |
- deploy |
| 252 |
|
| 253 |
pages: |
| 254 |
stage: deploy |
| 255 |
image: python:3.14-slim |
| 256 |
before_script: |
| 257 |
- apt-get update && apt-get install make --no-install-recommends -y |
| 258 |
- python -m pip install sphinx furo |
| 259 |
script: |
| 260 |
- cd docs && make html |
| 261 |
after_script: |
| 262 |
- mv docs/build/html/ ./public/ |
| 263 |
artifacts: |
| 264 |
paths: |
| 265 |
- public |
| 266 |
rules: |
| 267 |
- if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH |
| 268 |
|
| 269 |
This contains a GitLab CI workflow with one job of several steps: |
| 270 |
|
| 271 |
1. Install the necessary dependencies. |
| 272 |
2. Build the HTML documentation using Sphinx. |
| 273 |
3. Move the output to a known artifacts location. |
| 274 |
|
| 275 |
.. note:: |
| 276 |
You will need to `validate your account`_ by entering a payment method |
| 277 |
(you will be charged a small amount that will then be reimbursed). |
| 278 |
|
| 279 |
.. _validate your account: https://about.gitlab.com/blog/2021/05/17/prevent-crypto-mining-abuse/#validating-an-account |
| 280 |
|
| 281 |
After that, if the pipeline is successful, you should be able to see your HTML |
| 282 |
at the designated URL. |