|
| 1 | +.. _nfsn: |
| 2 | + |
| 3 | +==================== |
| 4 | +NearlyFreeSpeech.NET |
| 5 | +==================== |
| 6 | + |
| 7 | +This guide will go over how to host your built documentation on `NFSN <https://www.nearlyfreespeech.net/>`_. We'll be |
| 8 | +using GitHub and Travis CI to actually build the docs and push them to NFSN but any other providers can be substituted. |
| 9 | + |
| 10 | +We'll be covering two methods of having NFSN host your documentation: using ``rsync`` to transfer HTML files to NFSN and |
| 11 | +using a remote git repository hosted on NFSN using ``git init --bare`` and having a git hook export HTML files to the |
| 12 | +"/home/pubic" directory. Since NFSN's pricing structure is usage based the latter method technically costs more since |
| 13 | +the entire git history of the HTML files' git branch will be stored on NFSN, whereas in the rsync method only the HTML |
| 14 | +files are stored on NFSN. The cost difference is probably minimal but it's something to keep in mind. |
| 15 | + |
| 16 | +Before starting be sure to go through the :ref:`tutorial` first to make sure you can build your docs locally. If you're |
| 17 | +going with the ``rsync`` route you can stop after the :ref:`build-all-versions` section. Otherwise you should go through |
| 18 | +the :ref:`push-all-versions` section as well. |
| 19 | + |
| 20 | +This guide assumes: |
| 21 | + |
| 22 | +1. You already have documentation in your master branch and SCVersioning builds it locally. If not you'll need to use |
| 23 | + the :option:`--root-ref` argument. |
| 24 | +2. You already have Travis CI configured and running on your repo. |
| 25 | +3. You already have an account on NFSN. |
| 26 | + |
| 27 | +Running in CI |
| 28 | +============= |
| 29 | + |
| 30 | +Before touching NFSN let's setup Travis CI to run SCVersioning. Edit your |
| 31 | +`.travis.yml <https://docs.travis-ci.com/user/customizing-the-build/>`_ file with: |
| 32 | + |
| 33 | +.. code-block:: yaml |
| 34 | +
|
| 35 | + addons: |
| 36 | + ssh_known_hosts: ssh.phx.nearlyfreespeech.net |
| 37 | + install: |
| 38 | + - pip install sphinxcontrib-versioning |
| 39 | + after_success: |
| 40 | + - sphinx-versioning build docs docs/_build/html |
| 41 | +
|
| 42 | +Commit your changes and push. You should see documentation building successfully. |
| 43 | + |
| 44 | +SSH Key |
| 45 | +------- |
| 46 | + |
| 47 | +Now we need to create an SSH key pair and upload the private key to Travis CI. The public key will be given to NFSN in |
| 48 | +the next section. |
| 49 | + |
| 50 | +To avoid leaking the SSH private key (thereby granting write access to the repo) we'll be using Travis CI's |
| 51 | +`Encrypting Files <https://docs.travis-ci.com/user/encrypting-files/>`_ feature. You'll need to install the Travis CI |
| 52 | +`ruby client <https://github.com/travis-ci/travis.rb#installation>`_ for this section. |
| 53 | + |
| 54 | +Create the SSH key pair. |
| 55 | + |
| 56 | +.. code-block:: bash |
| 57 | +
|
| 58 | + ssh-keygen -t rsa -b 4096 -C "Travis CI Deploy Key" -N "" -f docs/key |
| 59 | + cat docs/key.pub # We'll be adding this to NFSN's Add SSH Key page. |
| 60 | + travis encrypt-file docs/key docs/key.enc --add after_success # Updates .travis.yml |
| 61 | + rm docs/key docs/key.pub # Don't need these anymore. |
| 62 | +
|
| 63 | +The ``travis encrypt-file`` command should have updated your ``.travis.yml`` with the openssl command for you. However |
| 64 | +we still need to make one more change to the file before committing it. Update .travis.yml to make the after_success |
| 65 | +section look like the following. Remember to replace **$encrypted_x_key** and **$encrypted_x_iv** with what you |
| 66 | +currently have. |
| 67 | + |
| 68 | +.. code-block:: yaml |
| 69 | +
|
| 70 | + after_success: |
| 71 | + - eval "$(ssh-agent -s)"; touch docs/key; chmod 0600 docs/key |
| 72 | + - openssl aes-256-cbc -d -K $encrypted_x_key -iv $encrypted_x_iv < docs/key.enc > docs/key |
| 73 | + && ssh-add docs/key # Use && to prevent ssh-add from prompting during pull requests. |
| 74 | + - sphinx-versioning build docs docs/_build/html |
| 75 | +
|
| 76 | +.. warning:: |
| 77 | + |
| 78 | + Always conditionally run ssh-add only if openssl succeeds like in the example above. Encrypted environment variables |
| 79 | + are not set on Travis CI and probably other CIs during pull requests for security reasons. If you always run ssh-add |
| 80 | + (which appears to be what everyone does) all of your pull requests will have failing tests because: |
| 81 | + |
| 82 | + #. Travis CI runs all commands in after_success even if one fails. |
| 83 | + #. openssl appears to copy "key.enc" to "key" when it fails to decrypt. |
| 84 | + #. ssh-add will prompt for a passphrase because it thinks the file is encrypted with an SSH passphrase. |
| 85 | + #. The Travis job will hang, timeout, and fail even if tests pass. |
| 86 | + |
| 87 | +Finally commit both **.travis.yml** and the encrypted **docs/key.enc** file. |
| 88 | + |
| 89 | +Create an NFSN Site |
| 90 | +=================== |
| 91 | + |
| 92 | +First we'll create a static site on NFSN. Even if you've been using NFSN it's a good idea to try this out in a dedicated |
| 93 | +and disposable site to avoid breaking anything important. |
| 94 | + |
| 95 | +1. Go to the **sites** tab in the member portal and click "Create a New Site". This guide will use **scversioning** as |
| 96 | + the new site's short name. |
| 97 | +2. Since this is all just static HTML files you won't need PHP/MySQL/etc. Select the "Static Content" server type. |
| 98 | +3. You should be able to visit `http://scversioning.nfshost.com/` and get an HTTP 403 error. |
| 99 | +4. Go to the **profile** tab and click "Add SSH Key". The key you're pasting will be one long line and will look |
| 100 | + something like "ssh-rsa AAAAB3N...== Travis CI Deploy Key" |
| 101 | + |
| 102 | +Pushing From CI to NFSN |
| 103 | +======================= |
| 104 | + |
| 105 | +This is the moment of truth. You need to decide if you want to just rsync HTML files from Travis CI to NFSN or add NFSN |
| 106 | +as a git remote, have SCVersioning push to NFSN, and let a git hook on NFSN move HTML files to the web root. |
| 107 | + |
| 108 | +Using Rsync |
| 109 | +----------- |
| 110 | + |
| 111 | +This is simpler and costs less (though probably not by much since NFSN is pretty cheap). All you need to do is add these |
| 112 | +lines to your .travis.yml file's ``after_success`` section. Be sure to replace username_scversioning with your |
| 113 | +actual username and remove the previous sphinx-versioning line. |
| 114 | + |
| 115 | +.. code-block:: yaml |
| 116 | +
|
| 117 | + - export [email protected]:/home/public |
| 118 | + - sphinx-versioning build docs docs/_build/html && rsync -icrz --delete docs/_build/html/ $destination |
| 119 | +
|
| 120 | +We're adding rsync to the same line as sphinx-versioning because Travis CI runs all commands in after_success even if |
| 121 | +one of them fails. No point in rsyncing if sphinx-versioning fails. |
| 122 | + |
| 123 | +After committing you should see Travis CI rsync HTML files to NFSN and your site should be up and running with your |
| 124 | +documentation. |
| 125 | + |
| 126 | +Using Git Bare Repo |
| 127 | +------------------- |
| 128 | + |
| 129 | +You can take advantage of SCVersioning's git push retry logic if you go this route. Here we'll be pushing build docs to |
| 130 | +the ``nfsn-pages`` branch on the remote repo located in your NFSN's private home directory. |
| 131 | + |
| 132 | +First create the remote repo on NFSN. SSH into your new site and run these commands: |
| 133 | + |
| 134 | +.. code-block:: bash |
| 135 | +
|
| 136 | + mkdir /home/private/repo |
| 137 | + cd /home/private/repo |
| 138 | + git init --bare |
| 139 | + touch hooks/post-receive |
| 140 | + chmod +x hooks/post-receive |
| 141 | +
|
| 142 | +Next setup the post-receive git hook. Write the following to **/home/private/repo/hooks/post-receive** on NFSN: |
| 143 | + |
| 144 | +.. code-block:: bash |
| 145 | +
|
| 146 | + # !/bin/bash |
| 147 | + export GIT_WORK_TREE="/home/public" |
| 148 | + while read sha1old sha1new refname; do |
| 149 | + branch=$(git rev-parse --symbolic --abbrev-ref $refname) |
| 150 | + [ "$branch" != "nfsn-pages" ] && continue |
| 151 | + lockf -k -t5 /home/tmp/nfsn_pages.lock git checkout -f $branch |
| 152 | + done |
| 153 | +
|
| 154 | +Now before we move on to the final step you'll need to create the initial commit to the nfsn-pages branch on the NFSN |
| 155 | +remote. SCVersioning does not create new branches, they must previously exist on the remote. Here we'll be renaming the |
| 156 | +``gh-pages`` branch you created in :ref:`pushing-to-remote-branch` to ``nfsn-pages`` and pushing it to our new NFSN |
| 157 | +remote repo. Run these commands on your local machine (replace username_scversioning with your actual username): |
| 158 | + |
| 159 | +.. code-block:: bash |
| 160 | +
|
| 161 | + git push origin --delete gh-pages # No longer need this in origin. |
| 162 | + git checkout gh-pages |
| 163 | + git branch -m nfsn-pages |
| 164 | + git remote add nfsn "[email protected]:/home/private/repo" |
| 165 | + git push nfsn nfsn-pages |
| 166 | +
|
| 167 | +At this point you should see .gitignore and README.rst in your /home/public directory on NFSN. Finally add these lines |
| 168 | +to your .travis.yml file's ``after_success`` section. Be sure to replace username_scversioning with your actual username |
| 169 | +and remove the previous sphinx-versioning line. |
| 170 | + |
| 171 | +.. code-block:: yaml |
| 172 | +
|
| 173 | + - git config --global user.email "[email protected]" |
| 174 | + - git config --global user.name "Travis CI" |
| 175 | + - git remote add nfsn "[email protected]:/home/private/repo" |
| 176 | + - export ${!TRAVIS*} # Optional, for commit messages. |
| 177 | + - sphinx-versioning push -P nfsn docs nfsn-pages . |
| 178 | +
|
| 179 | +After committing you should see Travis CI push HTML files to NFSN and your site should be up and running with your |
| 180 | +documentation. |
0 commit comments