iac/.forgejo/workflows/nixos.yaml
Sergei Poljanski c6431760bd
All checks were successful
nixos / nixos (push) Successful in 1m8s
ci: wait for a running activation before switching a host
nixos-rebuild activates through a transient unit with a fixed name. An
activation that started a long one-shot job waits for it; a deploy in
the meantime failed with "did you forget --ask-sudo-password?", twice
today. Wait up to 20 minutes for the earlier activation to finish.

Assisted-by: Claude:opus-5-5
2026-11-17 10:35:44 +04:00

98 lines
3.5 KiB
YAML

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.
run-name: "${{ github.ref == 'refs/heads/main' && 'deploy' || 'evaluate' }}: ${{ github.ref_name }}"
on:
push:
paths:
- 'nixos/**'
- '.forgejo/workflows/nixos.yaml'
workflow_dispatch:
jobs:
nixos:
runs-on: nix-latest
env:
NIX_CONFIG: |
experimental-features = nix-command flakes
build-users-group =
sandbox = false
steps:
- name: Prepare container for actions
run: nix-env -iA nixpkgs.nodejs_22
- name: Checkout
uses: actions/checkout@v7.0.1
# 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.
- name: Evaluate
working-directory: nixos
run: |
for host in asxpio-ops asxpio-services rehearsal-ops rehearsal-services; do
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