feat: Add seed command support to preview deployments

Run project-specific initialization commands (DB migrations, fixture
loading, etc.) automatically after containers are healthy, before the
preview is marked ready. Configured via per-service `seed` arrays in
.cw-preview.yml.
This commit is contained in:
Lukas May
2026-03-05 12:39:02 +01:00
parent 3913aa2e28
commit 714262fb83
8 changed files with 158 additions and 2 deletions

View File

@@ -111,11 +111,28 @@ Each service in `.cw-preview.yml` supports:
| `healthcheck` | no | `{path, interval?, retries?}` — polled before marking ready |
| `env` | no | Environment variables passed to the container |
| `volumes` | no | Additional volume mounts |
| `seed` | no | Array of shell commands to run inside the container after health checks pass |
| `dev` | no | Dev mode overrides: `{image, command?, workdir?}` |
\* Provide either `build` or `image`, not both.
\** Required unless `internal: true`.
### Seeding
If a service needs initialization (database migrations, fixture loading, etc.), add a `seed` array. Commands run inside the container via `docker compose exec` after all health checks pass, before the preview is marked ready.
```yaml
services:
app:
build: .
port: 3000
seed:
- npm run db:migrate
- npm run db:seed
```
Seeds execute in service definition order. Each command has a 5-minute timeout. If any seed command fails (non-zero exit), the preview fails and all containers are cleaned up.
### Dev Mode
Dev mode skips the Docker build and instead mounts your source code into a container running a dev server. Useful for hot reload during active development.
@@ -193,7 +210,7 @@ PreviewManager
### Lifecycle
1. **Start**: ensure gateway → discover config → create worktree (preview) or use provided path (dev) → generate compose → `docker compose up --build -d` → update gateway routes → health check → emit `preview:ready`
1. **Start**: ensure gateway → discover config → create worktree (preview) or use provided path (dev) → generate compose → `docker compose up --build -d` → update gateway routes → health check → run seed commands → emit `preview:ready`
2. **Stop**: `docker compose down --volumes --remove-orphans` → remove worktree → clean up `.cw-previews/<id>/` → update gateway routes → stop gateway if no more previews → emit `preview:stopped`
3. **List**: `docker compose ls --filter name=cw-preview` → skip gateway project → parse container labels → reconstruct status
4. **Shutdown**: `stopAll()` called on server shutdown — stops all previews, then stops gateway