hoàn thành docker compose giai đoạn 1

This commit is contained in:
2026-03-31 16:29:03 +07:00
parent 5baea90d61
commit 7da144286f
8 changed files with 211 additions and 2 deletions
+83 -1
View File
@@ -51,4 +51,86 @@ Please use the following technologies for this project:
Let's build this step-by-step to avoid context limits.
1. First, analyze this spec and confirm you understand.
2. Provide the database schema models (SQLAlchemy or Prisma).
3. Wait for my confirmation before writing the Backend API routes.
3. Wait for my confirmation before writing the Backend API routes.
---
## 7. CI/CD — Push lên GitHub Container Registry (GHCR)
### 7.1. Yêu cầu
- **Docker** đã cài và đang chạy
- **GitHub Personal Access Token (PAT)** với quyền `write:packages``read:packages`
- Tạo tại: https://github.com/settings/tokens → *Generate new token (classic)*
- File `.env` đã được cấu hình (xem `.env.example`)
### 7.2. Cấu hình `.env`
Thêm các dòng sau vào file `.env` (file này đã được gitignore, **không commit**):
```env
GITHUB_USER=<github_username_của_bạn>
GITHUB_TOKEN=<personal_access_token>
BACKEND_IMAGE=ghcr.io/<github_username>/lms-backend:latest
FRONTEND_IMAGE=ghcr.io/<github_username>/lms-frontend:latest
```
### 7.3. Build & Push lên GHCR
```bash
chmod +x push-ghcr.sh
./push-ghcr.sh
```
Script sẽ tự động:
1. Đọc `GITHUB_USER``GITHUB_TOKEN` từ `.env`
2. Đăng nhập vào `ghcr.io`
3. Build cả hai image (`lms-backend`, `lms-frontend`) bằng `docker compose build`
4. Tag và push lên GHCR
Để push với tag cụ thể (ví dụ: version):
```bash
./push-ghcr.sh v1.0.0
```
### 7.4. Deploy trên máy khác
Trên máy đích (server, VPS, máy tính khác):
```bash
# 1. Copy các file cần thiết
scp docker-compose.yml .env.example deploy.sh user@server:/opt/lms/
ssh user@server
# 2. Tạo .env từ example
cd /opt/lms
cp .env.example .env
# Điền các giá trị: POSTGRES_PASSWORD, JWT_SECRET_KEY, GITHUB_USER, GITHUB_TOKEN,
# BACKEND_IMAGE, FRONTEND_IMAGE
# 3. Chạy deploy
chmod +x deploy.sh
./deploy.sh
```
Script `deploy.sh` sẽ:
1. Kiểm tra `.env` hợp lệ
2. Đăng nhập GHCR (nếu có token)
3. Pull image từ GHCR
4. Khởi động toàn bộ stack: `db`, `backend`, `frontend`
### 7.5. Kiểm tra packages trên GitHub
Sau khi push, image sẽ xuất hiện tại:
```
https://github.com/<github_username>?tab=packages
```
> **Lưu ý:** Mặc định packages ở chế độ **Private**. Để máy khác pull mà không cần token, vào
> GitHub → Packages → tên package → *Package settings* → đổi visibility sang **Public**.
### 7.6. Sinh JWT Secret Key
```bash
python3 -c "import secrets; print(secrets.token_hex(32))"
```