GitHub Pages#
GitHub Pages hosts the generated site for free. It serves static files straight from a Git repository, so there is no server to run. This page covers creating the Pages repository, publishing the build to it without leaving stale pages behind, and using a custom domain. GitHub’s own quickstart covers the same ground in more general terms.
Create the Pages repository#
On GitHub, create a public repository named
<username>.github.io, where<username>is your GitHub user name. This special name makes the site available athttps://<username>.github.io.Clone it next to the folder that holds the site source, so that it can be reached as
..\<username>.github.io(or../<username>.github.io) fromdocs. The commands below assume this layout:projects/ ├── docs/ # source repository: .rst files and conf.py └── <username>.github.io/ # Pages repository: generated HTML only
In the repository’s Settings > Pages, set the source to Deploy from a branch, choose the
mainbranch and the/ (root)folder.
Why .nojekyll matters#
By default GitHub Pages runs every site through Jekyll, which ignores folders that start
with an underscore. Sphinx writes _static and _sources, so without a .nojekyll file in the root the site
loses its styles. The sphinx.ext.githubpages extension in conf.py creates that file on every build, so there is
nothing to do except keep the extension enabled.
Publish#
Publishing means making the Pages repository match _build/html, then committing and pushing. Use a mirror
copy, not a plain copy. A plain copy never deletes files, so a page that you renamed or removed stays live on the
public site. This happened when this section moved from projects/docs to projects/personal_website: the old
pages stayed online.
robocopy _build\html ..\<username>.github.io /MIR /XD .git
git -C ..\<username>.github.io add --all
git -C ..\<username>.github.io commit -m "Publish site from local build"
git -C ..\<username>.github.io push origin main
robocopy exit codes below 8 mean success, even though some of them are not zero.
rsync -a --delete --exclude .git _build/html/ ../<username>.github.io/
git -C ../<username>.github.io add --all
git -C ../<username>.github.io commit -m "Publish site from local build"
git -C ../<username>.github.io push origin main
Warning
The mirror deletes everything in the Pages repository that is not in _build/html, apart from .git. Anything
the live site needs, such as the CNAME file, must therefore come from the source (see below). To see what would
be deleted before you commit, add /L to the robocopy command, or --dry-run to rsync.
Authenticating#
git push needs permission to write to the Pages repository. Use an SSH key, or the HTTPS sign-in that Git Credential
Manager provides. Never put an access token in the repository or on a command line that you save.
Custom domain#
To serve the site from your own domain instead of <username>.github.io:
Put the domain, and nothing else, in
source/_extra/CNAME, for exampleexample.com. The linehtml_extra_path = ["_extra"]inconf.pycopies it to the root of every build, which keeps it through mirror publishes.Set
blog_baseurlinconf.pyto the same address, for example"https://example.com/".At your domain registrar, add the DNS records that point the domain at GitHub Pages.
In Settings > Pages, enter the custom domain and turn on Enforce HTTPS once it is available.
GitHub explains the DNS records in configuring a custom domain. If you have no custom
domain, delete source/_extra/CNAME.
Alternative: ablog deploy#
ABlog can copy, commit and push in one command. Set the values for your own accounts and folders:
set GITHUB_PAGES=<username>
set WEBSITE=_build/html
set REPODIR=<path to the local Pages repository>
set GITHUB_BRANCH=main
ablog deploy -w %WEBSITE% -p %REPODIR% -g %GITHUB_PAGES% --github-branch %GITHUB_BRANCH% --github-ssh
--github-ssh pushes over SSH. --github-token also works, but read the token from an environment variable that
you set outside the site source, and never commit it.
Note
On Windows, ablog deploy can fail with “command line is too long”. The workaround is a local patch around
line 435 of ablog/commands.py in the virtual environment’s site-packages, which has to be reapplied after
each ABlog upgrade. Copying the build yourself, as above, avoids the problem.