| 1 |
# Running `mdbook` in continuous integration |
| 2 |
|
| 3 |
There are a variety of services such as [GitHub Actions] or [GitLab CI/CD] which can be used to test and deploy your book automatically. |
| 4 |
|
| 5 |
The following provides some general guidelines on how to configure your service to run mdBook. |
| 6 |
Specific recipes can be found at the [Automated Deployment] wiki page. |
| 7 |
|
| 8 |
[GitHub Actions]: https://docs.github.com/en/actions |
| 9 |
[GitLab CI/CD]: https://docs.gitlab.com/ee/ci/ |
| 10 |
[Automated Deployment]: https://github.com/rust-lang/mdBook/wiki/Automated-Deployment |
| 11 |
|
| 12 |
## Installing mdBook |
| 13 |
|
| 14 |
There are several different strategies for installing mdBook. |
| 15 |
The particular method depends on your needs and preferences. |
| 16 |
|
| 17 |
### Pre-compiled binaries |
| 18 |
|
| 19 |
Perhaps the easiest method is to use the pre-compiled binaries found on the [GitHub Releases page][releases]. |
| 20 |
A simple approach would be to use the popular `curl` CLI tool to download the executable: |
| 21 |
|
| 22 |
```sh |
| 23 |
mkdir bin |
| 24 |
curl -sSL https://github.com/rust-lang/mdBook/releases/download/v{{ mdbook-version }}/mdbook-v{{ mdbook-version }}-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=bin |
| 25 |
bin/mdbook build |
| 26 |
``` |
| 27 |
|
| 28 |
Some considerations for this approach: |
| 29 |
|
| 30 |
* This is relatively fast, and does not necessarily require dealing with caching. |
| 31 |
* This does not require installing Rust. |
| 32 |
* Specifying a specific URL means you have to manually update your script to get a new version. |
| 33 |
This may be a benefit if you want to lock to a specific version. |
| 34 |
However, some users prefer to automatically get a newer version when they are published. |
| 35 |
* You are reliant on the GitHub CDN being available. |
| 36 |
|
| 37 |
[releases]: https://github.com/rust-lang/mdBook/releases |
| 38 |
|
| 39 |
### Building from source |
| 40 |
|
| 41 |
Building from source will require having Rust installed. |
| 42 |
Some services have Rust pre-installed, but if your service does not, you will need to add a step to install it. |
| 43 |
|
| 44 |
After Rust is installed, `cargo install` can be used to build and install mdBook. |
| 45 |
We recommend using a SemVer version specifier so that you get the latest **non-breaking** version of mdBook. |
| 46 |
For example: |
| 47 |
|
| 48 |
```sh |
| 49 |
cargo install mdbook --no-default-features --features search --vers "^{{ mdbook-version }}" --locked |
| 50 |
``` |
| 51 |
|
| 52 |
This includes several recommended options: |
| 53 |
|
| 54 |
* `--no-default-features` --- Disables features like the HTTP server used by `mdbook serve` that is likely not needed on CI. |
| 55 |
This will speed up the build time significantly. |
| 56 |
* `--features search` --- Disabling default features means you should then manually enable features that you want, such as the built-in [search] capability. |
| 57 |
* `--vers "^{{ mdbook-version }}"` --- This will install the most recent version of the `{{ mdbook-semver }}` version series. |
| 58 |
However, semver-incompatible versions after like `{{ mdbook-semver-break }}` won't be installed, as they may break your build. |
| 59 |
Cargo will automatically upgrade mdBook if you have an older version already installed. |
| 60 |
If you want to stick to a specific version of mdBook, replace the `^` with an `=`. |
| 61 |
* `--locked` --- This will use the dependencies that were used when mdBook was released. |
| 62 |
Without `--locked`, it will use the latest version of all dependencies, which may include some fixes since the last release, but may also (rarely) cause build problems. |
| 63 |
|
| 64 |
You will likely want to investigate caching options, as building mdBook can be somewhat slow. |
| 65 |
|
| 66 |
[search]: guide/reading.md#search |
| 67 |
|
| 68 |
## Running tests |
| 69 |
|
| 70 |
You may want to run tests using [`mdbook test`] every time you push a change or create a pull request. |
| 71 |
This can be used to validate Rust code examples in the book. |
| 72 |
|
| 73 |
This will require having Rust installed. |
| 74 |
Some services have Rust pre-installed, but if your service does not, you will need to add a step to install it. |
| 75 |
|
| 76 |
Other than making sure the appropriate version of Rust is installed, there's not much more than just running `mdbook test` from the book directory. |
| 77 |
|
| 78 |
You may also want to consider running other kinds of tests, like [mdbook-linkcheck2] which will check for broken links. |
| 79 |
Or if you have your own style checks, spell checker, or any other tests it might be good to run them in CI. |
| 80 |
|
| 81 |
[`mdbook test`]: cli/test.md |
| 82 |
[mdbook-linkcheck2]: https://github.com/marxin/mdbook-linkcheck2#continuous-integration |
| 83 |
|
| 84 |
## Deploying |
| 85 |
|
| 86 |
You may want to automatically deploy your book. |
| 87 |
Some may want to do this every time a change is pushed, and others may want to only deploy when a specific release is tagged. |
| 88 |
|
| 89 |
You'll also need to understand the specifics on how to push a change to your web service. |
| 90 |
For example, [GitHub Pages] just requires committing the output onto a specific git branch. |
| 91 |
Other services may require using something like SSH to connect to a remote server. |
| 92 |
|
| 93 |
The basic outline is that you need to run `mdbook build` to generate the output, and then transfer the files (which are in the `book` directory) to the correct location. |
| 94 |
|
| 95 |
You may then want to consider if you need to invalidate any caches on your web service. |
| 96 |
|
| 97 |
See the [Automated Deployment] wiki page for examples of various different services. |
| 98 |
|
| 99 |
[GitHub Pages]: https://docs.github.com/en/pages |
| 100 |
|
| 101 |
### 404 handling |
| 102 |
|
| 103 |
mdBook automatically generates a 404 page to be used for broken links. |
| 104 |
The default output is a file named `404.html` at the root of the book. |
| 105 |
Some services like [GitHub Pages] will automatically use this page for broken links. |
| 106 |
For other services, you may want to consider configuring the web server to use this page as it will provide the reader navigation to get back to the book. |
| 107 |
|
| 108 |
If your book is not deployed at the root of the domain, then you should set the [`output.html.site-url`] setting so that the 404 page works correctly. |
| 109 |
It needs to know where the book is deployed in order to load the static files (like CSS) correctly. |
| 110 |
For example, this guide is deployed at <https://rust-lang.github.io/mdBook/>, and the `site-url` setting is configured like this: |
| 111 |
|
| 112 |
```toml |
| 113 |
# book.toml |
| 114 |
[output.html] |
| 115 |
site-url = "/mdBook/" |
| 116 |
``` |
| 117 |
|
| 118 |
You can customize the look of the 404 page by creating a file named `src/404.md` in your book. |
| 119 |
If you want to use a different filename, you can set [`output.html.input-404`] to a different filename. |
| 120 |
|
| 121 |
[`output.html.site-url`]: format/configuration/renderers.md#html-renderer-options |
| 122 |
[`output.html.input-404`]: format/configuration/renderers.md#html-renderer-options |