Sub topic 1
Why run your own GitHub Actions runners
A runner is the machine that executes a GitHub Actions job. By default GitHub provides that machine. This chapter explains when you would run your own runners instead, what you take on by doing so, and why those costs lead to the autoscaled, Kubernetes-based approach the rest of this tutorial builds.
Prerequisites
This chapter assumes you already know:
- How to write a basic GitHub Actions workflow with jobs, steps, and
runs-on. - What a container image is and how a registry stores images.
- Kubernetes basics: pods, namespaces, and secrets.
- How to use
kubectlagainst a cluster you can access.
You do not need a cluster to read this chapter. Later chapters use one, and they also use Helm, which is covered where it first appears.
What a runner is
A workflow file describes what to do. A runner is the process that does it. The runner software polls GitHub for queued jobs, downloads the job definition, executes each step, streams logs back, and reports the result.
Two kinds of runners exist:
- GitHub-hosted runners. GitHub starts a fresh virtual machine for each job and deletes it afterward. You choose an image label such as
ubuntu-latest. - Self-hosted runners. You install the runner application on infrastructure you control, register it with GitHub, and keep it running. GitHub sends jobs to it when a workflow asks for it.
The workflow file decides which kind runs a job. The runs-on key selects it:
name: Where does this run?
on: workflow_dispatch
jobs:
hosted:
runs-on: ubuntu-latest
steps:
- run: echo "GitHub-hosted VM"
self-hosted-job:
runs-on: [self-hosted, linux, x64]
steps:
- run: echo "My own machine, $(uname -m)"For a self-hosted runner, GitHub matches the runs-on labels against the labels on registered runners. A runner receives self-hosted, its operating system, and its CPU architecture automatically. You can add custom labels such as gpu or build-large. A job runs on a runner only if that runner has every label the job lists.
Reasons to run your own runners
GitHub-hosted runners suit most projects. People move to self-hosted runners when one of the following needs outweighs the extra work.
Network access
A GitHub-hosted runner sits on the public internet. It cannot reach a private database, an internal package mirror, or a deployment target inside a VPC unless you open a path to it. A runner inside your network can reach those systems directly. This is the most common reason for self-hosting, particularly for deployment jobs.
Hardware and size
Hosted runners come in set sizes. GitHub also offers larger paid runners, but the choices are still a fixed menu. With your own runners you decide:
- How many CPUs and how much memory a job gets.
- Whether the machine has a GPU.
- Which CPU architecture it uses, such as ARM64 or a specific instruction set.
- How much local disk and what kind.
This matters for builds that need many cores, for machine-learning tests that need a GPU, and for compiling on the same architecture you ship to.
Software and caching
A hosted runner starts from a generic image. You install your toolchain on every run or pull a container, and caches have to be downloaded each time. With your own runners you can bake the toolchain into an image, keep large dependency layers on local disk, and place an artifact cache or registry mirror next to the runners. Large monorepos and heavy builds often see big time savings from this.
Cost at scale
Hosted runners are billed per minute beyond the free allowance in your plan. At high volume, running jobs on machines you already pay for, or on cheaper capacity such as spot instances, can cost less. Treat this as a calculation, not an assumption. Add engineering time, idle capacity, and any platform fee GitHub charges for self-hosted usage. Check GitHub's current pricing documentation for that last item, because billing rules for self-hosted minutes have been subject to change.
Compliance and control
Some organizations must keep source code and build output inside specific regions, accounts, or networks. Self-hosted runners let you decide where code is checked out and where secrets are used, and which logs and disks hold build data. They also let you apply your own audit, patching, and endpoint-monitoring rules.
Job limits
Documented job time limits are longer on self-hosted runners than on hosted ones. Check the current GitHub documentation for the exact figures. This helps with very long builds, though a job that takes that long is usually worth splitting up anyway.
What you take on
When you self-host, GitHub no longer maintains the machine. The costs are real:
| Responsibility | GitHub-hosted | Self-hosted |
|---|---|---|
| OS and tool patching | GitHub | You |
| Capacity planning | GitHub | You |
| Clean state between jobs | Automatic, new VM per job | Your design |
| Security of the host | GitHub | You |
| Idle cost | None, billed per minute used | You pay for capacity whether or not jobs run |
| Network reach to private systems | Needs a workaround | Direct |
Three of these rows cause most of the trouble in practice.
Idle and queued capacity
A fixed pool of runners has two failure modes. If it is too small, jobs wait in the queue while developers wait for feedback. If it is too large, you pay for machines that sit idle overnight and on weekends. Demand for CI is spiky: it rises when people push before a release and falls to nothing at 3 a.m. A static pool is wrong most of the time.
State left behind
A long-lived runner runs many jobs on the same machine. Files, caches, environment changes, and background processes from one job can affect the next. This causes two kinds of problems:
- Flaky builds. A job passes only because an earlier job left a file behind, or fails because of one.
- Leaked data. A later job, possibly from a different repository or a pull request, can read credentials or build output left on disk.
Security exposure
A self-hosted runner executes whatever code the workflow contains. If a workflow can be triggered by untrusted code, such as a pull request from a fork of a public repository, an attacker may run arbitrary commands on a machine inside your network. GitHub's guidance is to avoid self-hosted runners for public repositories unless you have strong isolation. Even for private repositories, a runner with network access and cached credentials is an attractive target.
How the problems point to a design
Look at the problems above together. Idle cost and queueing come from a fixed pool size. Leaked state and security risk come from machines that live across many jobs. The fixes mirror what GitHub-hosted runners already do:
- Create a runner when a job is waiting, and remove it when the job finishes.
- Give each job a clean environment that is thrown away.
- Add capacity when the queue grows and release it when the queue is empty.
Kubernetes is a good place to do this. Pods start in seconds, they are disposable by design, and the cluster scheduler already knows how to place work on available nodes. The next chapters cover the single-use runner idea and then the Actions Runner Controller (ARC), which is GitHub's Kubernetes operator that automates exactly this loop.
Choosing between the two
Use these criteria in order:
- Start with GitHub-hosted runners. They need no maintenance and give every job a clean machine.
- Move a specific job when a hard requirement forces it. Private network access, a GPU, an unusual architecture, or a compliance rule are good reasons. A vague sense that self-hosted is cheaper is not.
- Move only the jobs that need it. You can mix both kinds in one repository. A test job can stay on
ubuntu-latestwhile a deploy job uses aself-hostedlabel. - Estimate the full cost. Include engineer time for upgrades and incidents, idle capacity, and monitoring, not only the compute price.
- Plan the security model before registering the first runner. Decide which repositories may use the runners, using runner groups at the organization level, and what the runners can reach.
Common mistakes
- Using the
self-hostedlabel alone. It matches any self-hosted runner. Add OS, architecture, or custom labels so jobs reach the right hardware. - Treating a runner like a persistent server. Installing tools by hand on a runner makes it a snowflake that nobody can reproduce. Put tools in an image or in the workflow.
- Ignoring cleanup. Without cleanup between jobs, disks fill up and old files affect new builds.
- Allowing public forks to trigger jobs. This is the largest risk described above.
- Sizing the pool by averages. Average load hides the peaks that cause long queues.
Exercise
A team's deploy job must reach a database in a private subnet, while its unit tests need nothing special. Which runner type should each job use, and what two problems should the team expect if they register a fixed set of long-lived self-hosted runners for the deploy job?
Show answer
Run the unit tests on GitHub-hosted runners (for example ubuntu-latest) and run the deploy job on self-hosted runners that sit inside the private network, selected with labels such as [self-hosted, linux, x64]. With a fixed set of long-lived runners they should expect (1) a sizing problem: jobs queue when the pool is too small and idle capacity is paid for when it is too large, and (2) a state and security problem: files and credentials left by one job can affect or be read by later jobs on the same machine.
Exercise
Why is the bare label self-hosted a poor choice for runs-on in an organization that has both Linux x64 and ARM64 runners?
Show answer
The self-hosted label matches every self-hosted runner. A job that needs a particular architecture or OS could land on the wrong machine and fail. Adding labels such as linux and x64, or a custom label, restricts the job to runners that have all of them.