Quay lại blog
devops8 phút đọc

CI/CD với GitHub Actions: Pipeline Build, Test và Deploy

Tự động hóa quy trình phát triển phần mềm với GitHub Actions — pipeline, testing, Docker build, deployment và quản lý secrets.

V
Bởi Ventra Rocket
·Đăng ngày 15 tháng 1, 2026
#GitHub Actions#CI/CD#DevOps#Docker#Automation

GitHub Actions giúp CI/CD không cần quản lý server. Hướng dẫn này covers pipeline hoàn chỉnh cho Node.js.

1. Pipeline Test cơ bản

name: CI/CD
on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: "npm"
      - run: npm ci
      - run: npm run lint
      - run: npm run test:ci
        env:
          CI: true

2. Docker Build và Push

  build:
    needs: test
    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@v5
        with:
          context: .
          push: ${{ github.event_name != 'pull_request' }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

3. Matrix Testing — Test nhiều phiên bản

  test-matrix:
    strategy:
      matrix:
        node-version: [18, 20, 22]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci && npm test

4. Quản lý Secrets

env:
  DATABASE_URL: ${{ secrets.DATABASE_URL }}
  JWT_SECRET: ${{ secrets.JWT_SECRET }}

Không bao giờ hardcode secrets — dùng GitHub Secrets hoặc AWS Secrets Manager.

5. Cache để tăng tốc

- uses: actions/cache@v4
  with:
    path: ~/.npm
    key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}

Kết luận

Tách biệt jobs test/build/deploy. Dùng environments cho production gating. Cache npm và Docker layers. Ventra Rocket giảm deploy time từ 30 phút thủ công xuống 5 phút tự động.

Bài viết liên quan

CI/CD với GitHub Actions: Pipeline Build, Test và Deploy | Ventra Rocket