# 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-sdk` command fails with an authentication error, run login:

```bash
pnpm exec tailor-sdk 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-sdk workspace create --name <workspace-name> --region <region>
pnpm exec tailor-sdk 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-sdk apply --env-file-if-exists .env"`

Run:

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

Verify:

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

## 3) Seed Data

Default scripts:

- `"seed": "node --env-file=.env seed/exec.mjs"`
- `"seed:reset": "node --env-file=.env seed/exec.mjs --truncate --yes"`

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-sdk show --env-file-if-exists .env --json` (run in backend)
- `VITE_TAILOR_CLIENT_ID` — the client ID from `pnpm exec tailor-sdk 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-sdk 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
```