Bạn vừa chuyển notes-api (Node.js) sang GitLab CI. Giờ hãy tự làm lại toàn bộ quy trình đó cho một dự án khác ngôn ngữ: một API Python viết bằng FastAPI, test bằng pytest, dùng PostgreSQL. Mục tiêu là chứng minh bạn nắm được các khái niệm (stage, job, rules, services, cache, artifact, báo cáo, environment, when: manual), không chỉ chép lại cú pháp của bài trước.
todo-api/
├── app/
│ ├── __init__.py
│ ├── main.py # FastAPI: GET /health, GET/POST /todos
│ ├── db.py # kết nối PostgreSQL qua biến DATABASE_URL
│ └── validate.py # hàm thuần validate_todo(data)
├── tests/
│ ├── test_validate.py # unit test, không cần DB
│ └── test_todos.py # cần PostgreSQL thật (đọc DATABASE_URL)
├── migrations/001_init.sql
├── scripts/migrate.py # chạy các file .sql trong migrations/
├── pyproject.toml # cấu hình ruff và pytest
├── requirements.txt # fastapi, uvicorn, psycopg[binary]
├── requirements-dev.txt # -r requirements.txt, pytest, pytest-cov, httpx, ruff
└── Dockerfile # image production, cổng 8000Các lệnh đã chạy được trên máy dev:
pip install -r requirements-dev.txt
ruff check .
ruff format --check .
python scripts/migrate.py
pytest --cov=app --cov-report=term --cov-report=xml:coverage.xml --junitxml=report.xml
docker build -t todo-api .Project nằm ở gitlab.com/your-group/todo-api, nhánh mặc định main đã được bảo vệ. Server production todo.example.com có sẵn script /srv/todo-api/deploy.sh <tag> (giống deploy.sh của notes-api), user SSH là deploy. Các biến sau đã được khai báo ở Settings → CI/CD → Variables, đều Protected:
| Biến | Loại | Ghi chú |
|---|---|---|
SSH_PRIVATE_KEY | File | Khóa riêng của user deploy |
SSH_KNOWN_HOSTS | File | known_hosts đã kiểm tra |
DEPLOY_HOST | Variable | todo.example.com, environment scope production |
Viết file .gitlab-ci.yml đáp ứng mọi mục sau:
Khi nào tạo pipeline
Cấu trúc chung
lint, test, build, deploy.python:3.13-slim; job lint/test bị hủy được khi có pipeline mới hơn.requirements-dev.txt thay đổi. Nhớ: GitLab chỉ cache được thư mục bên trong project.Stage lint
lint chạy ruff check . và ruff format --check ., bắt đầu ngay khi pipeline chạy (không chờ stage nào).Stage test
test có service postgres:18 với alias db; khởi tạo database todo, user todo, mật khẩu chỉ dùng cho CI.DATABASE_URL trỏ tới service đó; chạy migration trước khi chạy pytest.coverage.xml (Cobertura) và report.xml (JUnit).coverage_report).test cũng không chờ lint.Stage build
build build image bằng Docker-in-Docker (TLS bật), push lên $CI_REGISTRY_IMAGE với tag là SHA rút gọn của commit.lint và test đều thành công.Stage deploy
deploy-production chỉ chạy trên nhánh mặc định, chờ người bấm, sau khi build thành công.production, URL https://todo.example.com.known_hosts, gọi deploy.sh với đúng tag vừa build, sau đó gọi https://todo.example.com/health (thử lại vài lần) để xác nhận.lint và test song song, build phụ thuộc cả hai, deploy-production phụ thuộc build.lint và test; trang MR hiện phần trăm coverage và tóm tắt kết quả test.main: pipeline có thêm build (image mới xuất hiện ở Deploy → Container registry) và deploy-production ở trạng thái chờ bấm.lint, test, không có build và deploy-production.TIP
Nếu chưa có server, thay lệnh ssh bằng echo để luyện phần còn lại; mọi phần khác (service, báo cáo, registry, manual job) đều chạy được trên GitLab.com với runner miễn phí.
# .gitlab-ci.yml cho todo-api
# TODO 1: workflow:rules - pipeline cho MR, nhánh, tag; không trùng khi nhánh đã có MR
workflow:
rules: []
# TODO 2: bốn stage theo thứ tự
stages: []
default:
image: python:3.13-slim
# TODO 3: cho phép hủy job khi có pipeline mới hơn
variables:
# TODO 4: đưa cache của pip vào trong thư mục project
PIP_CACHE_DIR: ""
# Khuôn dùng chung cho job Python
.python:
# TODO 5: cache theo requirements-dev.txt
before_script:
- pip install -r requirements-dev.txt
lint:
extends: .python
stage: lint
# TODO 6: chạy ngay, không chờ ai; ruff check + ruff format --check
script: []
test:
extends: .python
stage: test
# TODO 7: service postgres:18 alias db, biến POSTGRES_*, DATABASE_URL
# TODO 8: migrate rồi pytest có coverage + junit
# TODO 9: regex coverage, artifacts reports (junit, coverage_report), when: always, expire_in
script: []
build:
stage: build
# TODO 10: image docker + service dind, DOCKER_TLS_CERTDIR
# TODO 11: login bằng CI_REGISTRY_USER / CI_REGISTRY_PASSWORD qua stdin
# TODO 12: build và push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
# TODO 13: chỉ trên nhánh mặc định, sau lint và test
script: []
deploy-production:
stage: deploy
# TODO 14: image alpine, cài openssh-client và curl
# TODO 15: environment production + url, resource_group, interruptible: false
# TODO 16: ssh gọi deploy.sh, rồi kiểm tra /health
# TODO 17: chỉ nhánh mặc định, when: manual, sau build
script: []
Comments