Debian Codegraph @

doc/tutorial/deploying.rst

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 :doc:`the RTD
159 tutorial <readthedocs:tutorial/index>`, which is loosely 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 :ref:`readthedocs:tutorial/index:Creating a Read the Docs account`.
163 If you choose GitLab instead, the process is similar.
164
165 GitHub Pages
166 ~~~~~~~~~~~~
167
168 `GitHub Pages`_ requires you to :ref:`publish your
169 sources <publishing-sources>` on `GitHub`_. After that, you will need an
170 automated process that performs the ``make html`` step every time the sources
171 change. That can be achieved using `GitHub Actions`_.
172
173 After you have published your sources on GitHub, create a file named
174 ``.github/workflows/sphinx.yml`` in your repository with the following
175 contents:
176
177 .. code-block:: yaml
178 :caption: .github/workflows/sphinx.yml
179
180 name: "Sphinx: Render docs"
181
182 on: push
183
184 jobs:
185 build:
186 runs-on: ubuntu-latest
187 permissions:
188 contents: write
189 steps:
190 - uses: actions/checkout@v4
191 with:
192 persist-credentials: false
193 - name: Build HTML
194 uses: ammaraskar/sphinx-action@master
195 - name: Upload artifacts
196 uses: actions/upload-artifact@v4
197 with:
198 name: html-docs
199 path: docs/build/html/
200 - name: Deploy
201 uses: peaceiris/actions-gh-pages@v3
202 if: github.ref == 'refs/heads/main'
203 with:
204 github_token: ${{ secrets.GITHUB_TOKEN }}
205 publish_dir: docs/build/html
206
207 This contains a GitHub Actions workflow with a single job of four steps:
208
209 1. Checkout the code.
210 2. Build the HTML documentation using Sphinx.
211 3. Attach the HTML output the artifacts to the GitHub Actions job, for easier
212 inspection.
213 4. If the change happens on the default branch, take the contents of
214 ``docs/build/html`` and push it to the ``gh-pages`` branch.
215
216 Next, you need to specify the dependencies for the ``make html`` step to be
217 successful. For that, create a file ``docs/requirements.txt`` and add the
218 following contents:
219
220 .. code-block::
221 :caption: docs/requirements.txt
222
223 furo==2021.11.16
224
225 And finally, you are ready to `publish GitHub Pages from a branch`_. For
226 that, go to :guilabel:`Settings`, then :guilabel:`Pages` on the left sidebar,
227 select the ``Deploy from a branch`` item in the "Source" dropdown menu,
228 select the ``gh-page`` branch in the "Branch" dropdown menu, and click
229 :guilabel:`Save`. After a few minutes, you should be able to see your HTML at
230 the designated URL.
231
232 .. _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
233
234 GitLab Pages
235 ~~~~~~~~~~~~
236
237 `GitLab Pages`_, on the other hand, requires you to :ref:`publish your
238 sources <publishing-sources>` on `GitLab`_. When you are ready, you can
239 automate the process of running ``make html`` using `GitLab CI`_.
240
241 After you have published your sources on GitLab, create a file named
242 ``.gitlab-ci.yml`` in your repository with these contents:
243
244 .. code-block:: yaml
245 :caption: .gitlab-ci.yml
246
247 stages:
248 - deploy
249
250 pages:
251 stage: deploy
252 image: python:3.14-slim
253 before_script:
254 - apt-get update && apt-get install make --no-install-recommends -y
255 - python -m pip install sphinx furo
256 script:
257 - cd docs && make html
258 after_script:
259 - mv docs/build/html/ ./public/
260 artifacts:
261 paths:
262 - public
263 rules:
264 - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
265
266 This contains a GitLab CI workflow with one job of several steps:
267
268 1. Install the necessary dependencies.
269 2. Build the HTML documentation using Sphinx.
270 3. Move the output to a known artifacts location.
271
272 .. note::
273 You will need to `validate your account`_ by entering a payment method
274 (you will be charged a small amount that will then be reimbursed).
275
276 .. _validate your account: https://about.gitlab.com/blog/2021/05/17/prevent-crypto-mining-abuse/#validating-an-account
277
278 After that, if the pipeline is successful, you should be able to see your HTML
279 at the designated URL.