GitHub Actions self-hosted runners without handing out root

15 min read

Part 2 of the self-hosted-ci-runners series

  1. Part 1: GitLab Runner on your own VPS: requirements, setup in Docker and hardening
  2. Part 2: GitHub Actions self-hosted runners without handing out root

Self-hosted CI runners: Part 1 · Part 2

You change runs-on: ubuntu-latest to runs-on: self-hosted, paste three commands from the repository settings into a VPS, and the next push runs on your machine. That machine now executes whatever the workflow says, as the runner's user, with the VPS's network access and the job's secrets. The runner application sandboxes nothing. Below is a setup where a bad workflow, a malicious pull request or a compromised dependency costs you one throwaway container instead of the host.

Part 1 covers when self-hosting pays off, using GitLab Runner. In short: a warm Docker layer and dependency cache, no per-minute billing, and deploys into a private network without SSH open to the world. You pay with a machine to patch that runs code you did not write next to your secrets. The same trade applies on GitHub.

What the machine needs

The runner reference lists the supported systems: Ubuntu 20.04+, Debian 10+, RHEL 8+, Fedora and others on Linux, Windows 10/11 and Server 2016 to 2022, and macOS 11+. Architectures are x64 everywhere and ARM64 on Linux and macOS (Windows ARM64 is in preview).

The runner only makes outbound HTTPS connections on port 443. It long-polls GitHub for work, so there is no inbound port to expose. The essential hosts are github.com, api.github.com and *.actions.githubusercontent.com. Actions download from codeload.github.com, logs, artifacts and caches go to results-receiver.actions.githubusercontent.com and *.blob.core.windows.net, and updates come from objects.githubusercontent.com and sibling hosts. Behind an egress firewall, allow that list and nothing else. ./run.sh --check --url <repo-url> --pat <token> tests connectivity before you register anything.

The rule that bites later is updates. The runner updates itself by default. If you disable that with --disableupdate (for example because you pin an image tag), you must install a new version within 63 days of its release, or GitHub stops assigning it jobs. Jobs with no matching runner fail after 24 hours in the queue. Put the image tag in Renovate or Dependabot and treat a runner release like a security patch.

Container jobs and service containers need Linux with Docker. Cost-wise, as of 2026-11-10 GitHub's Actions billing page lists self-hosted runner usage as free and not counted against the included compute usage.

Registering a runner the documented way

In the repository go to Settings > Actions > Runners > New self-hosted runner (for an organization: Settings > Actions > Runners > New runner > New self-hosted runner), pick the OS and architecture, and run the commands shown. Their registration token expires after one hour. The flags worth adding to config.sh:

bash
./config.sh \
  --url https://github.com/your-org/your-repo \
  --token "$REGISTRATION_TOKEN" \
  --name vps-1 \
  --labels vps-trusted \
  --ephemeral \
  --unattended

sudo ./svc.sh install runner
sudo ./svc.sh start

--labels adds custom labels on top of the defaults (self-hosted, the OS such as linux, the architecture such as x64). --unattended turns off the prompts. --ephemeral makes the runner take one job and then de-register itself. svc.sh install creates a systemd unit; its optional argument is the user it runs as, so pass a dedicated unprivileged user.

After its one job an ephemeral runner is gone from GitHub and the next job waits, so something has to register a fresh one each time. That is what JIT config is for.

Labels, runs-on and runner groups

runs-on with a list of labels matches only runners that carry every label:

yaml
runs-on: [self-hosted, linux, x64, vps-trusted]

Use one custom label per trust level (vps-trusted, vps-deploy), not per machine. In an organization, runner groups add the access control labels lack: a group is limited to selected repositories (and, through the API, selected workflows), and creating groups beyond the default needs the Team plan. Public repositories cannot use a group unless you allow it. Target one with runs-on: { group: deploy-runners, labels: [linux] }. An org runner takes jobs from every repository its group allows, so keep groups small.

Ephemeral runners and JIT config

GitHub's hardening guide states the problem directly: self-hosted runners have no guarantee of a clean environment and can be persistently compromised by code in a workflow. A job can leave a modified ~/.bashrc, a poisoned tool cache or a background process waiting for the next job's secrets. An ephemeral runner in a fresh container removes the place where that state would live.

The cleanest way to get one is a just-in-time runner. One API call registers the runner and returns its whole configuration, so there is no registration token and no config.sh step:

bash
curl -fsSL -X POST \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer $GITHUB_TOKEN" \
  https://api.github.com/repos/your-org/your-repo/actions/runners/generate-jitconfig \
  -d '{"name":"vps-1-job","runner_group_id":1,"labels":["self-hosted","linux","x64","vps-trusted"]}' \
  | jq -r .encoded_jit_config

The output goes to ./run.sh --jitconfig <value>. The runner does at most one job and is removed. The token that calls this endpoint has wide rights: at repository level a fine-grained token needs Administration: write, which also lets it change repository settings. At organization level the narrower Self-hosted runners: write is enough. If you have an organization, register runners there and give the generator only that permission. Either way the token stays on the host and never enters the runner container: jobs run as the same user as the runner process and can read its environment.

Keep one caveat in mind: GitHub itself says there is no way to guarantee a self-hosted runner only runs one job. Ephemeral handles the cleanup; isolation still comes from the container and the machine.

Public repositories and fork pull requests

On public repositories, GitHub's guidance is blunt: "self-hosted runners should almost never be used for public repositories on GitHub." Anyone can fork, open a pull request and put their own commands in a workflow file or a test. Plain pull_request from a fork gets no secrets and a read-only GITHUB_TOKEN, and first-time contributors may need approval, but the code still runs on your machine and can stay there.

pull_request_target makes this worse. It runs in the context of the base repository, so the workflow can have write access and the repository's secrets. GitHub's guide says such workflows "must not explicitly check out untrusted code." Do that on a self-hosted runner and the fork's code runs with your secrets on your host. The safe default: no self-hosted runner is attached to a public repository, and pull_request_target never runs on one.

Jobs run on the host, so Docker is root

Without container:, steps run on the runner machine as the runner's user. With container: or services:, the runner starts containers through the local Docker daemon. Docker's own docs say "The docker group grants root-level privileges to the user." So a runner user in the docker group, or a containerized runner with /var/run/docker.sock mounted, gives every job root on the host. A job can start a privileged container with / mounted.

The official image is no exception. ghcr.io/actions/actions-runner (published with every runner release) is the minimal image built for ARC: runner binaries, container hooks, the Docker CLI and daemon binaries, git, curl and jq. It has no entrypoint, so you pass the command yourself. Its runner user (UID 1001) has passwordless sudo. For plain Docker Compose that means: build your toolchain on top of it, and run it with no-new-privileges, which makes sudo fail inside the container. Without the Docker socket, without privileged, and without sudo, a job is an unprivileged process in a disposable container.

If you need docker build in CI, give it its own runner on its own machine (or VM), treat that machine as root-equivalent, and never put deploy secrets there.

Secrets: least privilege, environments, OIDC

Start every workflow with permissions: contents: read and raise it per job. Deploy jobs get an environment and short-lived cloud credentials through OIDC instead of a stored access key:

yaml
name: ci

on:
  push:
    branches: [main]
  pull_request:

permissions:
  contents: read

jobs:
  test:
    runs-on: [self-hosted, linux, x64, vps-trusted]
    steps:
      - uses: actions/checkout@v7
      - run: composer install --no-interaction --prefer-dist
      - run: vendor/bin/pest

  deploy:
    needs: test
    if: github.ref == 'refs/heads/main'
    runs-on: [self-hosted, linux, x64, vps-deploy]
    environment: production
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@v7
      - uses: aws-actions/[email protected]
        with:
          role-to-assume: arn:aws:iam::123456789012:role/deploy
          aws-region: eu-central-1
      - run: ./deploy.sh

id-token: write lets the job request a token whose sub claim names the repository and environment (for example repo:your-org/your-repo:environment:production). The cloud role trusts only that claim, and the credentials expire with the job.

An environment can require a reviewer (up to six users or teams; whoever triggered the run cannot approve it), and its secrets reach the job only after approval. Two limits. On Free, Pro and Team, required reviewers exist only for public repositories, so private ones need Enterprise. And GitHub notes that environment protection does not help on self-hosted runners, because the job is not isolated: a deploy on the runner that ran untrusted tests lands on a machine they could have tampered with. That is why deploy has its own label and runner.

Cache: actions/cache still goes to GitHub

actions/cache on a self-hosted runner uploads to GitHub's storage, the same hosts as artifacts. The limits match hosted runners: 10 GB per repository by default, eviction after 7 days without access. On a VPS next to your registry, the round-trip to GitHub can cost more than the cache saves.

Local alternatives, each with a trade-off:

  • BuildKit cache in your own registry: docker buildx build --cache-to type=registry,ref=registry.internal/app:cache,mode=max plus the matching --cache-from. Needs Docker, so it belongs on the build runner.
  • A host directory for Composer and npm caches, mounted into the runner container. Every job can write to it, so it is a cache-poisoning path: one trust level only.
  • A persistent runner that keeps its state. That is the warm cache from part 1, and exactly the isolation you gave up.

ARC: when Kubernetes is worth it

Actions Runner Controller is GitHub's recommended way to autoscale. It is a Kubernetes operator installed from two Helm charts (gha-runner-scale-set-controller and gha-runner-scale-set). A listener pod long-polls GitHub; for each job ARC creates an ephemeral runner pod with a JIT registration and deletes it afterwards. runs-on targets the scale set name. Docker in jobs means containerMode: dind (privileged pods) or the kubernetes modes (job containers as separate pods).

ARC is worth it when you already run a cluster and have queues of parallel jobs. For one VPS and a few repositories, a cluster for CI is more infrastructure to patch than the runners.

One VPS, two runners

If you use GitLab and GitHub for your own private projects, one VPS can host both runners. The GitLab side is covered in part 1:

yaml
# One VPS, two runners, one trust level: your own private repositories.
name: ci-runners

services:
  gitlab-runner:
    image: gitlab/gitlab-runner:alpine-v19.5.3
    restart: unless-stopped
    volumes:
      - ./gitlab-runner:/etc/gitlab-runner
      # Docker executor: jobs start through the host daemon, so this socket is root on the VPS.
      - /var/run/docker.sock:/var/run/docker.sock

  github-runner:
    image: ghcr.io/actions/actions-runner:2.338.2
    # Not started by `up`. github-runner-loop.sh runs one container per job with `run --rm`.
    profiles: [ephemeral]
    command: ["bash", "-c", "exec ./run.sh --jitconfig \"$$JIT_CONFIG\""]
    environment:
      - JIT_CONFIG
    # Blocks the image's passwordless sudo. No Docker socket, no privileged mode.
    security_opt:
      - no-new-privileges:true
    cap_drop: [ALL]

The host loop requests a JIT config with a token only root can read, then starts a new container for one job. --rm removes it, so every job starts from the image:

bash
#!/usr/bin/env bash
# Registers a just-in-time runner, runs one job in a fresh container, repeats.
set -euo pipefail
cd /opt/ci-runners

repo="your-org/your-repo"
token=$(cat /etc/ci-runners/github-token)  # chmod 600, fine-grained token for this repo only

while true; do
  jit_config=$(curl -fsSL -X POST \
    -H "Accept: application/vnd.github+json" \
    -H "Authorization: Bearer ${token}" \
    "https://api.github.com/repos/${repo}/actions/runners/generate-jitconfig" \
    -d "{\"name\":\"vps-$(date +%s)\",\"runner_group_id\":1,\"labels\":[\"self-hosted\",\"linux\",\"x64\",\"vps-trusted\"]}" \
    | jq -r .encoded_jit_config) || { sleep 30; continue; }

  JIT_CONFIG="$jit_config" docker compose run --rm -T github-runner || sleep 10
done

And a systemd unit keeps the loop alive across reboots:

ini
[Unit]
Description=Ephemeral GitHub Actions runner, one container per job
After=docker.service
Requires=docker.service

[Service]
ExecStart=/opt/ci-runners/github-runner-loop.sh
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

The rule that makes this file acceptable is one runner per trust level, and the trust boundary is the machine. The GitLab runner holds the Docker socket, so any GitLab job is root on this VPS and can read the GitHub token file. That is fine only because both runners serve the same owner and the same private repositories. Client projects, open source and deploy jobs with production credentials each get their own machine.

When not to self-host

  • Public repositories. Use GitHub-hosted runners. Fork pull requests make a self-hosted runner a public shell.
  • Low build volume. If hosted runners cover your month, a VPS only adds patching.
  • Nobody owns the box. A runner that misses updates stops getting jobs after 63 days, and an unpatched host that runs foreign code is worse than no runner.
  • You need many parallel, clean builds. A single VPS loop runs one job at a time. That is the point where ARC or hosted larger runners make sense.

If you want a CI setup reviewed or built along these lines, see what I offer.

Sources

Related articles

Dominik Jasiński

Dominik Jasiński

Laravel & PHP developer · freelance and B2B subcontracting

Maintenance and growth

Do you need cleaner deployments or maintenance?

I help teams stabilize environments, improve release flow, and keep existing systems moving without unnecessary disruption.

For software houses & agencies
Comments (0)
Sign in to leave a comment

You need to be signed in to add a comment.

Login