# Hybrid CI/CD playbook: cloud VM + home-lab Tailscale deps Reusable pattern for any app that deploys containers to a **cloud VM** (e.g. GCP) while some dependencies stay on a **home server** reached over **Tailscale**. Canonical layout: | Piece | Where it runs | |---|---| | App containers (API, UI, workers, …) | Cloud VM under `/opt//` | | Local deps (often Postgres, Redis) | **Same cloud VM** on external Docker network `shared-services` (Docker DNS names) | | Remote deps (often RabbitMQ, Seq, …) | Home lab over **Tailscale** (`100.x` IP — not MagicDNS) | | CI/CD | Build/push images → migrate (if any) → SSH deploy → smoke → optional image rollback | **Rule of thumb:** app containers stay on **bridge + `shared-services`**. Do **not** put the app on host networking just to reach Tailscale — that breaks Docker DNS for same-host services (e.g. `Host=postgres`). This playbook is project-agnostic. Substitute ``, image names, ports, and env keys for each repo. The live application of it in this repository is **GrowthOS** (`/opt/growth-os`, [`GrowthOS/docker-compose.prod.yml`](../GrowthOS/docker-compose.prod.yml), deployed by [`.gitea/workflows/cd.yml`](../.gitea/workflows/cd.yml)); the archived FleetTelemetry variant (`/opt/fleet-telemetry`) is in [`Completed/FleetTelemetryAPI/`](../Completed/FleetTelemetryAPI/). The `shared-services` network itself is described in §1–§2 below. --- ## 1. Topology rules 1. **Same-host deps** → Docker DNS on `shared-services` (`postgres`, `redis`, …). 2. **Remote Tailscale deps** → peer **CGNAT IP** in `100.64.0.0/10` (e.g. `100.102.185.124`). 3. **Never** put Tailscale MagicDNS short names inside containers — Docker cannot resolve them. 4. **Never** point same-host Postgres at `127.0.0.1` / `localhost` from a bridge container — you often hit a different instance (`28P01` / wrong DB). 5. **Public browser origins** (API base URL, SPA origin, CORS) stay operator-reachable (usually public HTTPS), not Tailscale-only, unless every client is always on the tailnet. 6. Deploy host **`.env` stays on the VM** (`chmod 600`). CD should not upload secrets; it may only patch `IMAGE_TAG` (or equivalent). 7. **CI service containers are not deploy-host services.** They live on the runner's per-job network and are reached by service name (`Host=postgres`), never by a published host port. This depends on the runner's `container.network` — confirm in [`platform/observed-state.md`](platform/observed-state.md) before copying rule 7 into a new project. --- ## 2. Choose where each dependency lives | Dependency | On cloud VM (`shared-services`) | On home via Tailscale | |---|---|---| | Connection / host value | Service name, e.g. `Host=postgres` | `100.x.y.z` | | Migrate / one-shot network | `--network shared-services` | `--network host` (or equivalent) + Tailscale/`127.0.0.1` host | | Typical split | Postgres, Redis | RabbitMQ, Seq, optional extras | Mixed splits are fine (Postgres on GCP, broker on home). Keep the **app on bridge** either way. --- ## 3. One-time: Tailscale + reachability On **cloud VM** and **home server**: ```bash tailscale up tailscale status # record the home peer's 100.x address ``` From the **cloud VM host**: ```bash HOME_TS=100.x.y.z # home Tailscale IP nc -vz "$HOME_TS" nc -vz "$HOME_TS" # e.g. Seq 5341 ``` From a **bridge container on `shared-services`** (same path the app uses): ```bash docker network create shared-services 2>/dev/null || true docker run --rm --network shared-services curlimages/curl:8.5.0 \ -sS -o /dev/null -w '%{http_code}\n' --connect-timeout 5 \ "http://${HOME_TS}:" ``` | Result | Action | |---|---| | Host OK, container OK | Proceed | | Host OK, container fail | Fix Tailscale/Docker routing on the VM (Tailscale state, IP forwarding). Do **not** switch the app to host networking | | Host fail | Tailscale/firewall on home or cloud — fix before deploy | On the **home server**, allow the cloud node’s Tailscale IP to the published ports you expose (firewall / compose binds / `tailscale serve` as appropriate). --- ## 4. One-time: cloud deploy host ```bash APP=my-app # e.g. fleet-telemetry, pos docker network create shared-services sudo mkdir -p "/opt/${APP}" sudo chown "$USER":"$USER" "/opt/${APP}" ``` Start **same-host** infra (Postgres, …) on `shared-services`. Leave remote deps on the home peer. ```bash cp .env.example "/opt/${APP}/.env" # from the app repo chmod 600 "/opt/${APP}/.env" # edit secrets in place — CI/CD should not overwrite this file ``` ### Example hybrid `.env` (Postgres on VM, broker + Seq on Tailscale) Names vary by project; keep the **hosting pattern**: ```bash # Same-host DB — Docker DNS PG_CONNECTION=Host=postgres;Port=5432;Database=;Username=;Password=;SSL Mode=Disable # Remote over Tailscale — 100.x only (not MagicDNS, not "rabbitmq"/"seq") RABBITMQ_HOST=100.x.y.z RABBITMQ_PORT=5672 RABBITMQ_USERNAME=... RABBITMQ_PASSWORD=... SEQ_URL=http://100.x.y.z:5341 SEQ_API_KEY=... # Browser-facing (often baked into a UI image at CD build time) PUBLIC_API_ORIGIN=https://api.example.com PUBLIC_UI_ORIGIN=https://app.example.com API_PORT=8100 UI_PORT=8101 ``` Recommended CD validation on deploy: - DB connection string is Npgsql-style `Host=...;Password=...` (or your stack’s equivalent), **no wrapping quotes**, no accidental URI if the tool expects key=value. - Reject `Host=127.0.0.1` / `localhost` when the app is on bridge + same-host Postgres. - Reject MagicDNS short names for remote deps (heuristic: hostname with no `.` that isn’t `localhost`). --- ## 5. One-time: CI/CD secrets & variables (Gitea / GitHub Actions / etc.) Adapt names to your workflow; the **roles** are what matter. ### Secrets | Role | Typical name | Hybrid notes | |---|---|---| | Registry login | `REGISTRY_USERNAME`, `REGISTRY_TOKEN` | Push from CI; pull on deploy host. Gitea job token (`GITEA_TOKEN`) cannot push packages — `REGISTRY_TOKEN` must be a PAT. | | Offline Gitea in CI tests | Workflow `env:` `GITEA_TOKEN: ""`, product `Gitea__BaseUrl: ""`, `Gitea__DryRun: "true"` | Actions injects `GITEA_TOKEN` into every job; job containers can reach this instance. An app that binds that token will make live Contents/Pulls calls. | | SSH deploy | `DEPLOY_HOST`, `DEPLOY_USER`, `DEPLOY_SSH_KEY` | Cloud VM only | | Schema migrator | `PG_CONNECTION_DDL` (or URI for node-pg) | **Same reachability as runtime Postgres** | When Postgres is on the cloud VM: ```text Host=postgres;Port=5432;Database=;Username=;Password=...;SSL Mode=Disable ``` Paste secret values only (no `KEY=` prefix). Prefer a **file + SCP** for connection strings that contain `;` / spaces — never pass them as bare remote shell argv. ### Variables | Role | Typical name | Hybrid notes | |---|---|---| | Public API origin for UI bake | `UI_API_BASE_URL` / `PUBLIC_API_ORIGIN` | Required if the SPA bakes the API base at build time | | Feature flags / optional URLs | project-specific | Optional | | Registry namespace override | `REGISTRY` | Optional | | Migrator Docker network | `MIGRATE_DOCKER_NETWORK` | Default `shared-services` if Postgres is on the VM; `host` only if Postgres is on Tailscale | --- ## 6. CD pipeline shape (each release) Trigger: version tag (`v*`) or manual dispatch with an image tag. 1. **Build & push** app images to the registry (bake public origins into frontends at build time). 2. **Migrate** (if applicable) — copy bundle + connection file to the VM; run a one-shot container: - Postgres on VM → `--network shared-services` + `Host=postgres` - Postgres on Tailscale → `--network host` + `Host=100.x` 3. **Deploy** — copy `docker-compose.prod.yml` (or project equivalent), validate `.env`, set image tag, `docker compose pull && up -d`. 4. **Smoke** — hit readiness/liveness and a static UI route on `localhost` ports until healthy; dump compose ps/logs on failure. 5. **Rollback** — restore previous image tag only (DDL is usually not reverted; use expand/contract migrations). ```bash git tag v1.2.3 git push origin v1.2.3 ``` Production compose should attach app services to external `shared-services` and map host ports for API/UI. It should **not** start shared infra (Postgres/Rabbit/Seq live elsewhere). --- ## 7. Verify after first deploy ```bash APP=my-app cd "/opt/${APP}" docker compose -f docker-compose.prod.yml --env-file .env ps curl -sS "http://localhost:${API_PORT}/health/ready" # or your readiness path curl -sSI "http://localhost:${UI_PORT}/" | head ``` | Symptom | Likely cause | |---|---| | DB auth failure (`28P01`) / wrong database | Bad credentials, or `Host=127.0.0.1` hitting another Postgres — use `Host=postgres` on `shared-services` | | CI: `Bind for 0.0.0.0:5432 failed: port is already allocated` | Workflow published `ports: ["5432:5432"]` on the Postgres service while the runner host already uses 5432 — drop host port publish and use `Host=postgres` (job + service share Docker DNS under act_runner) | | CI integration test 500s / hangs talking to Gitea | Job has `GITEA_TOKEN` and a configured BaseUrl — blank `GITEA_TOKEN` and `Gitea__BaseUrl` in workflow `env:` (see §5) | | Gitea / SSH stalls while CI tests run | Host is e2-standard-2 (2 vCPU / 8 GB). Testhost defaults to both cores — set `DOTNET_PROCESSOR_COUNT=1` on **every** `dotnet test` / eval step, not only unit tests. See [`platform/observed-state.md` Host budget](platform/observed-state.md#host-budget). | | CD bake / registry push stalls Gitea or SSH; UI **504** | Default bake compiles every image at once; even one compile uses both cores and swap-thrashes 8 GB. One job, `cap-buildx-builder.sh` (cgroup `--cpus=1 --memory=3g`), then bake one target at a time. If already 504: GCP **Reset** (keeps ephemeral IP — never Stop/Start), then `runner-host.sh reclaim`. See [`platform/observed-state.md` Host budget](platform/observed-state.md#host-budget). | | CD fails after green bake: `docker create` → `no command specified` | Scratch `migrate-bundle` has no `CMD`. Set `CMD ["/out/migrate-api"]` and pass `/out/migrate-api` to `docker create`. | | CI integration `57P03` / “database system is in recovery mode” | Same host budget: uncapped testhost crashed the service Postgres. Cap testhost, give the service `--shm-size=256mb`, and wait for **queries** (`pg_isready` can pass during recovery). | | CI unit tests OOM / hang-dump on a large EF model | Share one InMemory provider and one `InMemoryDatabaseRoot`; isolate by database name. Blank `ConnectionStrings__Postgres` on the unit-test step so InMemory tests do not hit the service DB. | | Broker / remote dep timeout | Bridge cannot reach `100.x`, wrong IP, or home firewall — retest from a `shared-services` container | | Smoke connection refused (`000`) | Process not listening — image entrypoint, crash loop, wrong port | | UI OK, API readiness 503 | Fix dependency checks in the readiness payload | --- ## 8. Checklist (copy per project) - [ ] Tailscale up on cloud VM and home peer; home `100.x` recorded - [ ] Host `nc` to remote ports succeeds - [ ] `shared-services` container can reach remote HTTP/AMQP deps - [ ] `docker network create shared-services`; same-host deps attached - [ ] `/opt//.env` from `.env.example`, `chmod 600`; local DNS for same-host, `100.x` for remote - [ ] CI secrets: registry, deploy SSH, migrator connection (matching Postgres location) - [ ] CI var for public API origin if UI is baked at build time - [ ] CD copies `cap-buildx-builder.sh` and bakes one target at a time on this e2-standard-2 - [ ] Scratch migrate-bundle has `CMD`; extract uses `docker create IMAGE /path` - [ ] Migrator network matches Postgres location (`shared-services` vs `host`) - [ ] Tag/deploy smoke shows readiness healthy - [ ] Git VM external IP promoted to regional static (optional but recommended) --- ## 9. Anti-patterns | Avoid | Why | |---|---| | App `network_mode: host` to “fix” Tailscale | Loses Docker DNS; same-host `postgres` breaks; `127.0.0.1` is fragile | | MagicDNS names in container env | Unresolvable inside Docker | | Uploading `.env` from CI every release | Secret sprawl; prefer host-owned file + tag patch only | | URI form for tools that expect Npgsql key=value (or the reverse) | Silent parse / auth failures | | Assuming host Tailscale routes always work from bridge | Always verify with a test container on `shared-services` | | CI service `ports: ["5432:5432"]` + `Host=localhost` on the runner | Host bind collides with the runner host's own Postgres. Use service DNS (`Host=postgres`) with no host publish. **Valid only when the runner sets `container.network: ""`** (per-job network, DNS aliases). Under `container.network: bridge` there are no aliases and the high-port workaround is required instead — see [`platform/observed-state.md`](platform/observed-state.md) | | Default / uncapped `buildx bake` of every image on this VM | Takes both cores; Gitea 504s. Cap buildkitd and bake one target at a time | | GCP Stop/Start of the git VM to clear a hung bake | Releases ephemeral external IP; DNS must be redone. Use **Reset** |