|
| 1 | +.. _github_pages: |
| 2 | + |
| 3 | +============ |
| 4 | +GitHub Pages |
| 5 | +============ |
| 6 | + |
| 7 | +It's pretty easy to use `GitHub Pages <https://pages.github.com/>`_ to host all of your versioned documentation. Before |
| 8 | +starting be sure to go through the :ref:`tutorial` first to make sure you can build your docs locally. You'll also want |
| 9 | +to do the :ref:`push-all-versions` section. |
| 10 | + |
| 11 | +.. tip:: |
| 12 | + |
| 13 | + You may want to enable GitHub's `protected branches <https://help.github.com/articles/about-protected-branches/>`_ |
| 14 | + feature for the gh-pages branch to prevent you or anyone from accidentally deleting the branch from remote. |
| 15 | + |
| 16 | +This guide assumes: |
| 17 | + |
| 18 | +1. You already have documentation in your master branch and SCVersioning builds it locally. If not you'll need to use |
| 19 | + the :option:`--root-ref` argument. |
| 20 | +2. You already have your CI configured and running on your repo (this guide uses Travis CI but it should work on any |
| 21 | + other). |
| 22 | + |
| 23 | +Turn Off Jekyll |
| 24 | +=============== |
| 25 | + |
| 26 | +Building on the steps in the :ref:`tutorial` document you'll want to disable Jekyll because it doesn't copy over files |
| 27 | +and directories `that start with underscores <https://github.com/blog/572-bypassing-jekyll-on-github-pages>`_, which |
| 28 | +Sphinx uses. |
| 29 | + |
| 30 | +.. code-block:: bash |
| 31 | +
|
| 32 | + git checkout gh-pages |
| 33 | + git pull origin gh-pages |
| 34 | + touch .nojekyll |
| 35 | + git add .nojekyll |
| 36 | + git commit |
| 37 | + git push origin gh-pages |
| 38 | + git checkout master # Or whatever branch you were in. |
| 39 | +
|
| 40 | +Then navigate to https://username.github.io/repo_name/ and if you used ``.`` for your :option:`REL_DST` you should see |
| 41 | +your HTML docs there. Otherwise if you used something like ``html/docs`` you'll need to navigate to |
| 42 | +https://username.github.io/repo_name/html/docs/. |
| 43 | + |
| 44 | +.. tip:: |
| 45 | + |
| 46 | + Old repositories may not have **Enforce HTTPS** enabled for their GitHub Pages. It's a good idea to enable this |
| 47 | + feature. More info: https://help.github.com/articles/securing-your-github-pages-site-with-https/ |
| 48 | + |
| 49 | +Running in CI |
| 50 | +============= |
| 51 | + |
| 52 | +The goal of using GitHub Pages is to have docs automatically update on every new/changed branch/tag. In this example |
| 53 | +we'll be using Travis CI but any CI should work. |
| 54 | + |
| 55 | +Travis won't be able to push any changes to the gh-pages branch without SSH keys. This guide will worry about just |
| 56 | +getting Travis to run SCVersioning. It should only fail when trying to push to origin. |
| 57 | + |
| 58 | +CI Config File |
| 59 | +-------------- |
| 60 | + |
| 61 | +Edit your CI configuration file (e.g. `.travis.yml <https://docs.travis-ci.com/user/customizing-the-build/>`_) with: |
| 62 | + |
| 63 | +.. code-block:: yaml |
| 64 | +
|
| 65 | + install: |
| 66 | + - pip install sphinxcontrib-versioning |
| 67 | + after_success: |
| 68 | + - git config --global user.email "[email protected]" |
| 69 | + - git config --global user.name "Travis CI" |
| 70 | + - sphinx-versioning push gh-pages . docs |
| 71 | +
|
| 72 | +The two git config lines are needed to make commits locally to the gh-pages branch (cloned to a temporary directory by |
| 73 | +SCVersioning). If you want SCVersioning to delete unrelated files from the gh-pages branch (e.g. deleted branches' HTML |
| 74 | +documentation, deleted tags, etc) change the sphinx-versioning command to: |
| 75 | + |
| 76 | +.. code-block:: bash |
| 77 | +
|
| 78 | + sphinx-versioning -e .gitignore -e .nojekyll -e README.rst push gh-pages . docs |
| 79 | +
|
| 80 | +This tells SCVersioning to delete all files in gh-pages except those three. More information in :option:`--grm-exclude`. |
| 81 | + |
| 82 | +Commit |
| 83 | +------ |
| 84 | + |
| 85 | +Commit your changes to the CI config file and push. You should see documentation building successfully, but it should |
| 86 | +fail when it tries to push since we haven't given your CI any permission to make changes to the git repository. |
| 87 | + |
| 88 | +SSH Key |
| 89 | +======= |
| 90 | + |
| 91 | +Now that we know SCVersioning works fine locally and remotely it's time to unleash it. We'll be using |
| 92 | +`Deploy Keys <https://developer.github.com/guides/managing-deploy-keys/>`_ to grant Travis write access to your |
| 93 | +repository. At the time of this writing this is the most narrow-scoped authorization method for docs deployment. |
| 94 | + |
| 95 | +To avoid leaking the SSH private key (thereby granting write access to the repo) we'll be using Travis CI's |
| 96 | +`Encrypting Files <https://docs.travis-ci.com/user/encrypting-files/>`_ feature. You'll need to install the Travis CI |
| 97 | +`ruby client <https://github.com/travis-ci/travis.rb#installation>`_ for this section. |
| 98 | + |
| 99 | +ssh-keygen |
| 100 | +---------- |
| 101 | + |
| 102 | +First we'll create the SSH key pair. |
| 103 | + |
| 104 | +.. code-block:: bash |
| 105 | +
|
| 106 | + ssh-keygen -t rsa -b 4096 -C "Travis CI Deploy Key" -N "" -f docs/key |
| 107 | + cat docs/key.pub # We'll be adding this to GitHub's repo settings page. |
| 108 | + travis encrypt-file docs/key docs/key.enc --add after_success # Updates .travis.yml |
| 109 | + rm docs/key docs/key.pub # Don't need these anymore. |
| 110 | +
|
| 111 | +We need to give GitHub your SSH **public** key (the one we ran with ``cat``). Go to |
| 112 | +https://github.com/username/repo_name/settings/keys and click "Add deploy key". The title could be anything (e.g. |
| 113 | +"Travis CI Deploy Key"). The key you're pasting will be one long line and will look something like "ssh-rsa AAAAB3N...== |
| 114 | +Travis CI Deploy Key" |
| 115 | + |
| 116 | +Be sure to check **Allow write access**. |
| 117 | + |
| 118 | +travis.yml |
| 119 | +---------- |
| 120 | + |
| 121 | +The ``travis encrypt-file`` command should have updated your ``.travis.yml`` with the openssl command for you. However |
| 122 | +we still need to make one more change to the file before committing it. Update .travis.yml with these ``chmod``, |
| 123 | +``eval``, and ``git remote`` commands. The after_success section should end up looking like this: |
| 124 | + |
| 125 | +.. code-block:: bash |
| 126 | +
|
| 127 | + after_success: |
| 128 | + - touch docs/key; chmod 600 docs/key # Secure before storing key data. |
| 129 | + - openssl aes-256-cbc ... -in docs/key.enc -out docs/key -d |
| 130 | + - eval `ssh-agent -s`; ssh-add docs/key |
| 131 | + - git config --global user.email "[email protected]" |
| 132 | + - git config --global user.name "Travis CI" |
| 133 | + - git remote set-url origin "[email protected]:$TRAVIS_REPO_SLUG" |
| 134 | + - export ${!TRAVIS*} # Optional, for commit messages. |
| 135 | + - sphinx-versioning push gh-pages . docs |
| 136 | +
|
| 137 | +Finally commit both **.travis.yml** and the encrypted **docs/key.enc** file. Push and watch Travis update your docs |
| 138 | +automatically for you. |
0 commit comments