CI/CD Pipeline dari Nol Pakai GitHub Actions: Deploy Otomatis Setiap Push
Bayangkan: setiap kali push ke main, kode otomatis di-test, di-build, dan di-deploy ke production. Ini bukan kemewahan — ini standar industri yang bisa kamu setup gratis.
CI/CD (Continuous Integration / Continuous Deployment) mengotomasi proses yang developer lakukan secara manual: test, build, dan deploy. GitHub Actions membuatnya accessible untuk semua orang — termasuk project personal.
Konsep CI/CD
- Continuous Integration — setiap push otomatis menjalankan test untuk pastikan tidak ada yang rusak
- Continuous Delivery — setelah test pass, artifact siap di-deploy (butuh persetujuan manual)
- Continuous Deployment — setelah test pass, langsung deploy otomatis ke production
Struktur GitHub Actions
Buat file di .github/workflows/deploy.yml:
name: Test and Deploy
# Trigger: jalankan workflow ini kapan?
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
# 1. Checkout kode
- name: Checkout repository
uses: actions/checkout@v4
# 2. Setup Node.js
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "npm"
# 3. Install dependencies
- name: Install dependencies
run: npm ci
# 4. Run linter
- name: Run ESLint
run: npm run lint
# 5. Run tests
- name: Run tests
run: npm test
# 6. Build
- name: Build application
run: npm run build
deploy:
needs: test # Hanya jalan kalau job test berhasil
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' # Hanya deploy dari branch main
steps:
- uses: actions/checkout@v4
- name: Deploy ke Vercel
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"
Setup Secrets
Secrets (API key, token, password) tidak boleh di-hardcode. Simpan di GitHub:
- Repository → Settings → Secrets and variables → Actions
- New repository secret
- Akses di workflow:
${{ secrets.NAMA_SECRET }}
Contoh Workflow dengan Database Testing
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15
env:
POSTGRES_USER: testuser
POSTGRES_PASSWORD: testpass
POSTGRES_DB: testdb
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "20"
- run: npm ci
- name: Run migration
run: npm run db:migrate
env:
DATABASE_URL: postgresql://testuser:testpass@localhost:5432/testdb
- name: Run tests
run: npm test
env:
DATABASE_URL: postgresql://testuser:testpass@localhost:5432/testdb
Badge Status di README
[](https://github.com/username/repo/actions/workflows/deploy.yml)