Sub topic 3
Registering a Runner and Choosing Its Scope
This chapter assumes you can write a basic workflow (on, jobs, steps, runs-on), work in a Linux shell, and have used the gh CLI or curl against the GitHub REST API. You should also know that a runner is the application that executes a job. The previous chapters covered how runners differ from GitHub-hosted ones. Here you will register a runner and decide which repositories it serves. Those decisions affect security more than most people expect.
What registration does
A runner machine is just a host with the runner application unpacked. It does nothing until you register it. Registration ties the machine to a GitHub account, organization, or enterprise, and it gives the runner a credential for talking to GitHub Actions.
The steps are:
- You prove you have admin rights over the target scope by requesting a short-lived registration token.
- You run
config.shon the host and pass the token and the target URL. - The runner exchanges the registration token for its own credential and stores it on disk.
- You start
run.sh(or a service). The runner opens an outbound HTTPS connection and waits for jobs.
The runner polls GitHub. GitHub never connects to your host, so you open no inbound ports. You do need outbound HTTPS access to GitHub's hostnames.
The registration token
The registration token is a one-time-use secret that expires after one hour. It authorizes adding a runner and nothing else. You request it with a call that needs admin access to the scope:
# Repository scope
gh api --method POST /repos/OWNER/REPO/actions/runners/registration-token --jq .token
# Organization scope
gh api --method POST /orgs/ORG/actions/runners/registration-token --jq .tokenThe caller's token needs the right permission. For a classic personal access token that is the repo scope for repository runners and admin:org for organization runners. A fine-grained token or GitHub App needs the matching "Administration" (repository) or "Self-hosted runners" (organization) permission.
You can also copy a token from the web UI, under Settings > Actions > Runners > New self-hosted runner. The UI also shows the download URL for the runner package for your platform.
Two details matter for later chapters:
- The registration token is short-lived, but the credential the runner receives in exchange is not. The runner keeps it on disk and reuses it for every job.
- The token appears in your shell history and process list when you pass it on the command line. Read it from an environment variable or a file in automation.
Configuring the runner
Download and unpack the runner package, then run config.sh as a normal user. The script refuses to run as root unless you set RUNNER_ALLOW_RUNASROOT, and you should not set it.
mkdir actions-runner && cd actions-runner
# Download the package shown on the "New self-hosted runner" page, then:
tar xzf ./actions-runner-linux-x64-*.tar.gz
./config.sh \
--url https://github.com/OWNER/REPO \
--token "$REGISTRATION_TOKEN" \
--name build-01 \
--labels linux-build \
--work _work \
--unattendedThe options you will use most:
| Option | Purpose |
|---|---|
| `--url` | The scope: a repository, organization, or enterprise URL |
| `--token` | The registration token (or a removal token when removing) |
| `--name` | The runner's display name. It must be unique within the scope unless you use `--replace` |
| `--labels` | Extra labels, added to the defaults |
| `--runnergroup` | The runner group to join. Organization and enterprise scopes only |
| `--work` | The directory where job workspaces are created |
| `--replace` | Replace an existing runner with the same name |
| `--unattended` | Do not prompt for answers. Defaults are used for unset options |
| `--ephemeral` | Accept one job, then deregister. Covered in a later chapter |
On success, config.sh writes several hidden files in the runner directory. .runner holds the registration details. .credentials and .credentials_rsaparams hold the credential the runner uses to authenticate. Anyone who can read these files can impersonate the runner and pick up its jobs, so the runner directory needs tight permissions.
Every runner also gets default labels, self-hosted, an operating system label such as linux, and an architecture label such as x64. Chapter 4 covers using labels to route jobs, so for now treat them as metadata.
Running it as a service
./run.sh runs the runner in the foreground. That is fine for testing. For a long-lived host, install it as a service so it starts at boot and restarts after failures:
sudo ./svc.sh install
sudo ./svc.sh start
sudo ./svc.sh statusBy default the service runs as the user who installed it. You can pass a user name to svc.sh install to choose a different one. Pick an account with no sudo rights and no access to other users' files. Whatever account runs the runner is the account every job runs as.
Choosing a scope
You choose the scope when you pass --url. It decides who can use the runner and who can administer it.
| Scope | Who can send jobs | Who administers it | Typical use |
|---|---|---|---|
| Repository | Workflows in that one repository | Repository admins | One project with special hardware or network needs |
| Organization | Repositories the runner group allows | Organization owners | Shared fleet for a team or company |
| Enterprise | Organizations the group is shared with | Enterprise owners | Company-wide pools |
A repository runner serves only that repository. It cannot be shared, which limits the blast radius if the machine is compromised. The cost is administrative. If you have fifty repositories, you have fifty registrations to manage.
An organization runner joins a runner group. Groups decide which repositories can use the runners inside them. A new runner joins the Default group unless you pass --runnergroup. Chapter 10 covers groups in depth. For now, remember one rule: a scope is a trust boundary. Every repository that can reach the runner can run arbitrary commands on it. If one of those repositories is less trusted than the others, a compromise there can reach all the rest.
Picking the narrowest scope that works
Use these questions to decide:
- Does only one repository need this machine? Use repository scope.
- Do several repositories owned by one team need it? Use an organization runner in a dedicated group that contains only those repositories.
- Do several organizations need the same pool? Use the enterprise scope, and limit which organizations can use each group.
- Does any of the repositories accept pull requests from forks, or is any of them public? Do not put the runner in a shared group. Chapter 6 explains why.
A common mistake is to register one big organization runner into the Default group because it is the path of least resistance. Every repository in the organization then gets access to it, including ones created later. Check the group's repository access setting after you register.
Removing and replacing runners
To unregister a runner cleanly, request a removal token (a separate endpoint with the same one-hour lifetime) and pass it to config.sh remove:
REMOVE_TOKEN="$(gh api --method POST /repos/OWNER/REPO/actions/runners/remove-token --jq .token)"
sudo ./svc.sh stop
sudo ./svc.sh uninstall
./config.sh remove --token "$REMOVE_TOKEN"If the host is already gone, you cannot run config.sh remove. Delete the stale entry from the Runners page, or call the REST API DELETE /repos/OWNER/REPO/actions/runners/RUNNER_ID. List the runners with gh api /repos/OWNER/REPO/actions/runners to find the ID. GitHub also removes a self-hosted runner automatically if it has not connected for more than 14 days. Ephemeral runners are removed sooner, after one day offline.
If you rebuild a host and reuse the name, --replace overwrites the old entry. Without it, config.sh fails with a name conflict.
Common errors
- "Http response code: NotFound" or "Not configured" during
config.sh. The token expired, belongs to a different scope than the--url, or was already used. Request a fresh one and match it to the URL. - "Must not run with sudo". You ran
config.shas root. Create a dedicated user and run it as that user. - Runner shows "Offline" in the UI. The service is stopped, the host cannot reach GitHub over HTTPS, or a proxy blocks it. Check the logs in the runner's
_diagdirectory. - Jobs stay "Queued". No online runner in an accessible group has all the labels that
runs-onlists. Compare the workflow's labels with the runner's. - Name conflict. Another runner at this scope already uses the name. Choose a unique name or pass
--replace.
Why this matters for the rest of the tutorial
Registration produces a long-lived credential on a host that executes code from your repositories. With a persistent runner you do it once and the runner then serves every job until someone removes it. The next chapters show what that costs. Chapter 5 covers state leaking between jobs, and Chapter 7 covers registering a runner that handles exactly one job.
Exercise
You register a runner with --url https://github.com/acme (an organization URL) and no --runnergroup option. Which repositories can send jobs to it, and what should you check right after registering?
Show answer
The runner joins the organization's Default runner group, so it can serve whichever repositories that group allows. By default that can be every repository in the organization. Open the group's settings and restrict repository access to the repositories that actually need the runner. Also check that public repositories are not allowed on the group.
Exercise
A teammate says, "The registration token is only valid for an hour, so a leaked token isn't a problem once the runner is set up." What is wrong with that reasoning?
Show answer
The registration token is only exchanged for the runner's own credential, which config.sh stores in .credentials and .credentials_rsaparams. That credential does not expire after an hour. Anyone who reads those files can act as the runner. The token's short life protects the registration step, not the finished runner.