# App Deploy Workflow

Deploy applications using directory-scoped commands defined by the project structure.

## Scope

- Backend deploy in `{app_name}/backend`
- Frontend build and static website deploy in `{app_name}/frontend`

## Workflow

```
WORKSPACE CHECK → BACKEND DEPLOY → SEED DATA → FRONTEND BUILD → FRONTEND DEPLOY
```

## 1) Workspace Check

If any `tailor` command fails with an authentication error, run login:

```bash
pnpm exec tailor login
```

If `backend/.env` does not contain `TAILOR_PLATFORM_WORKSPACE_ID`, ask the user before creating a workspace:

1. Confirm they want to create a new workspace
2. Ask for the workspace name and region (`us-west` or `asia-northeast`)
3. Create the workspace and save the ID to `.env`:

```bash
cd backend
pnpm exec tailor workspace create --name <workspace-name> --region <region>
pnpm exec tailor workspace list --json
# Extract the workspace ID from the output and save it
echo "TAILOR_PLATFORM_WORKSPACE_ID=<workspace-id>" >> .env
```

## 2) Backend Deploy (`{app_name}/backend`)

Default script:

- `"deploy": "tailor deploy --env-file-if-exists .env"`

Run:

```bash
cd backend
pnpm run deploy
```

Verify:

```bash
pnpm exec tailor show --env-file-if-exists .env --json
```

## 3) Seed Data

Default scripts:

- `"seed": "tailor seed apply --env-file-if-exists .env"`
- `"seed:reset": "tailor seed apply --truncate --yes --env-file-if-exists .env"`

For the initial deploy:

```bash
cd backend
pnpm run seed
```

For subsequent deploys, confirm with the user first since `seed:reset` truncates existing data:

```bash
cd backend
pnpm run seed:reset
```

## 4) Frontend Environment Setup

The frontend requires `frontend/.env` with two variables. See `frontend/.env.example` for reference.

- `VITE_TAILOR_APP_URL` — the app URL from `pnpm exec tailor show --env-file-if-exists .env --json` (run in backend)
- `VITE_TAILOR_CLIENT_ID` — the client ID from `pnpm exec tailor oauth2client get default --env-file-if-exists .env --json` (run in backend)

If `frontend/.env` does not exist, create it from these values.

## 5) Frontend Build + Deploy

Default scripts:

- `"build": "vite build"` (frontend)
- `"deploy:frontend": "tailor staticwebsite deploy --dir ../frontend/dist --name default-web --env-file-if-exists .env"` (backend)

Run:

```bash
cd frontend
pnpm run build
cd ../backend
pnpm run deploy:frontend
```