| 1 |
# Deploying your docs |
| 2 |
|
| 3 |
A basic guide to deploying your docs to various hosting providers |
| 4 |
|
| 5 |
--- |
| 6 |
|
| 7 |
## GitHub Pages |
| 8 |
|
| 9 |
If you host the source code for a project on [GitHub], you can easily use |
| 10 |
[GitHub Pages] to host the documentation for your project. There are two basic |
| 11 |
[types of GitHub Pages sites]: Project Pages sites, and User and Organization |
| 12 |
Pages sites. They are nearly identical but have some important differences, |
| 13 |
which require a different work flow when deploying. |
| 14 |
|
| 15 |
### Project Pages |
| 16 |
|
| 17 |
Project Pages sites are simpler as the site files get deployed to a branch |
| 18 |
within the project repository (`gh-pages` by default). After you `checkout` the |
| 19 |
primary working branch (usually `master`) of the git repository where you |
| 20 |
maintain the source documentation for your project, run the following command: |
| 21 |
|
| 22 |
```sh |
| 23 |
mkdocs gh-deploy |
| 24 |
``` |
| 25 |
|
| 26 |
That's it! Behind the scenes, MkDocs will build your docs and use the |
| 27 |
[ghp-import] tool to commit them to the `gh-pages` branch and push the |
| 28 |
`gh-pages` branch to GitHub. |
| 29 |
|
| 30 |
Use `mkdocs gh-deploy --help` to get a full list of options available for the |
| 31 |
`gh-deploy` command. |
| 32 |
|
| 33 |
Be aware that you will not be able to review the built site before it is pushed |
| 34 |
to GitHub. Therefore, you may want to verify any changes you make to the docs |
| 35 |
beforehand by using the `build` or `serve` commands and reviewing the built |
| 36 |
files locally. |
| 37 |
|
| 38 |
WARNING: |
| 39 |
You should never edit files in your pages repository by hand if you're using |
| 40 |
the `gh-deploy` command because you will lose your work the next time you |
| 41 |
run the command. |
| 42 |
|
| 43 |
WARNING: |
| 44 |
If there are untracked files or uncommitted work in the local repository where |
| 45 |
`mkdocs gh-deploy` is run, these will be included in the pages that are deployed. |
| 46 |
|
| 47 |
### Organization and User Pages |
| 48 |
|
| 49 |
User and Organization Pages sites are not tied to a specific project, and the |
| 50 |
site files are deployed to the `master` branch in a dedicated repository named |
| 51 |
with the GitHub account name. Therefore, you need working copies of two |
| 52 |
repositories on our local system. For example, consider the following file |
| 53 |
structure: |
| 54 |
|
| 55 |
```text |
| 56 |
my-project/ |
| 57 |
mkdocs.yml |
| 58 |
docs/ |
| 59 |
orgname.github.io/ |
| 60 |
``` |
| 61 |
|
| 62 |
After making and verifying updates to your project you need to change |
| 63 |
directories to the `orgname.github.io` repository and call the |
| 64 |
`mkdocs gh-deploy` command from there: |
| 65 |
|
| 66 |
```sh |
| 67 |
cd ../orgname.github.io/ |
| 68 |
mkdocs gh-deploy --config-file ../my-project/mkdocs.yml --remote-branch master |
| 69 |
``` |
| 70 |
|
| 71 |
Note that you need to explicitly point to the `mkdocs.yml` configuration file as |
| 72 |
it is no longer in the current working directory. You also need to inform the |
| 73 |
deploy script to commit to the `master` branch. You may override the default |
| 74 |
with the [remote_branch] configuration setting, but if you forget to change |
| 75 |
directories before running the deploy script, it will commit to the `master` |
| 76 |
branch of your project, which you probably don't want. |
| 77 |
|
| 78 |
### Custom Domains |
| 79 |
|
| 80 |
GitHub Pages includes support for using a [Custom Domain] for your site. In |
| 81 |
addition to the steps documented by GitHub, you need to take one additional step |
| 82 |
so that MkDocs will work with your custom domain. You need to add a `CNAME` file |
| 83 |
to the root of your [docs_dir]. The file must contain a single bare domain or |
| 84 |
subdomain on a single line (see MkDocs' own [CNAME file] as an example). You may |
| 85 |
create the file manually, or use GitHub's web interface to set up the custom |
| 86 |
domain (under Settings / Custom Domain). If you use the web interface, GitHub |
| 87 |
will create the `CNAME` file for you and save it to the root of your "pages" |
| 88 |
branch. So that the file does not get removed the next time you deploy, you need |
| 89 |
to copy the file to your `docs_dir`. With the file properly included in your |
| 90 |
`docs_dir`, MkDocs will include the file in your built site and push it to your |
| 91 |
"pages" branch each time you run the `gh-deploy` command. |
| 92 |
|
| 93 |
If you are having problems getting a custom domain to work, see GitHub's |
| 94 |
documentation on [Troubleshooting custom domains]. |
| 95 |
|
| 96 |
[GitHub]: https://github.com/ |
| 97 |
[GitHub Pages]: https://pages.github.com/ |
| 98 |
[types of GitHub Pages sites]: https://docs.github.com/en/pages/getting-started-with-github-pages/about-github-pages#types-of-github-pages-sites |
| 99 |
[ghp-import]: https://github.com/davisp/ghp-import |
| 100 |
[remote_branch]: ./configuration.md#remote_branch |
| 101 |
[Custom Domain]: https://help.github.com/articles/adding-or-removing-a-custom-domain-for-your-github-pages-site |
| 102 |
[docs_dir]: ./configuration.md#docs_dir |
| 103 |
[CNAME file]: https://github.com/mkdocs/mkdocs/blob/master/docs/CNAME |
| 104 |
[Troubleshooting custom domains]: https://help.github.com/articles/troubleshooting-custom-domains/ |
| 105 |
|
| 106 |
## Read the Docs |
| 107 |
|
| 108 |
[Read the Docs][rtd] offers free documentation hosting. You can import your docs |
| 109 |
using the Git version control system. Read the Docs supports MkDocs out-of-the-box. |
| 110 |
Follow the [instructions] on their site to arrange the files in your repository properly, |
| 111 |
create an account and point it at your publicly hosted repository. If properly |
| 112 |
configured, your documentation will update each time you push commits to your |
| 113 |
public repository. |
| 114 |
|
| 115 |
[rtd]: https://readthedocs.org/ |
| 116 |
[instructions]: https://docs.readthedocs.io/en/stable/intro/getting-started-with-mkdocs.html |
| 117 |
|
| 118 |
## Other Providers |
| 119 |
|
| 120 |
Any hosting provider which can serve static files can be used to serve |
| 121 |
documentation generated by MkDocs. While it would be impossible to document how |
| 122 |
to upload the docs to every hosting provider out there, the following guidelines |
| 123 |
should provide some general assistance. |
| 124 |
|
| 125 |
When you build your site (using the `mkdocs build` command), all of the files |
| 126 |
are written to the directory assigned to the [site_dir] configuration option |
| 127 |
(defaults to `"site"`) in your `mkdocs.yaml` config file. Generally, you will |
| 128 |
simply need to copy the contents of that directory to the root directory of your |
| 129 |
hosting provider's server. Depending on your hosting provider's setup, you may |
| 130 |
need to use a graphical or command line [ftp], [ssh] or [scp] client to transfer |
| 131 |
the files. |
| 132 |
|
| 133 |
For example, a typical set of commands from the command line might look |
| 134 |
something like this: |
| 135 |
|
| 136 |
```sh |
| 137 |
mkdocs build |
| 138 |
scp -r ./site user@host:/path/to/server/root |
| 139 |
``` |
| 140 |
|
| 141 |
Of course, you will need to replace `user` with the username you have with your |
| 142 |
hosting provider and `host` with the appropriate domain name. Additionally, you |
| 143 |
will need to adjust the `/path/to/server/root` to match the configuration of |
| 144 |
your hosts' file system. |
| 145 |
|
| 146 |
[ftp]: https://en.wikipedia.org/wiki/File_Transfer_Protocol |
| 147 |
[ssh]: https://en.wikipedia.org/wiki/Secure_Shell |
| 148 |
[scp]: https://en.wikipedia.org/wiki/Secure_copy |
| 149 |
|
| 150 |
See your host's documentation for specifics. You will likely want to search |
| 151 |
their documentation for "ftp" or "uploading site". |
| 152 |
|
| 153 |
## Local Files |
| 154 |
|
| 155 |
Rather than hosting your documentation on a server, you may instead distribute |
| 156 |
the files directly, which can then be viewed in a browser using the `file://` |
| 157 |
scheme. |
| 158 |
|
| 159 |
Note that, due to the security settings of all modern browsers, some things |
| 160 |
will not work the same and some features may not work at all. In fact, a few |
| 161 |
settings will need to be customized in very specific ways. |
| 162 |
|
| 163 |
- [site_url]: |
| 164 |
|
| 165 |
The `site_url` must be set to an empty string, which instructs MkDocs to |
| 166 |
build your site so that it will work with the `file://` scheme. |
| 167 |
|
| 168 |
```yaml |
| 169 |
site_url: "" |
| 170 |
``` |
| 171 |
|
| 172 |
- [use_directory_urls]: |
| 173 |
|
| 174 |
Set `use_directory_urls` to `false`. Otherwise, internal links between |
| 175 |
pages will not work properly. |
| 176 |
|
| 177 |
```yaml |
| 178 |
use_directory_urls: false |
| 179 |
``` |
| 180 |
|
| 181 |
- [search]: |
| 182 |
|
| 183 |
You will need to either disable the search plugin, or use a third-party |
| 184 |
search plugin which is specifically designed to work with the `file://` |
| 185 |
scheme. To disable all plugins, set the `plugins` setting to an empty list. |
| 186 |
|
| 187 |
```yaml |
| 188 |
plugins: [] |
| 189 |
``` |
| 190 |
|
| 191 |
If you have other plugins enabled, simply ensure that `search` is not |
| 192 |
included in the list. |
| 193 |
|
| 194 |
When writing your documentation, it is imperative that all internal links use |
| 195 |
relative URLs as [documented][internal links]. Remember, each reader of your |
| 196 |
documentation will be using a different device and the files will likely be in a |
| 197 |
different location on that device. |
| 198 |
|
| 199 |
If you expect your documentation to be viewed off-line, you may also need to be |
| 200 |
careful about which themes you choose. Many themes make use of CDNs for various |
| 201 |
support files, which require a live Internet connection. You will need to choose |
| 202 |
a theme which includes all support files directly in the theme. |
| 203 |
|
| 204 |
When you build your site (using the `mkdocs build` command), all of the files |
| 205 |
are written to the directory assigned to the [site_dir] configuration option |
| 206 |
(defaults to `"site"`) in your `mkdocs.yaml` config file. Generally, you will |
| 207 |
simply need to copy the contents of that directory and distribute it to your |
| 208 |
readers. Alternatively, you may choose to use a third party tool to convert the |
| 209 |
HTML files to some other documentation format. |
| 210 |
|
| 211 |
## 404 Pages |
| 212 |
|
| 213 |
When MkDocs builds the documentation it will include a 404.html file in the |
| 214 |
[build directory][site_dir]. This file will be automatically used when |
| 215 |
deploying to [GitHub](#github-pages) but only on a custom domain. Other web |
| 216 |
servers may be configured to use it but the feature won't always be available. |
| 217 |
See the documentation for your server of choice for more information. |
| 218 |
|
| 219 |
[site_dir]: ./configuration.md#site_dir |
| 220 |
[site_url]: ./configuration.md#site_url |
| 221 |
[use_directory_urls]: ./configuration.md#use_directory_urls |
| 222 |
[search]: ./configuration.md#search |
| 223 |
[internal links]: ./writing-your-docs.md#internal-links |