Setup CI/CD Sederhana dengan GitHub Actions untuk Project Node.js

Otomasi testing dan deployment project Node.js dengan GitHub Actions — tanpa perlu server CI terpisah, gratis untuk repo publik.
Setup CI/CD Sederhana dengan GitHub Actions untuk Project Node.js

Kenapa CI/CD Itu Penting?

CI/CD (Continuous Integration/Continuous Deployment) memastikan setiap push kode yang masuk sudah melewati serangkaian test otomatis sebelum sampai ke production. Tanpa ini, deploy selalu terasa seperti judi.

Struktur Workflow GitHub Actions

Buat file .github/workflows/ci.yml:

name: CI

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [20, 22]

    steps:
      - uses: actions/checkout@v4

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: npm

      - name: Install dependencies
        run: npm ci

      - name: Run linter
        run: npm run lint

      - name: Run tests
        run: npm test

      - name: Build
        run: npm run build --if-present

Menambahkan Job Deploy ke Vercel

  deploy:
    needs: test
    runs-on: ubuntu-latest
    if: github.ref == "refs/heads/main"

    steps:
      - uses: actions/checkout@v4
      - uses: amondnet/vercel-action@v25
        with:
          vercel-token: ${{ secrets.VERCEL_TOKEN }}
          vercel-org-id: ${{ secrets.VERCEL_ORG_ID }}
          vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }}
          vercel-args: --prod

Tambahkan Secrets di GitHub

Pergi ke Repository → Settings → Secrets and variables → Actions, tambahkan VERCEL_TOKEN, VERCEL_ORG_ID, dan VERCEL_PROJECT_ID dari dashboard Vercel.

Hasil Akhir

Setiap PR otomatis menjalankan lint + test di Node.js 20 dan 22. Setiap merge ke main otomatis deploy ke Vercel production. Tim tidak perlu deploy manual lagi.

Butuh Solusi Digital Custom?

Kami siap membuatkan solusi digital sesuai kebutuhan bisnis Anda.

Konsultasi Gratis