This site is built with Hugo and hosted on a server I manage. I used to upload it by hand with FileZilla/SFTP, which was pretty gross in hindsight.

Now when I merge into the live branch, GitLab builds the site and my server downloads the files and the site switches over to them all at once. Each release lines up with one commit, and I can undo one with a single command.

This is kinda overcomplicated for a personal static site. There are waaaay easier methods of getting a little site like this online. GitLab Pages can publish a static site straight from a repository with no server of mine involved. But I wanted to force myself to build the whole thing so I could apply the Linux and CI/CD topics I’m studying right now to something I actually use.

From a merge to the site

  1. I make a change on a work branch (for example,release/gta6) and open a merge request into main. GitLab builds every merge request.
  2. When I want to publish, I merge main into live.
  3. GitLab builds that commit in a container and saves the result as a job artifact, which is just the saved output of a CI job.
  4. My server checks for a new build on a timer, downloads it if there’s a new one, and switches the site to it.

main and live are protected branches, so nobody can push to them directly or force-push. GitLab also won’t merge anything unless the build passes. I’m the only one working on this, but merge requests still give me one place to read over the whole diff and check the build before anything goes live.

Building

The Hugo version is pinned in the repository, and the build script stops if a different version is installed. The build job downloads that exact release and checks it against a SHA-256 hash I also keep in the repository, so if the download ever gets swapped for something else, the build fails before anything reaches the site. The container image the build runs in is pinned by digest too, which I update manually.

After each build, a small Python check makes sure the main pages exist and that nothing from the repository itself, like notes, scripts, or hidden files, ended up in the public output. It also rejects symlinks, so a release can’t contain a link pointing somewhere else on the server. The source repository is private, and this check is what keeps private notes from being published by accident. Drafts never make it this far anyway, since the production build skips them.

Merge requests also get a preview build that includes drafts. Those builds stay inside the private project, where only members with the Developer role or higher can download them, and GitLab deletes them after seven days.

Getting the build onto the server

GitLab never connects to my server, and nothing from the pipeline runs there. Instead, a systemd timer on the server checks whether live has a new successful build, and if it does, it downloads that build like any other file.

The server reads from GitLab with a token from a separate bot account that’s a member of this one project and nothing else. The token only has the read_api scope, so it can read the project but can’t push code, change settings or start pipelines. It also expires within a year, so if I ever forget about a copy of it somewhere, it stops working on its own.

One of the CI risks OWASP lists is poisoned pipeline execution, where someone with access to the repository edits the pipeline to run their own commands. My server only takes finished builds from the protected live branch, and only as files, so even an edited pipeline has nothing it can run on my server.

The download runs as an unprivileged account that has ownership of the site’s release folders and nothing else, inside a systemd sandbox. The rest of the filesystem is read-only to it, home folders are hidden, and it can’t gain new privileges. Before anything goes live, it unpacks only the site folder from the build, refuses any links or paths that point outside it, and runs the same output check as the build.

Switching releases

Each release gets its own folder, and the web server serves whichever one a current link points to. The build gets unpacked into a temporary folder first, and before switching, the server asks GitLab where live points now. If I merged again in the meantime, it skips the older build and grabs the newer one on the next check.

Then it renames the finished folder into place and swaps the current link with one rename. On Linux, a rename on the same filesystem replaces the old link atomically, so there’s never a moment where current is missing, and each request gets either the whole old release or the whole new one. Someone clicking around during the switch could still load one page from the old release and the next one from the new release, though.

A lock makes sure only one publish or rollback happens at a time, and each one gets written to a log. Old releases stay on the server, so rolling back is just the same link swap in the other direction. The timer reads that log too, so after a rollback it waits for my next merge instead of putting the bad release right back.

Manual publishing (just in case)

I still have a script on my computer that can build and publish over SSH, for when GitLab is down. It refuses to run with uncommitted changes, so every release still matches one commit.

The upload uses rsync with --link-dest, which turns files that didn’t change into hard links to the previous release. Only the changed files get sent over the network, and the unchanged ones keep their old timestamps, so browsers keep using their cached copies. The script and the timer write to the same release folders and the same log, so either one can roll back a release the other one made.

HTTPS and headers

Plain HTTP and www redirect to https://jimlarck.com. The site also sends a few response headers:

  • Strict Transport Security tells browsers to only use HTTPS for this site.
  • A Content Security Policy only lets pages load scripts, styles, and other files from this site. Inline scripts, plugins, form submissions, and other sites framing these pages are all blocked. The Coin Crunch player gets its own, looser policy.
  • X-Content-Type-Options: nosniff makes browsers check a file’s declared type before running it as a script or stylesheet.
  • The referrer policy gives other sites only my domain, not the full address of the page a visitor came from.
  • A permissions policy turns off camera, microphone, location, payment, and USB access.

Sources