Running mdbook in Continuous Integration

While the following examples use Travis CI, their principles should straightforwardly transfer to other continuous integration providers as well.

Ensuring Your Book Builds and Tests Pass

Here is a sample Travis CI .travis.yml configuration that ensures mdbook build and mdbook test run successfully. The key to fast CI turnaround times is caching mdbook installs, so that you aren’t compiling mdbook on every CI run.

  1. language: rust
  2. sudo: false
  3. cache:
  4. - cargo
  5. rust:
  6. - stable
  7. before_script:
  8. - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update)
  9. - (test -x $HOME/.cargo/bin/mdbook || cargo install --vers "^0.3" mdbook)
  10. - cargo install-update -a
  11. script:
  12. - mdbook build path/to/mybook && mdbook test path/to/mybook

Deploying Your Book to GitHub Pages

Following these instructions will result in your book being published to GitHub pages after a successful CI run on your repository’s master branch.

First, create a new GitHub “Personal Access Token” with the “public_repo” permissions (or “repo” for private repositories). Go to your repository’s Travis CI settings page and add an environment variable named GITHUB_TOKEN that is marked secure and not shown in the logs.

Then, append this snippet to your .travis.yml and update the path to the book directory:

  1. deploy:
  2. provider: pages
  3. skip-cleanup: true
  4. github-token: $GITHUB_TOKEN
  5. local-dir: path/to/mybook/book
  6. keep-history: false
  7. on:
  8. branch: master

That’s it!

Deploying to GitHub Pages manually

If your CI doesn’t support GitHub pages, or you’re deploying somewhere else with integrations such as Github Pages: note: you may want to use different tmp dirs:

  1. $> git worktree add /tmp/book gh-pages
  2. $> mdbook build
  3. $> rm -rf /tmp/book/* # this won't delete the .git directory
  4. $> cp -rp book/* /tmp/book/
  5. $> cd /tmp/book
  6. $> git add -A
  7. $> git commit 'new book message'
  8. $> git push origin gh-pages
  9. $> cd -

Or put this into a Makefile rule:

  1. .PHONY: deploy
  2. deploy: book
  3. @echo "====> deploying to github"
  4. git worktree add /tmp/book gh-pages
  5. rm -rf /tmp/book/*
  6. cp -rp book/* /tmp/book/
  7. cd /tmp/book && \
  8. git add -A && \
  9. git commit -m "deployed on $(shell date) by ${USER}" && \
  10. git push origin gh-pages