Skip to content
4 changes: 2 additions & 2 deletions .quality/baseline.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"measuredOn": "main 08061db, 2026-11-21, with the commands in .quality/README.md; the full mutation run went one file at a time with .quality/mutant-tests.sh and 2 workers",
"coverage": { "lines": 89.19, "metric": "statement coverage of cmd/ and internal/ without tests, as go tool cover -func totals it" },
"measuredOn": "main 08061db, 2026-11-21, with the commands in .quality/README.md; the full mutation run went one file at a time with .quality/mutant-tests.sh and 2 workers; coverage remeasured 2026-11-26 for the runner pools change",
"coverage": { "lines": 90.92, "metric": "statement coverage of cmd/ and internal/ without tests, as go tool cover -func totals it" },
"functionsMeasured": 59,
"functionsOverCrap10": 10,
"functionsOverCrap10List": [
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ the major version is 0, a minor release may change configuration (each such chan
## [Unreleased]

### Added
- `RUNNER_POOLS`: one controller serves several runner images. It takes a JSON list of pools, each with a `label`, `image`, `prefix`, `max_runners`, `scopes` and an optional `region` (falling back to `RUNNER_REGION`). Each cycle reads every scope once, whichever pools list it, gives each queued job to the pool whose label it asks for, and plans each pool on its own, so a full pool never holds up another. The App's one webhook wakes the controller for a job of any pool, so a second image no longer needs a second controller that only polls and scans the same repositories again. Labels and prefixes must differ across pools. The single-pool variables (`RUNNER_IMAGE`, `RUNNER_SCOPES`, `RUNNER_LABEL`, `RUNNER_SERVICE_PREFIX`, `MAX_RUNNERS`) still configure one pool unchanged; setting them together with `RUNNER_POOLS` is refused. `MAX_CREATE_PER_CYCLE` applies per pool. docs/deploy.md describes moving from two controllers to one; the second-controller setup is no longer documented.
- The `sqlite3` command-line shell in every image variant, for tests and scripts that drive a SQLite database from the shell.
- `libssl-dev` in every image variant, so Rust crates that link OpenSSL (`openssl-sys`) build on the pool; the build checks that `pkg-config` finds it.
- The GitHub CLI (`gh` 2.102.0, pinned by version and sha256) in every image variant, so a job can open or comment on issues without a GitHub-hosted runner. Nothing is logged in: a job passes `GH_TOKEN`.
Expand All @@ -17,6 +18,8 @@ the major version is 0, a minor release may change configuration (each such chan
- Jobs queued in a run that GitHub reports as `pending` (for example while it waits on a concurrency group) are served; the controller listed only `queued` and `in_progress` runs and left such jobs waiting forever.

### Changed
- The webhook matches a job's labels without case, as GitHub and the job scan already do; a label written `Railway` in `runs-on` now wakes the controller too.
- A label of `self-hosted`, `linux` or `x64` is refused at start: every runner has those, so they cannot pick out a pool.
- A queued-job webhook starts a cycle after a 3 s debounce instead of a fixed 20 s; cycles still start at least 20 s apart (measured from the previous cycle's start).

- CI rebuilds the runner images only when `image/` changes and the controller image only when its Go code or Dockerfile changes, with a per-target layer cache; a version tag still publishes all three.
Expand Down
53 changes: 40 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,15 @@ limits. [docs/deploy.md](docs/deploy.md) has the full guide, rollout and rollbac
| --- | --- | --- |
| `GITHUB_APP_ID`, `GITHUB_APP_PRIVATE_KEY`, `GITHUB_WEBHOOK_SECRET` | the App (the PEM may keep `\n` escaped) | required |
| `RUNNER_RAILWAY_TOKEN`, `RUNNER_RAILWAY_PROJECT_ID`, `RUNNER_RAILWAY_ENVIRONMENT_ID` | a project token, project and environment of the **runner** project (not `RAILWAY_*`: Railway injects those for the controller's own project, and the controller refuses to manage the project it runs in) | required |
| `RUNNER_IMAGE` | the runner image, pinned to a version or digest | required |
| `RUNNER_SCOPES` | comma list of `org:<name>` and `repo:<owner>/<name>` that may get runners | required |
| `RUNNER_LABEL` | the `runs-on` label of this controller's runners | `railway` |
| `RUNNER_SERVICE_PREFIX` | Railway service-name prefix of this controller's runners; a second controller in the same runner project needs its own (a letter, a letter or digit, a dash, e.g. `rp-`) | `rr-` |
| `RUNNER_REGION` | Railway region id for this controller's runners, e.g. `us-east4-eqdc4a`; the API's `regions` query lists them. Unset leaves placement to Railway (the workspace's preferred region) | unset |
| `RUNNER_POOLS` | several runner images on one controller: a JSON list of pools, see [Several pools](#several-pools). Set it instead of the five single-pool variables below | unset |
| `RUNNER_IMAGE` | the runner image, pinned to a version or digest (single pool) | required without `RUNNER_POOLS` |
| `RUNNER_SCOPES` | comma list of `org:<name>` and `repo:<owner>/<name>` that may get runners (single pool) | required without `RUNNER_POOLS` |
| `RUNNER_LABEL` | the `runs-on` label of the runners (single pool) | `railway` |
| `RUNNER_SERVICE_PREFIX` | Railway service-name prefix of the runners: a letter, a letter or digit, a dash, e.g. `rp-` (single pool) | `rr-` |
| `MAX_RUNNERS` | services at a time (single pool) | `8` |
| `RUNNER_REGION` | Railway region id for the runners, e.g. `us-east4-eqdc4a`, for every pool without a `region` of its own; the API's `regions` query lists them. Unset leaves placement to Railway (the workspace's preferred region) | unset |
| `RUNNER_GROUP_ID` | the dedicated runner group of org runners; required for an `org:` scope, and the Default group (1) is refused | none |
| `MAX_RUNNERS` / `MAX_CREATE_PER_CYCLE` | services in all / created in one cycle | `8` / `4` |
| `MAX_CREATE_PER_CYCLE` | services one pool creates in one cycle | `4` |
| `POLL_SECONDS` | polling period (a queued-job webhook wakes a cycle sooner: after a 3 s debounce, and at least 20 s after the previous cycle started) | `120` |
| `STARTUP_GRACE_SECONDS` | how long a registered runner may stay offline before its service is removed | `300` |
| `IDLE_TTL_SECONDS` | how long a surplus idle runner is kept | `120` |
Expand All @@ -149,6 +151,33 @@ limits. [docs/deploy.md](docs/deploy.md) has the full guide, rollout and rollbac
| `ALERT_FAILED_CYCLES` | after this many failed cycles in a row: one alert (naming scopes unreadable that long), `/healthz` 503, and one "recovered" alert when a cycle succeeds again. A scope GitHub answers 404 for this many cycles in a row raises one alert of its own | `3` |
| `PORT` | HTTP port | `8080` |

### Several pools

One controller can serve several runner images, each a pool with its own label, image, service-name
prefix, maximum and scopes. Each cycle reads every scope once, whichever pools list it, and gives each
queued job to the pool whose label it asks for. Each pool is then planned on its own, so a pool at its
maximum never holds up another. The App's one webhook wakes the controller for a job of any pool.

```
RUNNER_POOLS=[
{"label":"railway","image":"ghcr.io/farzai/github-runner-railway:<version>","prefix":"rr-","max_runners":4,"scopes":["repo:acme/app","repo:acme/api"]},
{"label":"railway-pg","image":"ghcr.io/farzai/github-runner-railway:pg-<version>","prefix":"rp-","max_runners":2,"scopes":["repo:acme/app"]}
]
```

* Every field is required except `region`, which falls back to `RUNNER_REGION`. An unknown field is
refused, so a typo stops the controller at start.
* Labels must differ (compared without case, as GitHub does) and cannot be `self-hosted`, `linux` or
`x64`. A job that asks for two pool labels fits no runner and is left alone.
* Prefixes must differ. The prefix rule (a letter, a letter or digit, a dash) means one prefix is never
the start of another, so no pool lists or removes another pool's services.
* `RUNNER_POOLS` together with `RUNNER_IMAGE`, `RUNNER_LABEL`, `RUNNER_SERVICE_PREFIX`, `MAX_RUNNERS` or
`RUNNER_SCOPES` is refused. Without `RUNNER_POOLS`, those five variables are one pool, as before.
* `MAX_CREATE_PER_CYCLE` applies to each pool, so N pools can create up to N times that many services in
one cycle.
* Before you take a pool out, drain it: move its workflows to another label and wait until its services
are gone. The controller no longer lists the services of a prefix it does not have.

## The runner images

| Image | Contents |
Expand All @@ -159,10 +188,8 @@ limits. [docs/deploy.md](docs/deploy.md) has the full guide, rollout and rollbac
| `ghcr.io/farzai/github-runner-railway:php-<version>` | the base image plus PHP 8.3, 8.4 and 8.5 (ondrej/php PPA) and Composer, and `use-php <version>` to pick one; for PHP projects, since `setup-php` cannot run without sudo |
| `ghcr.io/farzai/github-runner-railway:redis-<version>` | the base image plus a pinned `redis-server` and `ci-redis` |

To serve both, run a **second controller** with `RUNNER_LABEL=railway-pg`, `RUNNER_SERVICE_PREFIX=rp-`
and the `pg-` image, in the same runner project. Each controller lists and removes only its own
services and only takes jobs that ask for its label. A GitHub App has one webhook URL, so the second
controller works on polling alone (set `POLL_SECONDS=60`).
To serve more than one image, give each its own pool in `RUNNER_POOLS` (see [Several pools](#several-pools)):
one controller, one webhook, each pool with its own label and prefix.

The published `pg-` image has PostgreSQL 18 and no extensions. Build your own with another major and
PGDG extensions (all pinned, from the signed PGDG repository) and push it to your registry:
Expand Down Expand Up @@ -244,8 +271,8 @@ For workflows you cannot change yet (`sudo apt-get install`, `playwright install
`ldconfig` in a step), the `sudo-<version>` image gives the runner user passwordless sudo. Each job
is its own container, so root stays inside that job's container and is gone when it ends, but the
job can now change anything in it, and the container still shares the runner project's private
network with concurrent jobs. Use it for private repositories only, in its own pool (a controller
with its own label and `RUNNER_SERVICE_PREFIX`), and prefer a derived image where you can. The
network with concurrent jobs. Use it for private repositories only, in its own pool (its own label and
prefix in `RUNNER_POOLS`), and prefer a derived image where you can. The
default images stay sudo-free.

## Limits
Expand All @@ -261,7 +288,7 @@ default images stay sudo-free.
about 1,000 processes and threads). `nproc` shows the host's cores, not the quota; Node's
`os.availableParallelism()` reads the quota. Pass explicit `-j`/worker counts to be safe.
* **Railway API:** 1,000 requests an hour per token. A job costs about four calls and a cycle one, so
roughly 230 jobs an hour at the default poll; two controllers sharing one token share that budget.
roughly 230 jobs an hour at the default poll; every pool, and every controller sharing the token, shares that budget.
* **GitHub API:** 5,000 requests an hour per installation, shared by every scope of that installation
(one access token per installation). Run and job lists are read with `If-None-Match`, and an
unchanged list (`304`) does not count against the limit. Prefer `repo:` scopes for large organizations:
Expand Down
8 changes: 4 additions & 4 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func main() {
log.Error("startup", "err", err.Error())
os.Exit(1)
}
gh, err := ghapp.New(ghapp.Config{AppID: cfg.AppID, PrivateKey: cfg.PrivateKey, Label: cfg.Label, RunnerGroupID: cfg.RunnerGroupID})
gh, err := ghapp.New(ghapp.Config{AppID: cfg.AppID, PrivateKey: cfg.PrivateKey, Labels: cfg.Labels(), RunnerGroupID: cfg.RunnerGroupID})
if err != nil {
log.Error("startup", "err", err.Error())
os.Exit(1)
}
rw := railway.New(railway.Config{Token: cfg.RailwayToken, ProjectID: cfg.RailwayProject, EnvironmentID: cfg.RailwayEnv, Image: cfg.Image, Prefix: cfg.ServicePrefix, Region: cfg.Region})
rw := railway.New(railway.Config{Token: cfg.RailwayToken, ProjectID: cfg.RailwayProject, EnvironmentID: cfg.RailwayEnv, Prefixes: cfg.Prefixes()})
alert := func(msg string) {
log.Error("ALERT", "alert", msg) // not "msg": slog writes the message under that key
if cfg.AlertURL == "" {
Expand All @@ -43,13 +43,13 @@ func main() {
}
}
c := controller.New(controller.Config{
Scopes: cfg.Scopes, Prefix: cfg.ServicePrefix, MaxRunners: cfg.MaxRunners, MaxCreatePerCycle: cfg.MaxCreatePerCycle,
Pools: cfg.Pools, MaxCreatePerCycle: cfg.MaxCreatePerCycle,
StartupGrace: cfg.StartupGrace, IdleTTL: cfg.IdleTTL, MaxAge: cfg.MaxAge, AlertAfter: cfg.AlertAfter,
Interval: cfg.Interval, FailAlertAfter: cfg.FailAlertAfter, Alert: alert, Log: log,
}, gh, rw)

mux := http.NewServeMux()
mux.Handle("/webhook", webhook.Handler(cfg.WebhookSecret, cfg.Label, func(e webhook.Event) {
mux.Handle("/webhook", webhook.Handler(cfg.WebhookSecret, cfg.Labels(), func(e webhook.Event) {
log.Info("workflow_job", "action", e.Action, "repo", e.Repo)
c.OnEvent(e)
}))
Expand Down
Loading
Loading