# Self-Hosted CI Runners

A self-hosted runner runs the workflows in `.swarmfile/ci.yml` on a machine
your organization controls instead of on Swarmfile's containers. The job
protocol, secrets, logs and checks are the same as [Hosted CI](https://swarmfile.com/docs/guides/hosted-ci);
what changes is where the code executes and who holds the machine.

Self-hosted runners are **active by default**. An **organization owner** can
turn them off in **Settings → Hosted CI → Policy** (the machines receive the
org's CI secrets and, when opted in, repository keys, so an org that does not
trust its teams' hardware can disable the surface). While enabled, an owner
registers a runner for the organization and the runner claims jobs from the
repositories on its allowlist. While an owner has disabled the policy, a self-hosted job is refused at admission
with the visible `self_hosted_runners_not_enabled` reason instead of waiting
in the queue; hosted jobs in the same workflow still run.

## Register a runner

<div class="docs-steps">

1. Open **Settings → Hosted CI → Self-hosted runners** (or the tray's
   **Repository settings → Hosted CI** card).
2. Choose **New runner**, give it a lower-case name (`linux-builder-1`), and
   pick the labels it advertises. A job's `runs-on` list must be a subset of
   these labels for the runner to claim it.
3. Scope the runner to at least one project. Self-hosted runners are
   project-scoped: the allowlist must name the projects the runner may serve,
   and a grant is refused for a project outside it.
4. Choose whether the runner may serve **managed** repositories (see
   [Encryption tiers](#encryption-tiers)).
5. Copy the one-time `sf_shr_…` token - it is shown once and stored hashed.

</div>

Start the runner with the token and your hub URL:

```sh
export SWARMFILE_HUB_URL=https://hub.swarmfile.com
export SWARMFILE_SELF_HOSTED_RUNNER_TOKEN=sf_shr_…
swarmfile-self-hosted-runner
```

The runner writes its lease to a 0600 state file (SYSTEM/Administrators/current
user ACL on Windows), claims one job at a time, writes the rendered job spec
to a private file, runs it through the sibling `swarmfile-engine` binary, and
relays the status to the hub. Keep `swarmfile-engine` next to the runner (or
set `SWARMFILE_ENGINE_BIN`). `SWARMFILE_SELF_HOSTED_RUNNER_WAIT_SECS` (default
20, bounded 0-25) is the claim long-poll window, and
`SWARMFILE_SELF_HOSTED_RUNNER_POLL_SECS` (default 10) is the poll interval used
when it is 0. `SWARMFILE_SELF_HOSTED_RUNNER_ONCE` exits after one poll.

The package builder (`scripts/ci/build-self-hosted-runner-package.sh`, run by
the hermetic packaging lane) stages the runner, its sibling engine, the
installer and a SHA256 sidecar for a platform.

Release packages are built by
`.github/workflows/self-hosted-runner-packages.yml`; every published release
attaches the Linux and Windows packages automatically, and the release Mac
builds/attaches the macOS package (no GitHub-hosted macOS runners). Each
package carries its own `.sha256` sidecar; the release's `SHA256SUMS` is
generated before these assets attach, so verify the sidecar.

The package is a separate operator artifact, not part of the desktop
install, and the deployment must also have self-hosted runners enabled for
the organization (on by default; an owner can disable it). On Linux and macOS
the package's `install.sh` registers a systemd service or launchd daemon; on
Windows `install-self-hosted-runner.ps1` registers a scheduled task. See
[Windows](#windows) below.

### Fleet registration tokens

For an autoscaling fleet, create a **registration token** (`sf_srt_…`)
instead of a single runner. A registration token is mint-only: each use mints
one ephemeral runner that inherits the token's labels, project scope and
encryption capability, with its own one-time credential and the token's
expiry. `max-runners` caps live minted runners; revoking the registration
token revokes every runner it minted and drops their sessions. Rotate the
token by creating a new one and deleting the old.

Manage the fleet from the CLI (org owner/admin; the token is shown once and
never stored):

```sh
swarmfile ci runner list
swarmfile ci runner queue
swarmfile ci runner create linux-builder --label linux --label x64 --repository <project-id>
swarmfile ci runner token fleet --label linux --repository <project-id> --max-runners 20 --ttl-hours 24
swarmfile ci runner tokens
swarmfile ci runner revoke-token <token-id>
swarmfile ci runner delete <runner-id>
```

`ci runner create` prints the `sf_shr_…` enrollment token; `ci runner token`
prints the mint-only `sf_srt_…` token. Both are hashed at rest and cannot be
shown again. A runner or registration token may allow at most 25
repositories; enrollment is unlimited, while live sessions are capped at 64
per org and plan concurrency still applies. `ci runner queue` reports this
repository's per-label queued/running demand; the tray's CI card shows the
same queue.

## Labels and `runs-on`

A self-hosted job is a normal workflow job with `runs-on`:

```yaml
jobs:
  test:
    runs-on: [self-hosted, linux, x64]
    steps:
      - uses: actions/checkout@v4
      - run: cargo test
```

`runs-on:` and `size:` are mutually exclusive: use
`runs-on: [self-hosted, ...]` (not `size: self-hosted`) for a self-hosted job,
and hosted sizes are never served by a runner. A job is claimable only when
its `needs` have released it (the same dependency frontier hosted jobs obey;
an `if: always()`/`failure()` dependent waits for its needs and then starts).
If no live runner matches, the job stays queued for up to **2 hours**, then
the queue TTL expires it; cancel the run to stop it sooner.

## Security model

- **Protected branches only.** The claim route serves a job only from a
  branch with effective protection (or a branch protected by a rule); an
  unprotected branch is refused even when the job was dispatched manually.
- **No bucket credentials.** Cache, artifact and log bytes go through the hub
  routes with the job token. A runner never receives object-storage
  credentials.
- **One-time credentials.** A runner token is single-use; the minted lease is
  fenced, expires, and is revoked when the runner is deleted or its
  registration token is revoked.
- **Manual approval is honored.** A job with a pending approval is never
  claimed.

Canceling a run terminates the in-flight job process on the runner (SIGTERM
on POSIX; the child process is terminated on Windows) and its checks are
updated on the connected GitHub repository when the GitHub App is installed.

## Writable workspaces

A self-hosted job may use a live `mount: write` workspace, but only when all of
these hold:

- the deployment enables it (`CI_WRITABLE_MOUNT_SELF_HOSTED`; otherwise the job
  is refused at admission),
- the runner understands the mount spec (an older runner refuses the job rather
  than silently falling back to a checkout),
- the platform is Linux (`fuse-overlayfs`) or macOS (FUSE-T),
- the branch is effectively protected, and
- an organization owner opts in under **Hosted CI → policy → Self-hosted
  protected commits** (`selfHostedProtectedCommits`, off by default).

A successful job commits the step-made changes to the run's branch as one
CI-authored commit; a failed or canceled job commits nothing.

## Encryption tiers

A self-hosted runner serves **plaintext** repositories by default. Choosing
**Allow managed repositories** when registering (or on the registration
token) lets it claim managed jobs too: the managed key is delivered to the
job spec on your hardware, so only opt in on machines you trust with the
organization's managed keys.

**End-to-end encrypted** repositories need two extra opt-ins. Creating a
runner with **Allow end-to-end encrypted repositories** makes it a key
holder: on its first start it enrolls an X25519 device key under the owner
who created it (the private key never leaves the machine), and the existing
wrap-on-grant pipeline delivers the repository key. Then, in the
repository's **CI** tab, grant that runner under **End-to-end encrypted CI**.
The key is delivered to the granted runner on the claim, never in the job
spec. Revoking the runner revokes its device key and cuts every wrap.

Because the hub cannot read an encrypted tree, the workflow definition for
an E2E repository lives in the **plaintext workflow sidecar** in the CI tab
instead of `.swarmfile/ci.yml`. It is compiled with the same parser; local
composite actions and reusable workflows are refused there, since their
sources are encrypted tree files.

## Retries

A job that reports `infra_failed` **before it started** (a runner that could
not build or start the workspace) is retried automatically up to two times:
the attempt is bumped, the log store reset and the run re-queued.

A job that ever reported `running` stays terminal for a human retry, and a
runner that goes silent is reported `runner_lost` - never silently re-queued.

A restarted runner that polls with the lease of a job it already started is
stopped the same way: the job is never executed twice, and the previous
attempt's log, artifact and cache writes are refused once a retry has moved
the job on.

A skipped job reports `skipped`, a job-deadline timeout reports `timed_out`,
and a run canceled by a member reports `cancelled`<!-- docs-lint-allow cancelled: the CI status value is spelled this way -->, matching hosted jobs.

## Checks and GitHub

When the organization's GitHub App installation covers the repository and
the run's commit is the branch's latest pushed commit, job state is mirrored
to a GitHub check run named `<workflow> / <job>`: in progress on start,
success/failure/canceled on completion. Mirroring is best effort: a
mirroring failure never blocks a job.

## Billing

Self-hosted jobs are not metered for compute. The organization pays its own
infrastructure; the hub records the spend as zero for the run.

## Linux and macOS

```sh
sudo ./install.sh --hub-url https://hub.swarmfile.com --token sf_shr_...
```

The installer copies the runner and engine to
`/opt/swarmfile/self-hosted-runner` (Linux) or
`/usr/local/swarmfile/self-hosted-runner` (macOS), writes a 0600 environment
file with the token, and registers a service that restarts on failure.
`--dry-run` prints the plan, `--uninstall` removes the service, and
`--uninstall --purge` also removes the cache/state directory.

## Windows

The PowerShell installer registers a scheduled task and stores the
configuration under `%ProgramData%\Swarmfile\self-hosted-runner` with a
restricted ACL. Run it from the unpacked package (the `.exe` files sit beside
it):

```powershell
.\install-self-hosted-runner.ps1 `
  -HubUrl https://hub.swarmfile.com `
  -Token sf_shr_… `
  -InstallDir 'C:\ProgramData\Swarmfile\self-hosted-runner'
```

`-DryRun` prints the plan, `-Uninstall` removes the task and directory. Re-run
the installer with a new token to rotate credentials.

## Limitations

- Linux, Windows and macOS (the hermetic lane, including the E2E key
  ceremony) are exercised end to end; a real macOS mount lane is not
  automated.
- A runner claims one job at a time; run several runners for parallelism.
- `services:` and `container:` steps run on hosted infrastructure only.
- Windows self-hosted `mount: write` is a materialized checkout, not a live
  mount.
- E2E workflows cannot use local composite actions or reusable workflows;
  the sidecar must be self-contained.
- Shared-cache reads and writes follow the organization's `off`/`read`/`write`
  policy (server-enforced; a job token cannot widen it).
