Sub topic 7
Ephemeral runners: one job, then gone
This chapter assumes you can read a GitHub Actions workflow, use a Linux shell, and know the difference between a container and a virtual machine. It also assumes you have seen how a self-hosted runner registers and how labels route jobs to it. The previous chapters showed how a persistent runner can carry files, processes, and credentials from one job to the next. An ephemeral runner removes that problem by design.
What "ephemeral" means
An ephemeral runner accepts exactly one job. When that job finishes, the runner deregisters itself from GitHub, and the runner application exits. GitHub will not send it a second job, even if the same process were still running.
This changes what you need to trust. With a persistent runner, you must trust every earlier job not to have left anything behind. With an ephemeral runner, the next job starts on a runner that has never run a job before, as long as you create that runner from a clean image.
Registering an ephemeral runner
You turn the behavior on with the --ephemeral flag when you configure the runner:
./config.sh \
--url https://github.com/OWNER/REPO \
--token REGISTRATION_TOKEN \
--name "runner-$(hostname)-$(date +%s)" \
--labels ephemeral \
--ephemeral \
--unattended
./run.shThree details matter here:
--ephemeralmakes the runner take one job and then remove itself from GitHub.- The name includes a timestamp, so each new runner has a unique name and does not collide with an older registration.
--labels ephemeraladds a custom label. Workflows can use it to ask for this kind of runner.
A workflow selects the runner with the runs-on list. A job runs only on a runner that carries every label in the list:
name: test
on: pull_request
permissions:
contents: read
jobs:
test:
runs-on: [self-hosted, linux, x64, ephemeral]
steps:
- uses: actions/checkout@v4
- run: make testThe permissions block limits the GITHUB_TOKEN to read access on repository contents. This keeps the damage small if a step misbehaves.
The lifecycle of one runner
Read the diagram from left to right. GitHub does the deregistration step. Removing the machine or container is your job.
The part GitHub does not do
The --ephemeral flag removes the runner's registration. It does not wipe the machine. If you run ./run.sh on a long-lived server, the files on disk stay there, and the runner application has exited. The next job will have no runner to pick it up until you configure a new one, and that new runner would reuse the dirty disk.
A common pattern is to start each runner in a fresh container or VM from a known image. A supervisor process watches for the runner to exit, throws the environment away, and creates a new one. A simple loop shows the idea, but in production you would use an orchestrator:
while true; do
docker run --rm my-runner-image
doneHere --rm deletes the container when it exits. The image is expected to run config.sh and run.sh at startup, using a registration token that is passed in from outside.
Common errors
- Jobs wait forever in "Queued". No runner with every requested label is online. Because ephemeral runners vanish after one job, make sure something creates a replacement. Queued jobs can also pile up faster than your supervisor creates runners.
- Registration fails with a token error. Registration tokens expire after a short time. Generate one right before you configure the runner. Do not bake it into an image.
- A runner is still listed after its machine died. If the machine is killed before the job is assigned, the registration can linger until GitHub removes it. Delete stale entries as part of your cleanup.
- State still leaks. This happens when the host is reused. The runner is ephemeral, but the environment around it is not.
Why this matters
Ephemeral runners limit the lifetime of anything a job can plant. A malicious or buggy step cannot leave a modified tool, a stolen token, or a background process for the next job. Later chapters cover how to create these runners on demand and how to keep credentials short-lived.
Exercise
You add --ephemeral to config.sh on a long-lived server and keep using the same server for new runners. Which problem does this fix, and which problem remains?
Show answer
It fixes registration reuse: each runner takes one job and then deregisters, so GitHub will not send it another. The problem that remains is the shared filesystem. Files, caches, images, and processes from earlier jobs persist on the server, so you must also destroy or reset the host or container after every job.
Exercise
Why does the example runner name include $(date +%s)?
Show answer
It makes each runner's name unique, so a new runner does not collide with an older registration that has the same hostname.