# 🚀 Deployment Guide — GOFLOW Enterprise AI OS Portal

**Deployment & DevOps Guide**
เวอร์ชัน 1.0 · Confidential – GOFLOW Enterprise · อัปเดตล่าสุด: 2026-08-07

---

## 1. สภาพแวดล้อม (Environments)

| Environment | URL ตัวอย่าง | ใช้กับ |
|---|---|---|
| Local | `http://localhost:3000` | Developer |
| Staging | `https://staging.goflow.click` | UAT, QA, Demo ภายใน |
| Production | `https://goflow.click` | ลูกค้า, Investor, Launch |

**กฎ:** ห้าม deploy ตรงเข้า Production — ต้องผ่าน pipeline + Human Approval เสมอ

---

## 2. โครงสร้าง CI/CD Pipeline

```
Git push (feature/*)
   │
   ├─ Lint + Type check
   ├─ Unit Test (Jest)
   ├─ Build (Next.js)
   ├─ AI Code Review (comment ใน PR)
   ├─ Human Review (approve PR)
   │
   ├─ merge → main
   │      ├─ Deploy Staging (อัตโนมัติ)
   │      ├─ E2E + Lighthouse + Security scan
   │      └─ UAT
   │
   └─ Release tag (vX.Y.Z)
          ├─ Deploy Production (Human อนุมัติ / หนึ่งคลิก)
          └─ Smoke test + Rollback standby
```

เครื่องมือแนะนำ: GitHub Actions หรือ GitLab CI (ตามที่ทีม DevOps เลือก)

---

## 3. Docker Compose (Local + Staging)

```yaml
# docker-compose.yml (ตัวอย่าง)
services:
  web:        # Next.js 16
    build: ./apps/web
    ports: ["3000:3000"]
    depends_on: [api]
  api:        # Fastify
    build: ./apps/api
    ports: ["8080:8080"]
    environment:
      DATABASE_URL: postgres://goflow:secret@db:5432/goflow
      REDIS_URL: redis://redis:6379
      MINIO_ENDPOINT: minio:9000
      QDRANT_URL: http://qdrant:6333
      NEXUS_AI_URL: ${NEXUS_AI_URL}
  db:         # PostgreSQL 16
    image: postgres:16
    volumes: [pgdata:/var/lib/postgresql/data]
  redis:
    image: redis:7
  minio:
    image: minio/minio
    command: server /data --console-address ":9001"
  qdrant:
    image: qdrant/qdrant
volumes:
  pgdata:
```

**สั่งรัน:** `docker compose up -d` → เปิด `http://localhost:3000`

---

## 4. Production Topology (แนะนำ)

```
Cloudflare / CDN (HTTPS, WAF)
        │
   Load Balancer
   ┌────┴────┐
 Web-1     Web-2        (Next.js, stateless)
   └────┬────┘
   API-1     API-2      (Fastify)
        │
   ┌────┴─────────────────────┐
 PostgreSQL (HA/Replica)   Redis (cluster)
 MinIO (replicated)        Qdrant (cluster)
```

---

## 5. Nginx / Static Site (สถานะปัจจุบัน)

Portal ฉบับปัจจุบัน deploy เป็น **Static Site บน Function Compute (nginx environment)**:

- ไฟล์ `nginx.conf` ต้องอยู่ root ของ zip — listen port **9000**, root `/code`
- ไฟล์ temp/log/pid ทั้งหมดต้องอยู่ใต้ `/tmp` (filesystem อื่น read-only)
- `error_page 404 /404.html;` + `try_files $uri $uri/ /404.html;`
- อัปโหลดผ่านระบบ AutoClaw managed delivery → Preview URL → กด **Publish** เพื่อขึ้น stable URL

**หมายเหตุ:** สถาปัตยกรรม Next.js + Fastify ในเอกสารนี้คือเป้าหมาย Production จริงสำหรับ commercial launch

---

## 6. ตัวแปร Environment (Secrets)

| ตัวแปร | คำอธิบาย | ระดับ |
|---|---|---|
| `DATABASE_URL` | PostgreSQL connection string | secret |
| `REDIS_URL` | Redis connection | secret |
| `MINIO_*` | MinIO endpoint + credentials | secret |
| `QDRANT_URL` | Qdrant endpoint | secret |
| `NEXUS_AI_URL` | NEXUS AI base URL | secret |
| `OPENAI_API_KEY` | Cloud LLM key (SaaS) | secret |
| `OLLAMA_URL` | Local LLM (Private) | secret |
| `JWT_SECRET` | ลายเซ็น token | secret |
| `NEXT_PUBLIC_API_URL` | API base URL (public) | public |

⚠️ **ห้าม commit .env หรือ secrets ลง Git** — ใช้ Secret Manager ของผู้ให้บริการ + `.env.example` สำหรับ dev

---

## 7. ความปลอดภัย (Security Hardening)

| รายการ | มาตรการ |
|---|---|
| HTTPS | บังคับ + HSTS |
| Headers | Helmet: CSP, X-Frame-Options, X-Content-Type-Options, Referrer-Policy |
| Rate limit | Redis: 100 req/min/IP; AI chat 10 req/min/user |
| Input | Validation (zod/ajv) + Sanitize (XSS) |
| CSRF | Token บน state-changing requests |
| Secrets | ไม่มีในโค้ด, rotate ทุก 90 วัน |
| Container | รันด้วย non-root user, image scan (Trivy) |
| Backup | เข้ารหัส (AES-256) |

---

## 8. Backup & Disaster Recovery

| ข้อมูล | วิธี | ความถี่ | RPO | RTO |
|---|---|---|---|---|
| PostgreSQL | pg_dump + WAL (PITR) | รายวัน + ต่อเนื่อง | 15 นาที | 1 ชม. |
| MinIO | Bucket replication | ต่อเนื่อง | 5 นาที | 30 นาที |
| Redis | Snapshot + AOF | รายชั่วโมง | 1 ชม. | 15 นาที |
| Qdrant | Snapshot + replication | รายวัน | 24 ชม. | 2 ชม. |
| โค้ด | Git + Release bundle | ทุก release | — | 15 นาที |

**Disaster Recovery Drill:** ทุกไตรมาส — ทดสอบ restore จริงใน environment แยก (ดูแนวทางจาก `GOFLOW-AI-Incident-Response.md`)

---

## 9. Monitoring & Observability

| เครื่องมือ | วัดอะไร |
|---|---|
| Prometheus + Grafana | CPU, memory, request rate, error rate, latency |
| Loki / ELK | Logs รวมศูนย์ + tracing |
| Uptime monitor | Availability ≥ 99.9% |
| Sentry | Error tracking ฝั่ง client/server |
| Alert | Slack/Line/อีเมล: error rate spike, disk, backup fail |

**On-call:** DevOps เป็น first responder, AI Monitoring (GOFLOW AI-Monitoring-System) เป็นตัวช่วยคัดกรอง

---

## 10. Rollback Plan

1. ทุก release ใช้ version tag — deploy ใหม่ = เลือก tag เก่าได้
2. Database migration ต้อง **backward compatible** (expand-contract)
3. Feature flag สำหรับฟีเจอร์เสี่ยง
4. ถ้า error rate > 2% หรือ 500 spike ภายใน 15 นาที → rollback ทันที
5. หลัง rollback: ตรวจ root cause + เก็บเป็น postmortem

---

## 11. Launch Checklist (ก่อน Go Live)

- [ ] Staging UAT ผ่าน (ทุกหน้า, ทุกฟอร์ม)
- [ ] E2E regression 100% ผ่าน
- [ ] Lighthouse ≥ 95 หน้าหลัก
- [ ] Security scan ไม่มี critical
- [ ] DNS + HTTPS ทำงาน
- [ ] Monitoring + Alert ทำงาน
- [ ] Backup ระบบแรกสำเร็จ
- [ ] Rollback plan ทดสอบแล้ว
- [ ] ทีม Support รู้ขั้นตอนรับเรื่อง
- [ ] Go/No-Go meeting ผ่าน

---

*เอกสารนี้เป็นส่วนหนึ่งของ GOFLOW Developer Kit — อ่านคู่กับ 02-system-architecture.md และ GOFLOW-Enterprise-Deployment-Model.md*
