From 51445d4af1b00e43616ad4b26363ae4cbc86843e Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Mon, 23 Nov 2026 16:36:12 -0500 Subject: [PATCH] feat(github_runner): author Nix-enabled ephemeral GitHub Actions runner stack (#11) - .gitignore: ignore **/.env and **/app.env so runtime workload env files do not fail appctl clean-tree checks - github_runner/Dockerfile & entrypoint-nix.sh: extend pinned myoung34/github-runner:ubuntu-noble with nixos/nix:2.24.12 seed, /etc/nix/nix.conf, non-root runner ownership, and single-line APP_PRIVATE_KEY_B64 decoding - github_runner/docker-compose.yml: configure EPHEMERAL=true, RUN_AS_ROOT=false, DISABLE_AUTO_UPDATE=true, RUNNER_WORKDIR=/data/work, ../../var/lib/homelab/github_runner:/data, nix-store:/nix, and omit proxy-net for network isolation - github_runner/app.yaml, .env-example, README.md & deployments.yaml: register github_runner (alias gh-runner) in admission registry and document GitHub App org permissions --- .gitignore | 2 ++ README.md | 1 + deployments.yaml | 4 ++++ github_runner/.env-example | 9 +++++++++ github_runner/Dockerfile | 23 +++++++++++++++++++++++ github_runner/README.md | 28 ++++++++++++++++++++++++++++ github_runner/app.yaml | 17 +++++++++++++++++ github_runner/docker-compose.yml | 25 +++++++++++++++++++++++++ github_runner/entrypoint-nix.sh | 18 ++++++++++++++++++ 9 files changed, 127 insertions(+) create mode 100644 github_runner/.env-example create mode 100644 github_runner/Dockerfile create mode 100644 github_runner/README.md create mode 100644 github_runner/app.yaml create mode 100644 github_runner/docker-compose.yml create mode 100755 github_runner/entrypoint-nix.sh diff --git a/.gitignore b/.gitignore index 95d0eb6..d6a2fd6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ __pycache__/ **/data/ +**/.env +**/app.env .internal/ .venv diff --git a/README.md b/README.md index 296af84..6179b4e 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ docker compose --env-file .env up -d * **`deployments.yaml`**: Authoritative workload admission registry (`schemaVersion: "1.0"`). * **`gitea/`**: Self-hosted Git service and code repository. * **`gitea_runner/`**: Standalone Gitea Actions CI/CD runner (`act_runner`). +* **`github_runner/`**: Ephemeral Nix-enabled GitHub Actions self-hosted runner. * **`snappymail/`**: Lightweight webmail client. * **`jellyfin/`**: Media server and streaming hub. * **`armonia/`**: Armonía del Ser API and operator dashboard. diff --git a/deployments.yaml b/deployments.yaml index d97f69b..e4e87b8 100644 --- a/deployments.yaml +++ b/deployments.yaml @@ -12,6 +12,10 @@ repositories: aliases: - "act_runner" - "runner" + github_runner: + path: "~/Workloads/github_runner" + aliases: + - "gh-runner" snappymail: path: "~/Workloads/snappymail" aliases: diff --git a/github_runner/.env-example b/github_runner/.env-example new file mode 100644 index 0000000..b2a46df --- /dev/null +++ b/github_runner/.env-example @@ -0,0 +1,9 @@ +# GitHub Actions Runner environment variables (injected at runtime; never commit real credentials) +GITHUB_RUNNER_NAME=homelab-github-runner +GITHUB_RUNNER_SCOPE=org +GITHUB_RUNNER_ORG_NAME=replace-with-github-org +GITHUB_RUNNER_APP_ID=replace-with-github-app-id +GITHUB_RUNNER_APP_LOGIN=replace-with-github-app-slug +# Single-line base64-encoded GitHub App PEM private key: base64 -w0 < private-key.pem +GITHUB_RUNNER_APP_PRIVATE_KEY_B64=replace-with-base64-encoded-pem +GITHUB_RUNNER_LABELS=self-hosted,linux,x64,nix diff --git a/github_runner/Dockerfile b/github_runner/Dockerfile new file mode 100644 index 0000000..20d61bb --- /dev/null +++ b/github_runner/Dockerfile @@ -0,0 +1,23 @@ +FROM nixos/nix:2.24.12 AS nix-src +RUN tar -czf /nix-seed.tar.gz -C /nix . + +FROM myoung34/github-runner:ubuntu-noble@sha256:4c5ebb26d973b16e55250de938a498afb2f7714ef391709f3abb9d8b666b8515 + +# Regular file (never a symlink into /nix/store) so nix-store volume mounts cannot break it +RUN mkdir -p /etc/nix /nix && \ + printf '%s\n' \ + 'sandbox = false' \ + 'experimental-features = nix-command flakes' \ + 'trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=' \ + > /etc/nix/nix.conf + +COPY --from=nix-src /nix /nix +COPY --from=nix-src /nix-seed.tar.gz /opt/nix-seed.tar.gz +COPY entrypoint-nix.sh /usr/local/bin/entrypoint-nix.sh +RUN chmod +x /usr/local/bin/entrypoint-nix.sh && \ + chown -R runner:runner /nix + +ENV PATH="/usr/local/bin:/nix/var/nix/profiles/default/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin" + +ENTRYPOINT ["/usr/local/bin/entrypoint-nix.sh"] +CMD ["./bin/Runner.Listener", "run", "--startuptype", "service"] diff --git a/github_runner/README.md b/github_runner/README.md new file mode 100644 index 0000000..5d0f8d8 --- /dev/null +++ b/github_runner/README.md @@ -0,0 +1,28 @@ +# GitHub Actions Runner (`github_runner`) + +Ephemeral, Nix-enabled self-hosted GitHub Actions runner built on `myoung34/github-runner:ubuntu-noble` with Nix (`nix-command` and `flakes` enabled) and GitHub App authentication. + +## Architecture + +- **Host-Mode Execution (No Docker Socket)**: Canopy forbids mounting `/var/run/docker.sock` or running privileged containers. Workflows run directly inside the runner container as the non-root `runner` user (`RUN_AS_ROOT=false`). +- **Ephemeral Lifecycle (`EPHEMERAL=true`)**: After completing a single job, the runner deregisters and exits `0`; Docker (`restart: unless-stopped`) restarts the container and `entrypoint-nix.sh` re-authenticates via the GitHub App for a clean runner instance. +- **Persistent State & Nix Cache**: + - Runner work directory: `../../var/lib/homelab/github_runner:/data` (`RUNNER_WORKDIR=/data/work`). + - Nix store cache: named Docker volume `nix-store:/nix` so evaluated derivations persist across ephemeral restarts. If `/nix/var/nix/profiles/default/bin/nix` is missing or broken, `entrypoint-nix.sh` seeds `/nix` from `/opt/nix-seed.tar.gz` and ensures `runner:runner` ownership. +- **Network Isolation**: Intentionally **not** attached to `proxy-net`; uses the default isolated Compose bridge for outbound HTTPS to GitHub only. + +## GitHub App Permissions + +Create and install a dedicated GitHub App on your organization (for **private** repositories): +- **Organization permissions**: `Self-hosted runners: Read and write` +- **Repository permissions**: `Metadata: Read-only` + +## Configuration + +See [`.env-example`](.env-example) for required runtime variables (stored in gitignored `.env`, `chmod 0640`): +- `GITHUB_RUNNER_SCOPE`: `org` (default) or `repo` +- `GITHUB_RUNNER_ORG_NAME`: Target GitHub organization slug (when `org`) +- `GITHUB_RUNNER_APP_ID`: GitHub App ID +- `GITHUB_RUNNER_APP_LOGIN`: GitHub App slug/login +- `GITHUB_RUNNER_APP_PRIVATE_KEY_B64`: Single-line base64-encoded PEM (`base64 -w0 < key.pem`), decoded at container startup into `APP_PRIVATE_KEY` for pure-Python `.env` compatibility. +- `GITHUB_RUNNER_LABELS`: Comma-separated runner labels (default `self-hosted,linux,x64,nix`). diff --git a/github_runner/app.yaml b/github_runner/app.yaml new file mode 100644 index 0000000..5496f18 --- /dev/null +++ b/github_runner/app.yaml @@ -0,0 +1,17 @@ +schemaVersion: "1.0" +name: "github_runner" +aliases: + - "gh-runner" +domain: "internal" +description: "Ephemeral Nix-enabled GitHub Actions Self-Hosted Runner" +visible: false +auth: false +networks: + - default +deployment: + strategy: "compose" + branch: "main" + actions: + - git_pull + - compose_build + - compose_up diff --git a/github_runner/docker-compose.yml b/github_runner/docker-compose.yml new file mode 100644 index 0000000..a354fdc --- /dev/null +++ b/github_runner/docker-compose.yml @@ -0,0 +1,25 @@ +services: + github_runner: + build: . + image: ${RUNNER_IMAGE:-github-runner:local} + container_name: ${CONTAINER_NAME:-github_runner} + restart: unless-stopped + environment: + - EPHEMERAL=${EPHEMERAL:-true} + - RUN_AS_ROOT=false + - DISABLE_AUTO_UPDATE=${DISABLE_AUTO_UPDATE:-true} + - RUNNER_WORKDIR=/data/work + - RUNNER_NAME=${GITHUB_RUNNER_NAME:-homelab-github-runner} + - RUNNER_SCOPE=${GITHUB_RUNNER_SCOPE:-org} + - ORG_NAME=${GITHUB_RUNNER_ORG_NAME:-} + - REPO_URL=${GITHUB_RUNNER_REPO_URL:-} + - APP_ID=${GITHUB_RUNNER_APP_ID:-} + - APP_LOGIN=${GITHUB_RUNNER_APP_LOGIN:-} + - APP_PRIVATE_KEY_B64=${GITHUB_RUNNER_APP_PRIVATE_KEY_B64:-} + - LABELS=${GITHUB_RUNNER_LABELS:-self-hosted,linux,x64,nix} + volumes: + - ../../var/lib/homelab/github_runner:/data + - nix-store:/nix + +volumes: + nix-store: {} diff --git a/github_runner/entrypoint-nix.sh b/github_runner/entrypoint-nix.sh new file mode 100755 index 0000000..d4217de --- /dev/null +++ b/github_runner/entrypoint-nix.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -euo pipefail + +if [ ! -x /nix/var/nix/profiles/default/bin/nix ] || ! /nix/var/nix/profiles/default/bin/nix --version >/dev/null 2>&1; then + echo "[entrypoint-nix] /nix/var/nix/profiles/default/bin/nix missing or broken; seeding /nix from /opt/nix-seed.tar.gz..." >&2 + tar -xzf /opt/nix-seed.tar.gz -C /nix +fi + +mkdir -p /data/work +chown -R runner:runner /nix /data + +if [ -n "${APP_PRIVATE_KEY_B64:-}" ] && [ -z "${APP_PRIVATE_KEY:-}" ]; then + APP_PRIVATE_KEY="$(printf '%s' "${APP_PRIVATE_KEY_B64}" | base64 -d)" + export APP_PRIVATE_KEY + unset APP_PRIVATE_KEY_B64 +fi + +exec /entrypoint.sh "$@"