From 29dcc8915a9c19d4c0bd1e4d73966a4d2d0b2493 Mon Sep 17 00:00:00 2001 From: Sergei Poljanski Date: Tue, 17 Nov 2026 11:39:40 +0400 Subject: [PATCH] ci: evaluate only, comin deploys comin on each host pulls main and switches, so the CI deploy job, its SSH configuration and the known_hosts it used go. CI keeps evaluating every host on every push and no longer holds a key to the servers; SSH_DEPLOY_KEY can be removed from the repository secrets. Assisted-by: Claude:opus-5-5 --- .forgejo/workflows/nixos.yaml | 66 +++-------------------------------- README.md | 21 +++++++---- known_hosts | 2 -- nixos/README.md | 19 ++++++---- 4 files changed, 31 insertions(+), 77 deletions(-) delete mode 100644 known_hosts diff --git a/.forgejo/workflows/nixos.yaml b/.forgejo/workflows/nixos.yaml index fb1eed3..61ae6cc 100644 --- a/.forgejo/workflows/nixos.yaml +++ b/.forgejo/workflows/nixos.yaml @@ -1,17 +1,10 @@ name: nixos # Every push: evaluate every host, so a broken config never reaches main. -# main: build and switch each production host that already runs NixOS. -# A host still on Debian is skipped (it is checked for /etc/NIXOS first), -# so this job cannot touch a server before its conversion. -# -# Secret: SSH_DEPLOY_KEY (user deploy). Service secrets are decrypted on -# the hosts by sops-nix; CI holds none of them. -# -# Switching asxpio-services can restart the runner that runs this job: -# the change lands, the job shows as cancelled, the next run confirms. +# Deploying is not CI's job: each host pulls main itself (comin, +# nixos/modules/comin.nix). CI holds no credentials for the servers. -run-name: "${{ github.ref == 'refs/heads/main' && 'deploy' || 'evaluate' }}: ${{ github.ref_name }}" +run-name: "evaluate: ${{ github.ref_name }}" on: push: @@ -37,7 +30,7 @@ jobs: # Job containers are unprivileged, so Nix cannot sandbox builds in # them. Evaluating catches config errors; the build itself happens - # on each server during the deploy. + # on each server. - name: Evaluate working-directory: nixos run: | @@ -45,54 +38,3 @@ jobs: nix eval --raw ".#nixosConfigurations.$host.config.system.build.toplevel.drvPath" echo done - - - name: Set up SSH - if: github.ref == 'refs/heads/main' - env: - SSH_DEPLOY_KEY: ${{ secrets.SSH_DEPLOY_KEY }} - run: | - umask 077 - mkdir -p ~/.ssh - printf '%s\n' "$SSH_DEPLOY_KEY" > ~/.ssh/id_ed25519 - cp known_hosts ~/.ssh/known_hosts - # The runner's own host is reached through asxpio-ops: a job - # container connecting to its host arrives from a bridge address - # that sshd refuses. - cat > ~/.ssh/config <<'EOF' - Host asxpio-ops - HostName 10.10.0.10 - User deploy - Host asxpio-services - HostName 10.10.0.20 - User deploy - ProxyJump asxpio-ops - EOF - - - name: Deploy - if: github.ref == 'refs/heads/main' - working-directory: nixos - # Ops first: services reaches it for WireGuard, logs and Forgejo - run: | - # Tools from the flake's dev shell: the same pinned nixpkgs - for host in asxpio-ops asxpio-services; do - os=$(nix develop -c ssh "$host" 'test -e /etc/NIXOS && echo nixos || echo other') - if [ "$os" != nixos ]; then - echo "$host: not NixOS yet, skipped" - continue - fi - echo "== $host" - # nixos-rebuild runs the activation as a fixed transient unit; - # while an earlier one still runs (it waits for one-shot units - # it started), a new switch fails with a misleading sudo error - for i in $(seq 1 120); do - nix develop -c ssh "$host" 'systemctl is-active -q nixos-rebuild-switch-to-configuration.service' || break - echo "$host: an earlier activation is still running, waiting" - sleep 10 - done - nix develop -c nixos-rebuild switch --flake ".#$host" \ - --target-host "$host" --build-host "$host" --sudo - done - - - name: Remove credentials - if: always() - run: rm -f ~/.ssh/id_ed25519 diff --git a/README.md b/README.md index 8c183d1..4b714bc 100644 --- a/README.md +++ b/README.md @@ -24,18 +24,25 @@ delete or recreate them, reinstall in place (`nixos/README.md`). ## Changing something -Edit `nixos/`, push a branch (CI evaluates every host), merge to `main`: -`.forgejo/workflows/nixos.yaml` builds each host on itself and switches -to it, ops first. Only units whose definition changed restart. Undo -with `nixos-rebuild switch --rollback` on the host. +Edit `nixos/`, push a branch (CI evaluates every host), merge to `main`. +Each host pulls `main` within a minute, builds its own configuration and +switches to it ([comin](https://github.com/nlewo/comin), +`nixos/modules/comin.nix`). Only units whose definition changed restart. +Undo by reverting on `main`, or `nixos-rebuild switch --rollback` on the +host (the next commit on `main` replaces it). Versions are pinned (`flake.lock`, image tags); Renovate opens a PR per bump and updates `flake.lock` weekly. Things to know: -- The runner executes the deploy. A change to it restarts it mid-job: - the change lands, the job shows as cancelled, the next run confirms. +- Try a change on one host first: push it to `testing-` on + top of `main`; that host applies it with `test` (not the boot + default). Merge it to `main` to keep it. +- `comin status` on a host shows what it fetched, built and deployed. A + failing fetch, build or switch alerts in Grafana after 15 minutes. +- comin on ops fetches from Forgejo on ops. A change that breaks + Forgejo or Traefik has to be fixed by hand (`nixos/README.md`). - App stacks (asxpio, datacenterbingo, blog, czsk, vhpbot) are Podman containers that their own repositories deploy with `docker compose` as user `deploy`. `docker` and `podman` talk to the system Podman for @@ -72,7 +79,7 @@ and restores: `RESTORE.md`. Service secrets are in `nixos/secrets/.yaml`, encrypted with sops for the admin's age key and each host's SSH host key; the hosts decrypt -them at boot. CI holds only `SSH_DEPLOY_KEY`. Keep a copy of the admin +them at boot. CI holds no server credentials. Keep a copy of the admin age key (`~/.config/sops/age/keys.txt`) outside this machine. ## TODO diff --git a/known_hosts b/known_hosts deleted file mode 100644 index ed9c64c..0000000 --- a/known_hosts +++ /dev/null @@ -1,2 +0,0 @@ -10.10.0.10,10.20.0.10 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEzuhnXIFERF+slCGpjnQFhaO5iTVqVn9OMbt7jeFOUf -10.10.0.20,10.20.0.20 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDArwdI8bHpQhNnMqz7vOIGJvb5eYE7GahtkbxrNgtvp diff --git a/nixos/README.md b/nixos/README.md index d9dc8f2..60fe012 100644 --- a/nixos/README.md +++ b/nixos/README.md @@ -36,15 +36,22 @@ The servers themselves (and DNS, firewalls, buckets) are OpenTofu in ## Changing something -Edit, push a branch (CI evaluates every host), merge to `main`: CI -builds each host on itself and switches to it, ops first. A host that -does not run NixOS yet is skipped. By hand, over WireGuard: +Edit, push a branch (CI evaluates every host), merge to `main`: each +host's comin (`modules/comin.nix`) pulls `main` within a minute, builds +`nixosConfigurations.` on the host and switches to it. A +branch `testing-` on top of `main` is applied to that host +alone with `test`. `comin status` on the host shows the state. + +By hand, when comin can't fetch (Forgejo or Traefik on ops broken), +over WireGuard, building on the host itself: ```sh -nixos-rebuild switch --flake .#asxpio-services \ - --target-host root@10.20.0.20 --build-host root@10.20.0.20 +git archive HEAD | ssh root@10.20.0.10 'rm -rf /root/iac; mkdir /root/iac; tar -x -C /root/iac' +ssh root@10.20.0.10 'cd /root/iac/nixos && nixos-rebuild switch --flake .#asxpio-ops' ``` +comin moves on from there with the next commit on `main`. + Only units whose definition changed restart. Undo with `nixos-rebuild switch --rollback` on the host, or pick the previous generation in the boot menu (Hetzner console). Package updates come from @@ -134,7 +141,7 @@ tofu apply # puts the rebuild protection back ``` Then continue at step 2 above. The server gets new SSH host keys, so -step 3's `sops updatekeys` is needed, and `known_hosts` entries change. +step 3's `sops updatekeys` is needed. ## Secrets