# Deploy — Self-Updating Docker Container

A single Docker container that serves the web app and auto-rebuilds when it receives a webhook from Gitea. Designed for Unraid with a Pangolin reverse proxy.

## How It Works

The container runs a Node.js server that:

1. **Serves** the built Vite SPA with gzip compression and proper caching
2. **Listens** for `POST /api/webhook` calls (auth handled by Pangolin)
3. **On webhook**: clones the repo, installs deps, builds, and hot-swaps the served files with zero downtime
4. **On first start**: auto-builds if no previous release exists

## Quick Start

### 1. Build the image

From the **project root** (not `deploy/`):

```bash
docker build -f deploy/Dockerfile -t moyettes/brycord .
```

### 2. Run the container

```bash
docker run -d \
  --name moyettes/brycord \
  -p 3000:3000 \
  -e GIT_REPO_URL=https://gitea.example.com/user/discord-clone.git \
  -e VITE_CONVEX_URL=https://your-convex-deployment.convex.cloud \
  -e VITE_LIVEKIT_URL=wss://your-livekit-server.com \
  discord-clone-web
```

The container will clone the repo and build on first start. Watch progress with:

```bash
docker logs -f discord-clone-web
```

Once the build completes, visit `http://localhost:3000`.

## Environment Variables

| Variable | Required | Default | Description |
|---|---|---|---|
| `GIT_REPO_URL` | Yes | | Git repository URL to clone |
| `GIT_BRANCH` | No | `main` | Branch to build |
| `VITE_CONVEX_URL` | Yes | | Convex backend URL |
| `VITE_LIVEKIT_URL` | Yes | | LiveKit WebSocket URL |
| `PORT` | No | `3000` | Server port |

## API Endpoints

### `POST /api/webhook`

Triggers a new build. Auth is handled by Pangolin reverse proxy.

```bash
curl -X POST http://localhost:3000/api/webhook
```

**Responses:**
- `200` — Build triggered
- `409` — Build already in progress

### `GET /api/status`

Returns current build status. No auth required.

```bash
curl http://localhost:3000/api/status
```

**Response:**
```json
{
  "building": false,
  "status": "success",
  "timestamp": 1708531200000,
  "error": null
}
```

`status` is one of: `idle`, `building`, `success`, `failed`.

## Unraid Setup

1. Build the image (or push to a registry and pull from there)
2. In the Unraid Docker UI, create a new container:
   - **Repository**: `discord-clone-web`
   - **Port mapping**: 3000 -> 3000
   - Add each environment variable listed above
3. Start the container and wait for the initial build to finish

## Gitea Webhook Setup

1. In your Gitea repo, go to **Settings > Webhooks > Add Webhook > Gitea**
2. Configure:
   - **Target URL**: `https://your-domain.com/api/webhook`
   - **HTTP Method**: POST
   - **Content Type**: application/json
   - **Trigger On**: Push Events
   - **Branch filter**: `main`
3. Click **Add Webhook**, then **Test Delivery** to verify

Auth for the webhook endpoint is handled by Pangolin's HTTP Header Auth.

## Rebuild Manually

Trigger a rebuild at any time without pushing code:

```bash
curl -X POST http://localhost:3000/api/webhook
```

## Failure Handling

- **Build fails**: temp files are cleaned up, the previous version keeps serving, error is logged and visible via `/api/status`
- **Clone fails**: same as above — old version stays live
- **Concurrent webhooks**: second request gets `409`, only one build runs at a time
- **Container restart**: serves the most recent release from `/app/releases/`, or triggers a fresh build if empty
