Files
Codewalkers/apps/server/preview/worktree.ts
Lukas May 143aad58e8 feat: Replace per-preview Caddy sidecars with shared gateway architecture
Refactor preview deployments to use a single shared Caddy gateway container
with subdomain routing (<previewId>.localhost:<port>) instead of one Caddy
sidecar and one port per preview. Adds dev/preview modes, git worktree
support for branch checkouts, and auto-start on phase:pending_review.

- Add GatewayManager for shared Caddy lifecycle + Caddyfile generation
- Add git worktree helpers for preview mode branch checkouts
- Add dev mode: volume-mount + dev server image instead of build
- Remove per-preview Caddy sidecar and port publishing
- Use shared cw-preview-net Docker network with container name DNS
- Auto-start previews when phase enters pending_review
- Delete unused PreviewPanel.tsx
- Update all tests (40 pass), docs, events, CLI, tRPC, frontend
2026-03-05 12:22:29 +01:00

50 lines
1.5 KiB
TypeScript

/**
* Preview Worktree Helper
*
* Creates and removes git worktrees for preview deployments.
* Preview mode checks out a specific branch into a temp directory
* so the Docker build runs against the correct code.
*/
import { simpleGit } from 'simple-git';
import { createModuleLogger } from '../logger/index.js';
const log = createModuleLogger('preview:worktree');
/**
* Create a git worktree at the specified destination, checking out the given branch.
* Does NOT create a new branch — the branch must already exist.
*
* @param repoPath - Path to the git repository (bare clone)
* @param branch - Branch to check out
* @param destPath - Where to create the worktree
*/
export async function createPreviewWorktree(
repoPath: string,
branch: string,
destPath: string,
): Promise<void> {
log.info({ repoPath, branch, destPath }, 'creating preview worktree');
const git = simpleGit(repoPath);
await git.raw(['worktree', 'add', destPath, branch]);
}
/**
* Remove a git worktree.
*
* @param repoPath - Path to the git repository
* @param worktreePath - Path of the worktree to remove
*/
export async function removePreviewWorktree(
repoPath: string,
worktreePath: string,
): Promise<void> {
log.info({ repoPath, worktreePath }, 'removing preview worktree');
try {
const git = simpleGit(repoPath);
await git.raw(['worktree', 'remove', worktreePath, '--force']);
} catch (error) {
log.warn({ worktreePath, err: error }, 'failed to remove worktree (may not exist)');
}
}