8 tháng 2, 2026 · 12 phút đọc
CI/CD với GitHub Actions cho Node.js: từ commit đến production
Pipeline hoàn chỉnh: lint, test, build, Docker image, và deploy — với caching strategies để giữ pipeline nhanh.
Một CI/CD pipeline tốt giúp team merge tự tin hơn và deploy thường xuyên hơn. GitHub Actions có ecosystem phong phú và tích hợp native với GitHub repos — đây là lựa chọn mặc định cho hầu hết projects hiện nay.
Cấu trúc workflow file
Tổ chức thành jobs riêng biệt: lint, test, build, deploy. Jobs có thể chạy parallel (needs dependency graph) hoặc sequential. Tách biệt giúp fail fast — nếu lint fail, không cần chạy test expensive.
Trigger đúng events: pull_request cho CI checks, push to main cho deployment. Dùng paths filter để skip unnecessary runs khi chỉ docs thay đổi. concurrency group để cancel old runs khi có push mới.
Caching để tăng tốc
Cache node_modules với actions/cache, key theo hash của package-lock.json. npm ci + cache giúp install step từ 2-3 phút xuống 10-15 giây. Docker layer cache với buildx và cache-from/cache-to GitHub Actions Cache.
Environment Variables và Secrets
GitHub Secrets cho sensitive values (API keys, passwords). Environment variables cho non-sensitive config (APP_ENV, LOG_LEVEL). Dùng environments feature để có separate secrets cho staging vs production, với required reviewers cho production deploys.
Docker Build và Push
actions/setup-buildx-action + docker/login-action + docker/build-push-action là combo chuẩn. Tag image với commit SHA (immutable) và branch name (mutable pointer). Build multi-platform (amd64 + arm64) để support cả x86 servers và Apple Silicon macs.
Deploy Strategy
Với container-based deployments: update image tag trong Kubernetes manifest hoặc ECS task definition. Dùng deployment strategies như rolling update hoặc blue/green để zero-downtime. Health checks phải pass trước khi old instances bị terminate.