Building docs on GitHub

To host on GitHub Pages, you configuring the settings in the project (Settings -> Pages -> Source branch) to a branch. Thereafter, any content in that branch is automatically hosted at https://<user>.github.io/<project>.

A common pattern is to maintain a separate branch called gh-pages, and keep it completely cleaned out except for the built HTML documentation (with index.html as the top level).

This template documentation uses GitHub Actions to build the docs and push to gh-pages. If you’re going to be using the GitHub Actions config, you’ll need to create that branch manually first:

# Assuming there's nothing in your repo yet.
git checkout -b gh-pages

# If you have anything commited, use `git rm *` to completely
# clean out this branch.

# Then assuming your remote is on GitHub
git push origin gh-pages

# Get back to the main branch
git checkout main

This repo uses GitHub Actions to set up an environment, build the docs, and add to the gh-pages branch, which is automatically served at the URL https://<user>.github.io/<project>, which in this case is https://nichd-bspc.github.io/documentation-template.

The configuration is in .github/workflows/main.yml. It’s generic, so as long as you use gh-pages and have the source directory separate from build, it should work.

This GitHub Actions configuration will:

  • install conda and any dependencies in requirements.txt

  • run doctests

  • build the docs

  • commit them to the gh-pages branch

  • upload them as an artifact in the Actions web interface so you can inspect

From the Actions tab of the GitHub interface, you can click on a job and look for the Artifacts section to download the built docs. This can be nice for contributors to see the results of their changes without needing to set everything up locally.

It will do those steps on every push. If we’re on the main branch, then the docs will also be pushed to the gh-pages branch, at which time they will be publicly available at https://<user>.github.io/<project>.

Note

Note that GitHub by default expects pages generated by Jekyll. You need to add a file at the top level of your repo, .nojekyll, to tell GitHub to not expect Jekyll-generated docs. The GitHub Actions for this repo take care of this.

.github/workflows/main.yml

You can find the configuration in this repository under .github/workflows/main.yml, and it is included directly in this documentation here:

 1# This GitHub Actions configuration will install conda, run doctests, build the
 2# docs, commit them to the gh-pages branch, upload them as an artifact so you
 3# can inspect. It will do this on every push. If we're on the `main` branch,
 4# then the docs will be pushed to the gh-pages branch, at which time they will
 5# be publicly available at https://<user>.github.io/<project>.
 6#
 7# Note: there are actions that can be used to wrap up some of this
 8# configuration. But I find it more useful to do thing the "long way" because
 9# it's easier to customize later when needed.
10
11
12# This name will show up in the Actions interface.
13name: main
14
15# Run everything here on every push to the repo.
16on: [push]
17
18jobs:
19
20  build-and-test:
21
22    # This is the base image we'll be using. It's Ubuntu plus conveniently
23    # installed extras like conda.
24    runs-on: "ubuntu-latest"
25    steps:
26
27    # All our steps will need the code, so we use this action to check out our
28    # repo for us
29    - uses: actions/checkout@v2
30
31    # Set up conda environment for subsequent jobs. If you need other packages,
32    # add them to requirements.txt
33    - name: build env
34      run: |  # Note the "|" which indicates "a multiline string follows"
35
36        # The base GitHub Ubuntu image has conda already installed.
37        # The eval call does the equivalent of "conda init bash" and we will
38        # use it at the beginning of subsequent steps that need to use conda.
39        eval "$(conda shell.bash hook)"
40        conda create -p ./env --file requirements.txt --channel conda-forge
41
42    # Run doctests (delete this if you are not using doctests)
43    - name: run doctests
44      run: |
45        eval "$(conda shell.bash hook)"
46        conda activate ./env
47        (cd doc && make doctest)
48
49    # Build docs here, and copy them over to a temp directory that is a clone
50    # of the gh-pages branch.
51    - name: build docs
52      run: |
53        # Activate env and build docs
54        eval "$(conda shell.bash hook)"
55        conda activate ./env
56        (cd doc && make html)
57
58        # Clone just the gh-pages branch to a temp dir.
59        git clone \
60          --single-branch \
61          --branch gh-pages "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY" \
62          /tmp/doc
63
64        # Clean out that temp dir because we're about to move our just-built
65        # docs there
66        rm -rf /tmp/doc/*
67        cp -r doc/build/html/* /tmp/doc
68
69        # GitHub defaults to assuming Jekyll pages; the presence of this file
70        # makes Sphinx pages work properly with GitHub
71        touch /tmp/doc/.nojekyll
72
73    # Upload the built docs as an artifact for inspection (even on PRs). This
74    # will show up in the Actions web interface.
75    - name: push artifact
76      uses: actions/upload-artifact@v2
77      with:
78        name: doc
79        path: /tmp/doc
80
81    # Push docs to gh-pages but ONLY if this test is running on master branch
82    - name: push docs to gh-pages branch
83      if: ${{ (github.ref == 'refs/heads/main') }}
84      run: |
85
86        # Commit our changes to the gh-pages branch
87        cd /tmp/doc
88        git config --global user.email "action@github.com"
89        git config --global user.name "GitHub Action"
90        git add .
91
92        # git commit exits nonzero if nothing to commit, but we don't want to
93        # consider that an error so we ignore it.
94        git commit -m 'update docs' -a || true
95
96        # Push the gh-pages branch. Once this happens, your changes will be
97        # live (though could take a couple mins to show up)
98        git push "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY" gh-pages