Sub topic 2
Ephemeral runners: one job, one clean environment
This chapter assumes you know how GitHub Actions workflows, jobs, and runs-on work. It also assumes you know what a Kubernetes pod is. You do not need to have installed ARC yet. The goal here is a working model of ephemeral runners, since the rest of the tutorial builds on it.
What "ephemeral" means
A self-hosted runner is a process that polls GitHub for work and executes jobs. A runner is ephemeral when it accepts exactly one job and then deregisters and exits. The next job gets a new runner, started from a clean image, with a clean filesystem.
The opposite is a persistent runner. A persistent runner registers once and then runs job after job until you stop it. Most hand-built runners on a VM are persistent, because that is what the default setup script produces.
| Property | Persistent runner | Ephemeral runner |
|---|---|---|
| Jobs per registration | Many | Exactly one |
| Filesystem between jobs | Shared, leftovers remain | Discarded |
| Registration lifetime | Until manually removed | Removed after the job |
| Cleanup responsibility | You, with scripts | The platform |
| Idle cost | One machine always running | Can be zero |
| Start latency | None, already running | Pod start and registration |
Why persistent runners cause trouble
Persistent runners accumulate state. Each of the following problems has been seen on real fleets.
- Cross-job contamination. A job leaves files in the workspace, in
~/.cache, or in/tmp. A later job reads them and passes when it should fail, or fails when it should pass. - Credential leakage. A step writes a token to a dotfile, a Docker config, or a Git credential helper. The next job, possibly from another workflow or branch, can read it.
- Configuration drift. A job installs a tool globally. Two weeks later, another job depends on it without declaring it. When the runner is rebuilt, the second job breaks.
- Disk exhaustion. Docker layers, build caches, and checkouts pile up until the disk fills and jobs fail for reasons unrelated to the code.
- Poisoned runners. A pull request from a compromised or malicious contributor can leave a background process or a modified binary. Every later job runs on that tainted machine.
Ephemeral runners remove these problems by construction. You do not have to remember to clean up. A fresh environment is the only kind there is.
The lifecycle of an ephemeral runner
An ephemeral runner goes through a fixed sequence. Knowing it helps you read pod states later when you troubleshoot.
- Demand appears. A workflow job is queued with a matching
runs-onlabel. - A runner is created. On Kubernetes, this is a new pod from a known image.
- The runner registers. It tells GitHub it exists and is ready.
- GitHub assigns one job. No other job can be assigned to it.
- The job runs. All steps execute inside this pod.
- The runner exits. It deregisters, and the pod is deleted along with its filesystem.
The important property is the "exactly one" at step 4. GitHub enforces it on its side. An ephemeral runner is removed from the runner list automatically once it completes a job, so a stale registration cannot take a second job even if the process misbehaves.
Ephemeral registration in practice
Outside Kubernetes, you can make a runner ephemeral with a flag at configuration time. This is useful for understanding what ARC automates.
./config.sh \
--url https://github.com/<your-org>/<your-repo> \
--token <REGISTRATION_TOKEN> \
--ephemeral \
--unattended
./run.shWith --ephemeral, the runner process exits after one job. That alone is not enough for a clean environment. If the process runs on a long-lived VM, the next start reuses the same disk. To get true isolation, something must also replace the machine or container after each exit.
On a plain VM, you would have to write that automation yourself:
- Notice that the runner exited.
- Destroy or reset the environment.
- Request a new registration token.
- Configure and start a new runner.
- Do all of this fast enough that jobs do not wait long.
Each step has failure modes. A registration token expires after a short time. A runner that crashes before taking a job can leave a stale entry in GitHub's runner list. A reset script can miss a directory.
ARC takes over this loop on Kubernetes. It creates a pod per job, handles registration, and deletes the pod afterward. Later chapters show how.
Ephemeral does not mean stateless for free
A fresh environment means every job pays to rebuild whatever it needs. The costs are concrete.
- Cold caches. Dependency downloads and build outputs are gone. Use
actions/cacheor a remote build cache, and expect network transfer time. - Image pulls. Each new pod needs its image on the node. A large runner image on a fresh node adds start time. Node-level image caching helps once a node has pulled it.
- Tool installation. Steps like
setup-nodedownload the toolchain on every job. Baking common tools into a custom runner image trades image size for job speed. Chapter 11 covers customizing the image. - Start latency. A pod must be scheduled, started, and registered before the job begins. This usually takes seconds to tens of seconds, and more if a new node must be provisioned.
A common mistake is to try to keep a persistent volume mounted at the workspace for speed. That reintroduces shared state, and with ReadWriteOnce volumes it prevents running jobs in parallel. If you need cached data, prefer an explicit cache mechanism that jobs opt into and that is keyed by content, such as a hash of a lockfile.
Using GitHub's cache from an ephemeral runner
Because the filesystem is discarded, caching must go through a service outside the pod. The following workflow restores and saves a Node dependency cache keyed on the lockfile. It works the same on ephemeral and hosted runners.
name: Build with cache
on: workflow_dispatch
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm testHere runs-on is ubuntu-latest so the example runs anywhere. When you use ARC later, you will change only that line to your scale set name. The cache behavior stays the same, because the cache lives in GitHub's storage and not on the runner.
Scaling and ephemeral runners fit together
Ephemeral runners are what make autoscaling simple. With persistent runners, scaling down is risky. You must find a runner that is idle, make sure no job lands on it during shutdown, and remove it. A job assigned at the wrong moment is lost or fails.
With ephemeral runners, there is no idle runner to retire. A runner exists because a job needed it, and it disappears when that job ends. The scaler's decision is only "how many runners should exist right now," and the number follows the job queue. This is the model ARC uses, and later chapters describe the components that implement it.
Edge cases and gotchas
- Runner starts but no job arrives. A runner can be created for a job that another runner then takes, or a job can be cancelled before assignment. An idle ephemeral runner that never receives a job still has to be cleaned up. Treat leftover idle pods as something the controller should reap, and check for them when debugging.
- Job cancelled mid-run. The runner stops and the pod is removed. Any state you wanted to keep must have been uploaded as an artifact or cache before that point. Use
if: always()on upload steps that must run on failure, though they cannot run if the pod is killed outright. - Long jobs. A pod can be evicted by the node, for example under memory pressure or during a node upgrade. An ephemeral runner does not resume. The job fails and must be re-run. Set sensible resource requests so the scheduler places pods on nodes with room.
- Sensitive data in the pod. The pod is clean at the start, but secrets provided to a job exist in the pod while it runs. Deleting the pod ends the exposure, but logs and artifacts can still leak a secret if a step prints it.
- Self-hosted does not equal trusted. Public repositories that run pull request code on self-hosted runners accept risk, even with ephemeral runners. A job can still make outbound connections or attack internal services.
Summary of the working model
- An ephemeral runner takes one job, then deregisters and exits.
- Isolation also requires that the environment be replaced, not just the process restarted.
- The cost of a clean environment is cold caches, image pulls, and start latency, which you manage with external caches and prebuilt images.
- Ephemeral runners make autoscaling a matter of matching runner count to queue demand, with no idle-runner retirement to coordinate.
Exercise
A team runs persistent self-hosted runners on VMs. A job from a pull request writes a token into ~/.docker/config.json. Explain why a later job on the same runner is at risk, and name two things an ephemeral runner on Kubernetes changes that remove this risk.
Show answer
The later job runs on the same filesystem and user, so it can read ~/.docker/config.json and use the token. An ephemeral runner removes the risk because (1) it accepts only one job, so no later job runs on that runner, and (2) its pod and filesystem are deleted after the job, so the file does not survive. The token still exists during the original job, so limiting what that job can access remains necessary.
Exercise
You add --ephemeral to the config.sh command for a runner on a long-lived VM. Why is the environment still not clean for each job, and what must be added?
Show answer
The flag makes the runner process exit after one job and removes its registration, but the VM disk and any files from the job remain. Something must also destroy or reset the environment and register a new runner for each job, which is what ARC does by creating and deleting a pod per job.
Exercise
A build job on an ephemeral runner takes four minutes longer than on your old persistent runner, mostly spent downloading dependencies. What is the likely cause, and how do you address it without mounting a shared persistent volume?
Show answer
The workspace and package caches are discarded with the pod, so every job downloads dependencies from scratch. Use an external cache such as actions/cache or the cache option in actions/setup-node, keyed on a lockfile hash, and consider baking common tools into a custom runner image. A shared persistent volume would bring back shared state and can block parallel jobs.