Sub topic 5
Choosing a runner with runs-on
This chapter assumes you can read a basic workflow file and know what a job is. A runner is the machine that executes a job. The runs-on key tells GitHub which kind of runner to use.
What runs-on does
Every job needs a runs-on value. GitHub finds a matching runner, starts a fresh environment, and runs the job's steps there. When the job ends, that environment is discarded. Nothing carries over to the next job unless you pass it with outputs or artifacts.
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm testGitHub-hosted runners
GitHub-hosted runners are virtual machines that GitHub maintains. You choose one by label.
| Label | Operating system |
|---|---|
| `ubuntu-latest` | Linux (Ubuntu) |
| `windows-latest` | Windows Server |
| `macos-latest` | macOS |
The -latest labels move to newer images over time. If you need a stable environment, pin a specific version such as ubuntu-24.04. The set of available labels changes, so check the GitHub documentation for the current list.
jobs:
build:
runs-on: ubuntu-24.04
steps:
- run: cat /etc/os-releaseSelf-hosted runners
A self-hosted runner is a machine you register and manage yourself. Use one when you need special hardware, private network access, or software that the hosted images lack. You select it with a list of labels. The job runs only on a runner that has every label in the list.
jobs:
train:
runs-on: [self-hosted, linux, gpu]
steps:
- run: nvidia-smiIf no online runner has all the labels, the job waits in the queue. A job stuck on "Waiting for a runner" usually means a label is misspelled or the runner is offline.
Choosing the runner dynamically
You can use a matrix value in runs-on to run the same job on several operating systems.
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v4
- run: echo "Running on ${{ runner.os }}"The runner context describes the machine the job landed on. Its runner.os value is Linux, Windows, or macOS.
Common errors
- Shell differences. On Windows runners the default shell for
runis PowerShell, not bash. Setshell: bashon a step if your script expects bash. - Unexpected image changes. A
-latestlabel can change the operating system version under you. Pin a version if a sudden break would be costly. - Untrusted code on self-hosted runners. A pull request from a fork can run code on your machine. Avoid self-hosted runners for public repositories unless you restrict who can trigger workflows.
Exercise
A job has runs-on: [self-hosted, linux, arm64] and sits in the queue indefinitely. Your only registered runner has the labels self-hosted and linux. What is wrong, and how do you fix it?
Show answer
The job needs a runner that has all three labels, and the registered runner lacks arm64. Either add the arm64 label to a runner that really is ARM, register an ARM runner, or remove arm64 from the job's runs-on list if the architecture does not matter.