TLDR #
Git hooks memungkinkan deployment otomatis: kamu git push dari laptop, dan server langsung menjalankan script untuk men-deploy kode terbaru. Cara klasiknya memakai bare repository di server dengan hook post-receive yang menyalin kode ke direktori produksi lalu menjalankan build.
Pendahuluan #
Deployment manual yang diulang-ulang rawan lupa langkah. Git hooks mengotomatiskannya: setiap kali ada push, hook tertentu dieksekusi di server.
Ada dua pendekatan umum:
- Bare repo di server — push ke server, hook men-deploy.
- CI/CD service (GitHub Actions, dsb.) — lebih kompleks, untuk tim besar.
Artikel ini fokus pada pendekatan pertama yang sederhana dan cocok untuk server pribadi atau project kecil.
Apa itu Git Hook? #
Git hook adalah script yang dijalankan otomatis oleh Git pada event tertentu. Hook yang relevan untuk deployment:
| Hook | Kapan dijalankan |
|---|---|
post-receive |
Setelah push diterima (di server remote) |
pre-receive |
Sebelum push diterima |
post-commit |
Setelah commit lokal |
post-merge |
Setelah merge lokal |
Untuk deployment server, post-receive adalah yang paling berguna.
Prasyarat #
Siapkan:
- Server dengan Git terpasang.
- Aplikasi/project yang sudah di-hosting di direktori produksi.
- Akses SSH ke server.
Langkah 1: Buat Bare Repository di Server #
Di server:
mkdir -p /var/repo/appku.git
cd /var/repo/appku.git
git init --bareBare repository tidak berisi file kerja — hanya data Git. Ia bertindak sebagai “remote” tempat kamu push.
Langkah 2: Buat Hook post-receive #
Buat file hook:
nano /var/repo/appku.git/hooks/post-receiveIsi dengan script deployment:
#!/bin/bash
TARGET="/var/www/appku"
GIT_DIR="/var/repo/appku.git"
BRANCH="main"
while read oldrev newrev ref; do
if [[ "$ref" == "refs/heads/$BRANCH" ]]; then
echo "Deploying $BRANCH..."
git --work-tree="$TARGET" --git-dir="$GIT_DIR" checkout -f "$BRANCH"
cd "$TARGET" || exit 1
# Jalankan perintah build/install sesuai project
# npm install --production
# npm run build
echo "Deploy selesai."
fi
doneBuat executable:
chmod +x /var/repo/appku.git/hooks/post-receiveLangkah 3: Hubungkan dari Laptop #
Di komputer lokal:
cd project-lokal
git remote add production user@server:/var/repo/appku.git
git push production mainSetiap push ke branch main akan memicu hook dan men-deploy otomatis.
Contoh Hook yang Lebih Lengkap #
Untuk aplikasi Node.js dengan build step:
#!/bin/bash
set -e
TARGET="/var/www/appku"
GIT_DIR="/var/repo/appku.git"
BRANCH="main"
LOG="/var/log/appku-deploy.log"
while read oldrev newrev ref; do
if [[ "$ref" == "refs/heads/$BRANCH" ]]; then
echo "$(date): Deploying commit $newrev" >> "$LOG"
git --work-tree="$TARGET" --git-dir="$GIT_DIR" checkout -f "$BRANCH"
cd "$TARGET" || exit 1
npm install --production >> "$LOG" 2>&1
npm run build >> "$LOG" 2>&1
# Restart service (sesuaikan dengan setup)
# pm2 restart appku
echo "$(date): Deploy selesai" >> "$LOG"
fi
doneset -e menghentikan script jika ada perintah yang gagal, mencegah deploy setengah jalan.
Best Practices #
- Gunakan branch produksi terpisah (misal
main), jangan deploy setiap branch. - Tulis log agar mudah melacak riwayat deploy.
- Jalankan build di hook hanya jika ringan; proses berat sebaiknya offload.
- Backup sebelum deploy jika aplikasi kritis.
- Gunakan
set -eagar berhenti saat error. - Simpan konfigurasi sensitif di environment variable, bukan di repo.
Troubleshooting #
Hook tidak berjalan #
Periksa:
ls -l /var/repo/appku.git/hooks/post-receivePastikan executable (-rwxr-xr-x). Jika tidak:
chmod +x /var/repo/appku.git/hooks/post-receiveHook berjalan tapi checkout gagal #
Pastikan direktori target bisa ditulis oleh user yang menjalankan hook:
sudo chown -R $USER:$USER /var/www/appkuError “Permission denied” saat push #
Pastikan user SSH punya akses tulis ke bare repo:
sudo chown -R $USER:$USER /var/repo/appku.gitDeploy ke branch salah #
Tambahkan pengecekan ref seperti pada contoh, sehingga hanya push ke branch tertentu yang men-trigger deploy.
Alternatif: GitHub Actions (untuk GitHub) #
Jika project di GitHub, kamu bisa pakai GitHub Actions dengan workflow sederhana .github/workflows/deploy.yml:
name: Deploy
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Deploy via SSH
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USER }}
key: ${{ secrets.SSH_KEY }}
script: |
cd /var/www/appku
git pull origin main
npm install --production
pm2 restart appkuSimpan kredensial di GitHub Secrets, jangan hardcode.
Ringkasan #
| Langkah | Perintah |
|---|---|
| Buat bare repo | git init --bare |
| Buat hook | hooks/post-receive |
| Remote dari laptop | git remote add production ... |
| Deploy | git push production main |
Dokumentasi: git-scm.com/book/en/v2/Customizing-Git-Git-Hooks