CI/CD Pipeline dengan GitHub Actions
Setelah bisa Dockerize aplikasi, langkah selanjutnya adalah otomasi prosesnya. Di sinilah CI/CD berperan.
Apa itu CI/CD?
- CI (Continuous Integration) — otomasi build & test setiap ada perubahan kode
- CD (Continuous Delivery/Deployment) — otomasi pengiriman kode ke server/production
Tujuannya: dari "push kode → deploy manual" menjadi "push kode → otomatis live".
GitHub Actions — Cara Kerjanya
GitHub Actions menggunakan file .yml yang disimpan di .github/workflows/. File ini mendefinisikan kapan dan apa yang dijalankan.
Trigger (push, PR, schedule)
↓
Job 1: Test
↓
Job 2: Build Docker Image
↓
Job 3: Push ke Registry
↓
Job 4: Deploy ke Server
Contoh Workflow Nyata
File: .github/workflows/deploy.yml
name: CI/CD Pipeline
on:
push:
branches: [main]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Build Docker image
run: docker build -t my-app:${{ github.sha }} .
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Push image
run: |
docker push my-app:${{ github.sha }}
docker tag my-app:${{ github.sha }} my-app:latest
docker push my-app:latest
- name: Deploy ke server
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_KEY }}
script: |
docker pull my-app:latest
docker stop my-app || true
docker rm my-app || true
docker run -d -p 3000:3000 --name my-app my-app:latest
Secrets — Jangan Hardcode Credential!
Simpan data sensitif sebagai GitHub Secrets di Settings > Secrets and variables > Actions.
DOCKER_USERNAME → username Docker Hub
DOCKER_TOKEN → access token Docker Hub
SERVER_HOST → IP server production
SERVER_USER → user SSH (biasanya ubuntu)
SSH_KEY → private key SSH
Pelajaran yang Dipetik
- Matrix testing — test di beberapa versi Node.js sekaligus
- Cache — simpan
node_modulescache agar pipeline lebih cepat - Environments — pisahkan staging dan production
- Artifacts — simpan hasil build untuk debugging
Perbandingan Tools CI/CD
| Tool | Keunggulan |
|---|---|
| GitHub Actions | Free, terintegrasi GitHub, mudah |
| GitLab CI | Self-hosted, cocok untuk enterprise |
| Jenkins | Sangat fleksibel, banyak plugin |
| ArgoCD | GitOps native, cocok untuk K8s |
Next: belajar ArgoCD untuk GitOps-based deployment ke Kubernetes.