GitHub Pages provides hosting for static files by serving a branch (e.g. gh-pages) of the respective repository. GitHub Actions can be used to automate deployments, avoiding the hassle of having to update that branch manually when the main branch (typically master) changes.
The idea of using other people’s actions made me slightly uncomfortable, due to mild security concerns (handing full repo access to some unknown party) and the complexity of excessive abstraction (who wants to read documentation when they can write code instead…. ). So I set out to automate this myself – thinking it should be straightforward:
Here we run ./bin/build
(a placeholder for make
, npm start
or similar) to
generate build artifacts in the dist
directory and then commit them to the
gh-pages branch. The script relies on various
environment variables.
We can make GitHub Actions execute that script (./bin/update-gh-pages
) by
creating a workflow description (e.g. .github/workflows/pages.yml
):
Note that we pass in GITHUB_TOKEN
, which is used in the script’s repo URI to
provide read/write access.
However, turns out that repo updates using GITHUB_TOKEN
do not trigger GitHub Pages builds.
(Which was not at all obvious… )
So we need to
generate a personal access token, add it
to respective repo’s
secrets
(via Settings → Secrets; named DEPLOY_TOKEN
here) and use that instead of
GITHUB_TOKEN
:
With those adjustments, our automated repo updates finally result in GitHub Pages being published as well.
PS: In my case, the repo in question is a Node-based
application, so the workflow file includes a few
additional steps
: