|
| 1 | +# Deployment Guide |
| 2 | + |
| 3 | +このプロジェクトのデプロイ手順書(Vercel + Render構成) |
| 4 | + |
| 5 | +## 🏗️ アーキテクチャ |
| 6 | + |
| 7 | +- **フロントエンド**: Vercel (Next.js) |
| 8 | +- **バックエンド**: Render (FastAPI + PostgreSQL) |
| 9 | + |
| 10 | +## 📋 前提条件 |
| 11 | + |
| 12 | +1. GitHubリポジトリがpublicまたはVercel/Renderに接続可能 |
| 13 | +2. OpenAI APIキー |
| 14 | +3. GitHub OAuth Appの作成 |
| 15 | + |
| 16 | +## 🚀 デプロイ手順 |
| 17 | + |
| 18 | +### 1. バックエンドのデプロイ (Render) |
| 19 | + |
| 20 | +#### 1.1 Renderダッシュボード |
| 21 | + |
| 22 | +1. https://dashboard.render.com/ にアクセス |
| 23 | +2. "New +" → "Web Service" を選択 |
| 24 | +3. GitHubリポジトリを接続 |
| 25 | + |
| 26 | +#### 1.2 Web Service設定 |
| 27 | + |
| 28 | +- **Name**: `team29-backend` |
| 29 | +- **Region**: Oregon (Free) |
| 30 | +- **Branch**: `main` または `develop` |
| 31 | +- **Root Directory**: `backend` |
| 32 | +- **Runtime**: Python 3 |
| 33 | +- **Build Command**: `pip install poetry && poetry install --no-dev` |
| 34 | +- **Start Command**: `poetry run uvicorn app.main:app --host 0.0.0.0 --port $PORT` |
| 35 | + |
| 36 | +#### 1.3 環境変数の設定 |
| 37 | + |
| 38 | +Renderダッシュボード → Environment で以下を設定: |
| 39 | + |
| 40 | +```bash |
| 41 | +# Database (PostgreSQL推奨) |
| 42 | +DATABASE_URL=postgresql://user:password@host/dbname |
| 43 | + |
| 44 | +# CORS設定(フロントエンドURLに更新) |
| 45 | +BACKEND_CORS_ORIGINS=["https://your-app.vercel.app"] |
| 46 | + |
| 47 | +# 暗号化キー(以下コマンドで生成) |
| 48 | +# python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())" |
| 49 | +ENCRYPTION_KEY=<生成した32バイトbase64文字列> |
| 50 | + |
| 51 | +# Admin API Key(任意の安全な文字列) |
| 52 | +ADMIN_API_KEY=<ランダム文字列> |
| 53 | + |
| 54 | +# LLM設定 |
| 55 | +LLM_PROVIDER=openai |
| 56 | +OPENAI_API_KEY=sk-proj-xxxxxxxxxxxxx |
| 57 | +OPENAI_MODEL=gpt-4o-mini |
| 58 | + |
| 59 | +# JWT設定(以下コマンドで生成) |
| 60 | +# openssl rand -hex 32 |
| 61 | +JWT_SECRET_KEY=<64文字の16進数文字列> |
| 62 | +JWT_ALGORITHM=HS256 |
| 63 | +JWT_EXPIRE_HOURS=24 |
| 64 | + |
| 65 | +# GitHub OAuth(次のステップで設定) |
| 66 | +GITHUB_CLIENT_ID=<後で設定> |
| 67 | +GITHUB_CLIENT_SECRET=<後で設定> |
| 68 | + |
| 69 | +# フロントエンドURL(Vercelデプロイ後に更新) |
| 70 | +FRONTEND_URL=https://your-app.vercel.app |
| 71 | + |
| 72 | +# Skill Tree Cache |
| 73 | +SKILL_TREE_CACHE_MINUTES=10 |
| 74 | +``` |
| 75 | + |
| 76 | +#### 1.4 PostgreSQLデータベースの作成 |
| 77 | + |
| 78 | +1. Render Dashboard → "New +" → "PostgreSQL" |
| 79 | +2. **Name**: `team29-db` |
| 80 | +3. **Database**, **User**, **Password**を設定 |
| 81 | +4. "Create Database" |
| 82 | +5. 生成された`DATABASE_URL`をコピーして、Web ServiceのDATABASE_URLに設定 |
| 83 | + |
| 84 | +#### 1.5 デプロイ |
| 85 | + |
| 86 | +"Create Web Service"をクリックしてデプロイ開始 |
| 87 | + |
| 88 | +デプロイが完了したら、バックエンドのURLをメモ: |
| 89 | + |
| 90 | +``` |
| 91 | +https://team29-backend.onrender.com |
| 92 | +``` |
| 93 | + |
| 94 | +### 2. GitHub OAuth Appの作成 |
| 95 | + |
| 96 | +1. https://github.com/settings/developers にアクセス |
| 97 | +2. "New OAuth App"をクリック |
| 98 | +3. 以下を設定: |
| 99 | + - **Application name**: Team29 Skill Tree |
| 100 | + - **Homepage URL**: `https://your-app.vercel.app` |
| 101 | + - **Authorization callback URL**: `https://team29-backend.onrender.com/api/v1/auth/github/callback` |
| 102 | +4. "Register application"をクリック |
| 103 | +5. **Client ID**と**Client Secret**をコピー |
| 104 | +6. RenderのEnvironment変数に設定: |
| 105 | + - `GITHUB_CLIENT_ID`: <コピーしたClient ID> |
| 106 | + - `GITHUB_CLIENT_SECRET`: <コピーしたClient Secret> |
| 107 | + |
| 108 | +### 3. フロントエンドのデプロイ (Vercel) |
| 109 | + |
| 110 | +#### 3.1 Vercelダッシュボード |
| 111 | + |
| 112 | +1. https://vercel.com/new にアクセス |
| 113 | +2. GitHubリポジトリを接続 |
| 114 | + |
| 115 | +#### 3.2 プロジェクト設定 |
| 116 | + |
| 117 | +- **Framework Preset**: Next.js |
| 118 | +- **Root Directory**: `frontend` |
| 119 | +- **Build Command**: `npm run build` |
| 120 | +- **Install Command**: `npm install` |
| 121 | + |
| 122 | +#### 3.3 環境変数の設定 |
| 123 | + |
| 124 | +"Environment Variables"で以下を設定: |
| 125 | + |
| 126 | +```bash |
| 127 | +NEXT_PUBLIC_API_URL=https://team29-backend.onrender.com |
| 128 | +``` |
| 129 | + |
| 130 | +#### 3.4 デプロイ |
| 131 | + |
| 132 | +"Deploy"をクリック |
| 133 | + |
| 134 | +デプロイが完了したら、フロントエンドのURLをメモ: |
| 135 | + |
| 136 | +``` |
| 137 | +https://your-app.vercel.app |
| 138 | +``` |
| 139 | + |
| 140 | +### 4. CORS設定の更新 |
| 141 | + |
| 142 | +1. RenderのDashboard → team29-backend → Environment |
| 143 | +2. `BACKEND_CORS_ORIGINS`を更新: |
| 144 | + ``` |
| 145 | + ["https://your-app.vercel.app"] |
| 146 | + ``` |
| 147 | +3. `FRONTEND_URL`を更新: |
| 148 | + ``` |
| 149 | + https://your-app.vercel.app |
| 150 | + ``` |
| 151 | +4. "Save Changes" → サービスが自動的に再デプロイされます |
| 152 | + |
| 153 | +### 5. GitHub OAuth Appの更新 |
| 154 | + |
| 155 | +1. https://github.com/settings/developers |
| 156 | +2. 作成したOAuth Appを選択 |
| 157 | +3. **Homepage URL**を更新: `https://your-app.vercel.app` |
| 158 | +4. **Authorization callback URL**は変更不要(バックエンドのURL) |
| 159 | + |
| 160 | +### 6. データベースマイグレーション |
| 161 | + |
| 162 | +RenderのShellまたはローカルから実行: |
| 163 | + |
| 164 | +```bash |
| 165 | +# RenderのShell(Dashboard → Shell) |
| 166 | +cd backend |
| 167 | +poetry run alembic upgrade head |
| 168 | +``` |
| 169 | + |
| 170 | +## ✅ 動作確認 |
| 171 | + |
| 172 | +1. フロントエンドURL(https://your-app.vercel.app)にアクセス |
| 173 | +2. "GitHubでログイン"をクリック |
| 174 | +3. GitHub OAuth認証が成功すること |
| 175 | +4. ランク判定が実行されること |
| 176 | +5. スキルツリーが表示されること |
| 177 | +6. AI問題生成が動作すること |
| 178 | + |
| 179 | +## 🔧 トラブルシューティング |
| 180 | + |
| 181 | +### CORSエラー |
| 182 | + |
| 183 | +``` |
| 184 | +Access to fetch at 'https://team29-backend.onrender.com/api/v1/...' from origin 'https://your-app.vercel.app' has been blocked by CORS policy |
| 185 | +``` |
| 186 | + |
| 187 | +→ RenderのBACKEND_CORS_ORIGINSにフロントエンドのURLが正しく設定されているか確認 |
| 188 | + |
| 189 | +### OAuth認証エラー |
| 190 | + |
| 191 | +``` |
| 192 | +GitHub OAuth failed: redirect_uri_mismatch |
| 193 | +``` |
| 194 | + |
| 195 | +→ GitHub OAuth AppのAuthorization callback URLがバックエンドのURLと一致しているか確認 |
| 196 | + |
| 197 | +### Database接続エラー |
| 198 | + |
| 199 | +``` |
| 200 | +sqlalchemy.exc.OperationalError: could not connect to server |
| 201 | +``` |
| 202 | + |
| 203 | +→ DATABASEURLが正しく設定されているか、PostgreSQLサービスが起動しているか確認 |
| 204 | + |
| 205 | +### LLM API エラー |
| 206 | + |
| 207 | +``` |
| 208 | +Quest generation failed: API key invalid |
| 209 | +``` |
| 210 | + |
| 211 | +→ OPENAI_API_KEYが正しく設定されているか、クレジットが残っているか確認 |
| 212 | + |
| 213 | +## 📊 モニタリング |
| 214 | + |
| 215 | +- **Render Logs**: Dashboard → Logs |
| 216 | +- **Vercel Logs**: Dashboard → Deployments → Logs |
| 217 | +- **エラートラッキング**: RenderのMetricsタブで確認 |
| 218 | + |
| 219 | +## 💰 コスト |
| 220 | + |
| 221 | +- **Vercel**: フリープラン(Hobby) |
| 222 | +- **Render**: |
| 223 | + - Web Service: フリープラン(750時間/月、スリープあり) |
| 224 | + - PostgreSQL: フリープラン(90日間、その後$7/月) |
| 225 | + |
| 226 | +## 🔄 更新デプロイ |
| 227 | + |
| 228 | +1. `git push origin main`(またはdevelop) |
| 229 | +2. Vercel/Renderが自動的に検知して再デプロイ |
| 230 | +3. 環境変数の変更は手動でDashboardから |
| 231 | + |
| 232 | +## 🔐 セキュリティチェックリスト |
| 233 | + |
| 234 | +- [ ] JWT_SECRET_KEYは十分にランダムか(64文字以上推奨) |
| 235 | +- [ ] ENCRYPTION_KEYはFernet.generate_key()で生成したものか |
| 236 | +- [ ] ADMIN_API_KEYは推測不可能なランダム文字列か |
| 237 | +- [ ] OPENAI_API_KEYは本番用のキーか |
| 238 | +- [ ] GITHUB_CLIENT_SECRETは絶対に公開していないか |
| 239 | +- [ ] BACKEND_CORS_ORIGINSは"\*"ではなく具体的なURLか |
| 240 | +- [ ] PostgreSQLのパスワードは安全か |
| 241 | +- [ ] .envファイルが.gitignoreに含まれているか |
| 242 | + |
| 243 | +## 📝 参考リンク |
| 244 | + |
| 245 | +- [Vercel Documentation](https://vercel.com/docs) |
| 246 | +- [Render Documentation](https://render.com/docs) |
| 247 | +- [FastAPI Deployment](https://fastapi.tiangolo.com/deployment/manual/) |
| 248 | +- [Next.js Deployment](https://nextjs.org/docs/deployment) |
0 commit comments