Sub topic 7
Ephemeral runners: one job, then gone
This chapter assumes you can read a GitHub Actions workflow (on, jobs, steps, runs-on), work in a Linux shell, and know the basics of containers or virtual machines. It also builds on the previous chapters. You have seen how a runner registers and how labels route jobs. You have also seen what goes wrong when one runner machine serves many jobs.
What "ephemeral" means
An ephemeral runner is a self-hosted runner that accepts exactly one job. When that job finishes, the runner application removes its own registration from GitHub and exits. The runner will not pick up a second job, even if one is waiting with matching labels.
Ephemeral is a property of the registration, not of the machine. The --ephemeral flag tells GitHub to treat the registration as single-use. The host is only as disposable as you make it. If you leave the same VM running and its workspace untouched, the next job can still find leftovers on disk. The design works because you pair a single-use registration with a fresh environment for every job.
The goal is to remove the problems from the last chapter by construction instead of by cleanup:
- No workspace files, caches, or build output carry over between jobs.
- No credentials written by one job are readable by the next.
- A background process started by one job cannot keep running into the next.
- A compromised job can damage only its own environment, which is thrown away afterwards.
Why cleanup scripts are not enough
Many teams first try to make a persistent runner safe with a cleanup step. The step deletes the workspace, clears ~/.docker, or runs git clean. This approach fails in predictable ways.
- The cleanup list is incomplete. State lives in many places: the Docker image cache,
~/.ssh,~/.npm,/tmp, systemd user units, cron entries, and shell profiles. - Cleanup runs inside the same trust boundary as the job. A malicious step can edit or disable the cleanup, or tamper with tools the next job will use.
- A cancelled or crashed job may never reach the cleanup step.
- Race conditions appear when several runner processes share one host.
Replacing the environment is easier to reason about than scrubbing it. You do not need to know what changed, because everything is discarded.
The lifecycle of an ephemeral runner
The diagram shows the states a single ephemeral runner passes through.
Two points matter here. First, GitHub does the deregistration, but you do the destruction. Nothing in the runner application deletes your VM. Second, an idle ephemeral runner that never gets a job still exists. You need a timeout or a controller that cleans it up. Otherwise idle runners pile up and cost money.
Registering an ephemeral runner
The simplest way to get an ephemeral runner is to add --ephemeral when you configure it:
./config.sh \
--url https://github.com/OWNER/REPO \
--token REGISTRATION_TOKEN \
--name "runner-$(hostname)-$(date +%s)" \
--labels ephemeral \
--ephemeral \
--unattended
./run.shEach option does a specific job:
| Option | Purpose |
|---|---|
| `--url` | The repository, organization, or enterprise the runner belongs to. This sets its scope. |
| `--token` | A registration token. It is short-lived and obtained from GitHub or its API. |
| `--name` | A unique name. Using the hostname plus a timestamp avoids collisions between runners. |
| `--labels` | Extra labels added to the default `self-hosted`, OS, and architecture labels. |
| `--ephemeral` | Marks the registration as single-use. |
| `--unattended` | Skips interactive prompts, which is required in scripts and images. |
When run.sh starts, the runner connects to GitHub and waits. After one job it exits with a normal status. A wrapper that is allowed to restart the process would defeat the purpose, so the wrapper must not simply loop on run.sh. The next step is to destroy the environment and create a new one.
Common mistakes with `--ephemeral`
- Restarting the same host in a loop. A
while true; do ./config.sh ...; ./run.sh; doneloop gives you single-use registrations but a shared filesystem. This is better than a normal persistent runner, because a job cannot be reused across registrations, but leftover files still carry over. Recreate the VM or container instead. - Baking the registration into the image. If the image contains a
.runnerfile or credentials from an earlier registration, every instance starts with stale state. Register at start-up, not at image build time. - Reusing names. Registering with a name that already exists can replace the old registration or fail. Use unique names.
- Forgetting the idle timeout. A runner that never gets a job needs to be removed by something other than GitHub.
- Assuming labels guarantee freshness. A label named
ephemeralis only a label. Nothing stops a persistent runner from carrying it. The workflow below routes to those labels, but it is the registration flag and your provisioning that make the claim true.
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 runs-on list selects only runners that carry every listed label. The permissions block limits the GITHUB_TOKEN to read access on repository contents. Both choices are part of the same defense. The ephemeral runner limits what a job can leave behind, and the narrow token limits what a job can do while it runs.
What happens when a job is cancelled or the runner dies
Edge cases are where ephemeral fleets cause surprises.
- Job cancelled while running. The runner stops the job, then deregisters and exits as it would after a normal finish. Your controller still has to destroy the environment.
- Runner process crashes before taking a job. The registration may stay in GitHub's list as offline until GitHub removes it. A queued job can wait for a runner that no longer exists. Monitor for offline runners and for jobs queued longer than expected.
- Runner crashes mid-job. The job fails and, for a runner that has stopped heartbeating, GitHub eventually marks it as lost. The host should still be destroyed, not reused.
- Job queued with no matching runner. The job waits. GitHub does not fall back to hosted runners, because the
runs-onlabels name only self-hosted ones. Autoscaling, covered in a later chapter, is how you make sure a fresh runner appears when a job arrives. - Two runners race for one job. Only one is assigned. The other stays idle and unused, so it needs an idle timeout.
Ephemeral does not mean secure by itself
Single-use registration closes the door on cross-job contamination. It does not fix everything.
- A job still runs arbitrary code with whatever network access and credentials the environment has. If the runner sits on a network with access to internal services, a malicious job can reach them during its lifetime.
- A pull request from a fork running on a self-hosted runner still executes untrusted code. Ephemeral limits the blast radius in time, but not what the code can reach while it runs. Later chapters cover runner groups, workflow triggers, and isolation.
- The instance metadata service on cloud hosts can hand out the host's role credentials. Block it or restrict it for runner instances.
- Secrets passed to a job are available to that job's steps. A fresh host does not protect secrets from the code running in the job.
For these reasons, ephemeral runners work best together with narrow permissions, restricted runner groups, network controls, and short-lived cloud credentials, all of which later chapters cover.
Choosing what "fresh" means
The strongest freshness comes from replacing the whole machine. The options differ in cost and startup time.
| Approach | Freshness | Startup time | Notes |
|---|---|---|---|
| New VM per job | Strong | Tens of seconds to minutes | Strongest boundary. Needs prebuilt images to be fast. |
| New container per job | Moderate | Seconds | Shares the host kernel. Avoid mounting the Docker socket. |
| Restart a long-lived host with a clean directory | Weak | Immediate | Not truly ephemeral. Leftover state outside the directory survives. |
Prebuilding an image that contains the runner binaries and common tools shortens startup. Keep the registration step out of the image and run it at boot.
A minimal start-up wrapper
This script shows the shape of a per-instance entry point. It runs on a freshly created host, registers once, runs one job, and then shuts the host down so the platform can discard it. It assumes the registration token is passed in through the environment by your provisioning system.
#!/usr/bin/env bash
set -euo pipefail
: "${REPO_URL:?set REPO_URL}"
: "${REG_TOKEN:?set REG_TOKEN}"
cd /opt/actions-runner
./config.sh \
--url "$REPO_URL" \
--token "$REG_TOKEN" \
--name "runner-$(hostname)-$(date +%s)" \
--labels ephemeral \
--ephemeral \
--unattended
# Exits after exactly one job.
./run.sh || true
# Hand the host back to the platform for disposal.
sudo shutdown -h nowThe || true ensures the shutdown always runs, even when the job fails. The next chapter on just-in-time configuration removes the need for a registration token on the host at all.
Summary of the model
--ephemeralmakes a registration single-use. GitHub assigns one job, then the runner removes itself.- You provide the other half: a fresh environment for every registration, plus removal of idle or crashed instances.
- Cleanup scripts try to scrub state. Ephemeral environments avoid the need by discarding it.
- Ephemeral reduces cross-job leakage. It does not replace access control, minimal permissions, or isolation.
Exercise
A team runs ./config.sh --ephemeral ... followed by ./run.sh in a while true loop on one long-lived VM. A job writes a file to /tmp and a later job reads it. Why does this happen, and what should the team change?
Show answer
The --ephemeral flag makes each registration single-use, but the loop reuses the same VM, so the filesystem, including /tmp, persists between jobs. The team should create a new VM or container for every registration, run the runner once inside it, and destroy the environment after the job ends.
Exercise
Name two situations in which an ephemeral runner can still exist after no job will use it, and what must handle each.
Show answer
An idle runner that never received a job, and a runner that crashed before or during a job. GitHub does not delete your host in either case, so your controller or provisioning system must apply an idle timeout and destroy failed instances. It should also watch for stale offline registrations.