---
title: "Tự động hóa deploy ứng dụng Next.js lên Cloud VPS bằng GitHub Actions"
description: "Hướng dẫn thiết lập CI/CD với GitHub Actions để build Docker image Next.js standalone, push lên GHCR và tự động deploy lên Cloud VPS không gián đoạn."
canonical: "https://thuecloud.vn/tu-dong-hoa-deploy-ung-dung-next-js-len-cloud-vps-bang-github-actions"
markdown_url: "https://thuecloud.vn/tu-dong-hoa-deploy-ung-dung-next-js-len-cloud-vps-bang-github-actions.md"
locale: "vi"
kind: "article"
published_at: "2026-11-22T16:18:17.538Z"
updated_at: "2026-11-22T16:18:15.649Z"
site: "ThueCloud.Vn"
estimated_tokens: 1963
---
# Tự động hóa deploy ứng dụng Next.js lên Cloud VPS bằng GitHub Actions

> Hướng dẫn thiết lập CI/CD với GitHub Actions để build Docker image Next.js standalone, push lên GHCR và tự động deploy lên Cloud VPS không gián đoạn.

- Canonical HTML: https://thuecloud.vn/tu-dong-hoa-deploy-ung-dung-next-js-len-cloud-vps-bang-github-actions
- Markdown URL: https://thuecloud.vn/tu-dong-hoa-deploy-ung-dung-next-js-len-cloud-vps-bang-github-actions.md
- Organization: ThueCloud.Vn
- Support Email: support@thuecloud.vn
- Hotline: 03333 499 48
- Published: 2026-11-22
- Updated: 2026-11-22

## Mục lục

- Chuẩn bị Dockerfile Multi-stage cho Next.js Standalone
- Cấu hình Workflow .github/workflows/deploy.yml
- So sánh Deploy trực tiếp trên VPS vs Build qua GitHub Actions
- Câu hỏi thường gặp (FAQ)
  - Biến môi trường NEXT_PUBLIC_* xử lý thế nào khi build trên GitHub Actions?
  - Làm sao để không rớt request nào trong 2 giây docker compose up -d?
  - Có cần mở quyền root cho SSH key trong GitHub Actions không?

Việc SSH vào [Cloud VPS Việt Nam](https://thuecloud.vn/vps-viet-nam) để gõ `git pull` và `npm run build` thủ công không chỉ tốn thời gian mà còn dễ làm gián đoạn website khi quá trình biên dịch Next.js ngốn hết CPU/RAM. Giải pháp chuẩn là chuyển toàn bộ bước build sang GitHub Actions, đóng gói ở chế độ `standalone` bằng Docker multi-stage và tự động triển khai xuống VPS.

> **Tóm tắt nhanh (TL;DR):**
> - Bật `output: 'standalone'` trong `next.config.mjs` để giảm dung lượng image cuối xuống còn ~120–180 MB.
> - Build và push Docker image lên GitHub Container Registry (GHCR) ngay trên runner của GitHub.
> - VPS chỉ việc `docker compose pull` và `up -d`, không tốn CPU/RAM để biên dịch.
> - *Cập nhật: 11/2026*

## Chuẩn bị `Dockerfile` Multi-stage cho Next.js Standalone

Nếu không dùng multi-stage build, image Node.js có thể phình to tới 1.5–2 GB (tương tự lỗi thường gặp trong bài [build Docker image nặng 2GB vì thiếu multi-stage](https://thuecloud.vn/sua-loi-vibe-coding-khi-deploy-app-ai-len-vps-build-docker-image-nang-2gb-vi-thieu-multi-stage)). Trước tiên, hãy bật `output: 'standalone'` trong `next.config.mjs`, sau đó tạo `Dockerfile` tối ưu:

```dockerfile
FROM node:22-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:22-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
RUN addgroup -g 1001 -S nodejs && adduser -S nextjs -u 1001
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
CMD ["node", "server.js"]
```

## Cấu hình Workflow `.github/workflows/deploy.yml`

Workflow dưới đây tự động chạy mỗi khi bạn push vào nhánh `main`, đẩy image lên GHCR và kết nối SSH tới VPS để cập nhật container:

```yaml
name: Deploy Next.js to VPS
on:
  push:
    branches: ["main"]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v4
      - uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - uses: docker/build-push-action@v6
        with:
          push: true
          tags: ghcr.io/${{ github.repository }}:latest
      - name: Deploy via SSH
        uses: appleboy/ssh-action@v1.0.3
        with:
          host: ${{ secrets.VPS_HOST }}
          username: ${{ secrets.VPS_USER }}
          key: ${{ secrets.VPS_SSH_KEY }}
          script: |
            cd /opt/nextjs-app
            docker compose pull web
            docker compose up -d --remove-orphans
            docker image prune -f
```

## So sánh Deploy trực tiếp trên VPS vs Build qua GitHub Actions

| Tiêu chí | Build trực tiếp trên VPS | Build qua GitHub Actions + GHCR |
| :--- | :--- | :--- |
| Tiêu thụ CPU/RAM VPS | Tăng vọt 100% lúc build, dễ treo web | Gần như bằng 0, VPS chỉ pull layer mới |
| Thời gian downtime | 30–120 giây khi rebuild | Dưới 2 giây khi đổi container |
| Lưu trữ lịch sử bản cũ | Khó rollback nhanh | Rollback tức thì theo tag SHA |
| Yêu cầu ổ đĩa | Cần cache `node_modules` lớn | Xem thêm [phân tích NVMe Gen4 vs Gen5](https://thuecloud.vn/phan-tich-ky-thuat-nvme-gen4-vs-nvme-gen5-vs-sas-ssd-khi-nao-nen-dung-trong-thuc-te) |

Nếu ứng dụng Next.js của bạn kết nối với Headless CMS, hãy xem thêm hướng dẫn [thiết lập sao lưu tự động cho Strapi/Directus](https://thuecloud.vn/thiet-lap-sao-luu-tu-dong-va-khoi-phuc-nhanh-cho-headless-cms-strapi-directus) và tham khảo mô hình triển khai tại [Giải pháp ThueCloud](https://thuecloud.vn/giai-phap).

> **Hạ tầng tối ưu cho Next.js & Docker tại ThueCloud.Vn:**
> Chạy container mượt mà trên **Cloud VPS KVM Enterprise NVMe** đặt tại `vn-hn`, **VAT 0%**, **SLA 99,9%**, **hoàn tiền 07 ngày**. Gói **VPS VN 1** (`45.000đ`–`60.000đ`/tháng) đủ chạy Next.js standalone nhẹ; **VPS VN 2** (`78.000đ`–`90.000đ`/tháng) và **VPS VN 3** (`139.000đ`–`170.000đ`/tháng) lý tưởng cho stack Next.js + Nginx + Redis. Tham khảo tại [Bảng giá ThueCloud](https://thuecloud.vn/pricing).

## Câu hỏi thường gặp (FAQ)

### Biến môi trường `NEXT_PUBLIC_*` xử lý thế nào khi build trên GitHub Actions?
Các biến `NEXT_PUBLIC_*` được nhúng tĩnh vào bundle client ngay tại thời điểm `npm run build`. Bạn cần truyền chúng vào bước `docker/build-push-action` thông qua `build-args` lấy từ GitHub Repository Variables/Secrets.

### Làm sao để không rớt request nào trong 2 giây `docker compose up -d`?
Bạn có thể cấu hình `healthcheck` trong `docker-compose.yml` kết hợp công cụ điều phối nhẹ như Caddy/Traefik hoặc chạy 2 container luân phiên (blue-green) sau Nginx upstream.

### Có cần mở quyền root cho SSH key trong GitHub Actions không?
Không nên dùng `root`. Hãy tạo một user deploy riêng (ví dụ `deployer`), thêm vào group `docker`, và chỉ cấp quyền ghi trên thư mục `/opt/nextjs-app`.

## References / Sources

- https://nextjs.org/docs/app/building-your-application/deploying#docker-image
- https://docs.github.com/en/actions/use-cases-and-examples/deploying/deploying-to-your-cloud-server
- https://docs.docker.com/build/building/multi-stage/

---

## Navigation & Machine-Readable Endpoints

- [Site Summary (llms.txt)](https://thuecloud.vn/llms.txt)
- [Full Technical Reference (llms-full.txt)](https://thuecloud.vn/llms-full.txt)
- [Pricing Catalog (Markdown)](https://thuecloud.vn/pricing.md)
- [Blog & Technical Guides (Markdown)](https://thuecloud.vn/blog.md)
- [XML Sitemap](https://thuecloud.vn/sitemap.xml)
