Back

Self-Hosted CI Needs a Janitor

By Pablo Fernandez8 min read

GitHub's documentation describes its hosted runners in one line that is easy to skim past: "each GitHub-hosted runner is a new virtual machine (VM) hosted by GitHub". A job gets a machine, does its work, and the machine is gone. Whatever the job downloaded, built, cached or left running goes with it. Nobody has to clean anything, because there's nothing left to clean.

On October 22 I moved my deploys off those machines and onto my own: a server at OVH in Warsaw that runs the GitHub Actions jobs for this site, for Mailingest and for a couple of other projects. The reasons were good ones, and I'll get to them. What I didn't think through was the other side of that one line. A machine that isn't thrown away remembers everything, and something has to decide what to forget. Three times this autumn, a deploy broke on something that was remembered and that nobody owned.

Why my own runners

The runners are ephemeral, which is GitHub's own recommendation: each one is a fresh container that registers with GitHub, takes exactly one job and is removed, and systemd starts the next. So the job itself always starts clean. What survives is outside the container, on purpose: the Rust compile cache (sccache), the cargo registry, and Docker's BuildKit layers all live on the host, so a Mailingest build picks up where the last one stopped instead of compiling from nothing. And no hosted minutes are billed.

That's the trade in one sentence. The caches that make it fast are state, and state on a machine that never gets thrown away doesn't leave unless something removes it.

A runner that died without saying goodbye

Each runner slot is a systemd service. When I restarted one (systemctl restart github-runner@pablofrcom-1), systemd killed its container, the slot script ran docker rm -f on any leftover, and a new container came up and registered. From the server's side, everything was fine.

From GitHub's side, a runner had simply stopped answering. GitHub's documentation is precise about when an ephemeral runner goes away: the service will "automatically de-register the runner after it has processed one job". This one never processed its job. It was killed while holding it. GitHub kept the registration, marked it offline, and kept the job it had already assigned to it. That job stayed queued, for a runner that no longer existed, and gh run cancel, forced or not, wouldn't touch it.

before restart ovh-waw-pablofrcom-1-<t1> online job assignedrestart container killed, never deregisteredafter restart ovh-waw-pablofrcom-1-<t1> offline job still queued ovh-waw-pablofrcom-1-<t2> online waiting, idlethe fix DELETE runner <t1> ──▶ job goes to <t2>
The restart killed the runner but not its registration; the job stayed with the dead one until that registration was deleted.

The registration flag that should prevent duplicates didn't help, and the reason is in my own entrypoint. The runner registers with --replace, which replaces a runner of the same name, but the name ends in a timestamp (ovh-waw-<slot>-$(date +%s)), so every container is a new name and nothing is ever replaced. The fix was the REST API: list the repository's runners, delete the ones marked offline, and the queued job goes to the next runner that asks.

That fix is still a command I run by hand, not code. Nothing on the server deletes offline registrations today, so the honest rule is a sentence in my notes: don't restart a slot while it holds a job, and if you do, delete the offline runner.

A disk that remembers every build

The second leftover was bigger, in the literal sense. The OVH host has a 96 GB disk, and BuildKit keeps a cache of layers so the next image build can reuse them. How much it keeps is one number, and that number moved three times.

It started at 10 GB. Mailingest's release image includes a long Rust compile, and at 10 GB that compile kept getting evicted between builds, so every image was built from scratch, about 11 minutes each. On October 29 the cap went to 25 GB, and builds got fast again. On November 9 a full Mailingest rebuild filled the 96 GB disk, twice, and the cap came back down.

CapBefore November 9After
BuildKit cache kept25 GB14 GB
BuildKit cache when the disk is high12 GB8 GB
One job workspace's target/20 GB16 GB
Prune harder once the disk passes80 %75 %

Those caps are enforced by a script I call, without much imagination, the janitor. It runs every 10 minutes from a systemd timer, and its header is a list of everything a job can leave behind on this host: per-run NATS and ClickHouse containers from Mailingest's tests, BuildKit builders, service containers like Postgres older than two hours, images no container uses, workspaces idle for 10 days, cargo registries past 4 GB. GitHub stops a job's containers when the job ends cleanly, the comment says, "not when its runner is killed or the host reboots". Every line of that list is something a hosted VM would have taken with it.

The janitor also has a rule for what it must never touch: a runner container, or anything a running job is using. That rule is most of the code. Deleting things is easy; knowing that nobody needs them anymore is the actual job.

The disk wasn't even the first resource to run out. Memory was, on the same October 29: three heavy Mailingest jobs at once filled the host's 11 GB, and twice that night it stopped answering even ping and had to be rebooted through OVH's API. It now has a memory ceiling for the jobs, 8 GB of swap, two Mailingest slots instead of three, and heavy jobs from different repositories take turns.

An image deleted under a running container

The third one came first, in October, and not on the CI host but on the VPS the deploys go to. It's the same mistake from the other side: a machine that keeps state, and two parts of it that disagreed about who owned a piece of it.

On October 9 the VPS moved to Ubuntu 26.04, and Docker there now uses the containerd image store. My infrastructure repo still deployed three of its own images (a Postgres build and two others) by rebuilding them on the VPS with docker compose up -d --build. Under the new store, every rebuild produced a new image ID, even when every layer came from cache, because the build's attestations carry a timestamp. Compose saw nothing to change and left the running container alone. Docker saw an old image with no tag pointing at it and deleted it. The container kept running, on an image that no longer existed.

The container was fine. Watchtower, which checks every hour (that's my setting, WATCHTOWER_POLL_INTERVAL: 3600) whether a container's image has a newer version, was not: it emailed me "Failed to retrieve image info" every hour, because there was no image info left to retrieve.

before VPS: compose up -d --build ──▶ new image ID, same layers ──▶ container keeps the old ID, Docker deletes it ──▶ Watchtower: "Failed to retrieve image info"after CI builds ──▶ registry ──▶ VPS pulls, up -d, no --build
Building on the VPS left containers running on images Docker had already deleted; building in CI leaves the VPS nothing to rebuild.

The fix, on October 15, was to stop building on the VPS at all. Those images are now built in CI, pushed to my own registry, and the VPS only pulls them, which is how this site's images already worked. The compose files keep their build: sections as a fallback for the day the registry itself is down.

The registry being the source of truth had already bitten me once, five days earlier, the other way round. I deployed by building an image on the VPS and starting it, without pushing it. Watchtower, doing exactly its job, saw that the container didn't match the registry and put the registry's image back: an older build, missing a file the new code needed. The API went into a crash loop, and the day's web deploys were quietly rolled back with it. That one ended up in bold in the deploy notes: every image built anywhere gets pushed, or Watchtower will undo it.

Who forgets

Each of the three was a piece of state that outlived the thing it described. A registration outlived its runner. A cache outlived the builds that needed it. A running container outlived its image, and a local build outlived its chance to be the truth. On a hosted runner, none of them could have happened, because the machine that held them would have been gone within the hour.

That's what the minutes on a hosted runner pay for, more than the CPU: someone else's machine forgets everything for you. Running my own was still the right call. Mailingest builds are incremental now, and nothing is billed per minute. But the bill moved instead of disappearing, and it's paid in a janitor script, a registry that is the only truth, and one command I still type by hand. Every piece of state on a machine you keep needs an owner whose job is to delete it.

Related articles

Turning a VPS into a Platform

Why I encoded a whole VPS as one Go CLI, pfrctl, instead of renting a PaaS or running a heavy self-hosted panel, and where that trade-off pays off.

Skew Protection for Next.js

Every deploy strands open tabs asking for chunks only the old build had. My skew protection for self-hosted Next.js on k3s: a Go router that pins each tab.

Search a Table in Plain Words

Type a sentence into a table's search box and get its filters back as chips, the first in a third of a second, from a model that only picks real values.