Prepare Alpine artifact deploy workflow

This commit is contained in:
2026-05-26 06:44:12 +07:00
parent f0ceebbbc8
commit 76b37cd4c8
11 changed files with 288 additions and 48 deletions

View File

@ -112,6 +112,7 @@ Dokumen ini menyimpan konteks kerja terbaru setelah rangkaian patch mobile, logi
- `native`
- `darwin-arm64`
- `debian-openssl-3.0.x`
- `linux-musl-openssl-3.0.x`
- File:
- [prisma/schema.prisma](/Users/wirabasalamah/Documents/Codex/abelbirdnest-web/prisma/schema.prisma)
@ -122,26 +123,71 @@ Dokumen ini menyimpan konteks kerja terbaru setelah rangkaian patch mobile, logi
- Script:
- [deploy/scripts/build-linux-release.sh](/Users/wirabasalamah/Documents/Codex/abelbirdnest-web/deploy/scripts/build-linux-release.sh)
- Karakteristik workflow ini:
- build di Docker `linux/amd64`
- build di Docker Alpine Linux agar cocok dengan server Alpine
- install `openssl` di container
- pakai `NODE_OPTIONS=--max-old-space-size=4096`
- copy source ke `/work` agar tidak menimpa `node_modules` lokal Mac
- artifact final:
`abelbirdnest-release.tar.gz`
- artifact final memakai timestamp:
`abelbirdnest-release-YYYYMMDD-HHMMSS.tar.gz`
- Artifact final tidak membawa `.env.production` lokal.
### Catatan Tambahan 2026-05-26
- Server production memakai Alpine Linux dengan RAM sekitar 1 GB.
- Jangan compile/build di server.
- Artifact harus dibuat dari container Alpine, default script sekarang memakai `node:20-alpine`.
- Runtime server Alpine perlu `nodejs` dan `openssl`.
- Ukuran artifact runtime-only yang normal sekitar `43 MB`.
- Jika artifact membengkak sekitar `120 MB+`, kemungkinan Prisma CLI atau `node_modules` lengkap ikut terbawa. Jangan pakai varian besar itu untuk server 1 GB kecuali memang sengaja.
- Artifact runtime-only tidak dipakai untuk menjalankan `prisma migrate deploy` di server. Untuk update UI/app tanpa perubahan schema DB, cukup extract release baru lalu restart service.
- Jika nanti ada migration database, jalankan migration dari mesin build/admin yang punya dependency lengkap dan akses DB production, bukan dari server kecil.
## Catatan Deploy Server Saat Ini
- Path app di server:
`/var/www/abelbirdnest-web/AbelBirdNest-Stock`
- Path app aktif di server production saat ini:
`/opt/abelbirdnest-web`
- Struktur release yang dipakai sekarang diasumsikan:
- upload artifact ke root repo server
- upload artifact ke root app server:
`/opt/abelbirdnest-web`
- extract ke `releases/<timestamp>`
- symlink `current` diarahkan ke release aktif
- `systemd` yang benar untuk mode artifact:
- `WorkingDirectory=/var/www/abelbirdnest-web/AbelBirdNest-Stock/current`
- `EnvironmentFile=/var/www/abelbirdnest-web/AbelBirdNest-Stock/.env.production`
- `ExecStart=/usr/bin/node server.js`
- Folder aktif runtime:
`/opt/abelbirdnest-web/current`
- Service server memakai OpenRC, bukan systemd.
- Service OpenRC production yang ditemukan:
- `directory="/opt/abelbirdnest-web/current"`
- `command="/usr/bin/node"`
- `command_args="/opt/abelbirdnest-web/current/server.js"`
- env yang dicek service:
`/opt/abelbirdnest-web/current/.env`
- Karena service mencari `.env` di folder `current`, setiap release baru perlu copy `.env` dari release/current lama ke folder release baru sebelum `current` diarahkan ke release baru.
- Jangan gunakan path lama `/var/www/abelbirdnest-web/AbelBirdNest-Stock` untuk server ini kecuali service production memang sudah diubah.
### Command Manual Deploy Yang Dipakai
Di lokal:
```bash
scp abelbirdnest-release-YYYYMMDD-HHMMSS.tar.gz abelbirdnest@SERVER_IP:/opt/abelbirdnest-web/
```
Di server:
```bash
cd /opt/abelbirdnest-web
RELEASE_NAME=YYYYMMDD-HHMMSS
mkdir -p /opt/abelbirdnest-web/releases/$RELEASE_NAME
tar -xzf /opt/abelbirdnest-web/abelbirdnest-release-$RELEASE_NAME.tar.gz -C /opt/abelbirdnest-web/releases/$RELEASE_NAME
cp /opt/abelbirdnest-web/current/.env /opt/abelbirdnest-web/releases/$RELEASE_NAME/.env
mv /opt/abelbirdnest-web/current /opt/abelbirdnest-web/current-backup-$(date +%Y%m%d-%H%M%S) 2>/dev/null || true
ln -sfn /opt/abelbirdnest-web/releases/$RELEASE_NAME /opt/abelbirdnest-web/current
sudo chown -R abelbirdnest:abelbirdnest /opt/abelbirdnest-web
sudo rc-service abelbirdnest-web restart
sudo rc-service abelbirdnest-web status
curl -I http://127.0.0.1:3007/login
```
Jika `ln -sfn ... current` gagal dengan pesan `File exists`, berarti `current` masih folder biasa. Backup/rename folder itu dulu, lalu ulangi `ln -sfn`.
## Verifikasi yang Sudah Dilakukan

View File

@ -0,0 +1,121 @@
# Update Deploy Alpine
Panduan ini untuk server production Alpine Linux x86_64 RAM 1 GB. Server tidak menjalankan build/compile.
## 1. Build Artifact Di Mesin Lokal/Build Machine
Mesin build harus punya Docker.
```bash
./deploy/scripts/build-linux-release.sh
```
Script memakai `node:20-alpine`, menjalankan `npm ci`, `prisma generate`, `next build`, lalu membuat artifact bertanggal:
```bash
abelbirdnest-release-YYYYMMDD-HHMMSS.tar.gz
```
## 2. Upload Artifact Ke Server
```bash
./deploy/scripts/upload-linux-release.sh abelbirdnest@SERVER_IP /opt/abelbirdnest-web
```
Script otomatis memilih artifact terbaru dengan pola `abelbirdnest-release-*.tar.gz`, upload artifact, extract ke:
```bash
/opt/abelbirdnest-web/releases/<timestamp>
```
Lalu update symlink:
```bash
/opt/abelbirdnest-web/current
```
## 3. Prasyarat Server Alpine
Jalankan sekali di server:
```bash
sudo apk add --no-cache nodejs openssl
```
Pastikan env production ada di:
```bash
/var/www/abelbirdnest-web/AbelBirdNest-Stock/.env.production
```
## 4. Setup Service OpenRC
Copy template service dari mesin lokal/build machine:
```bash
scp deploy/openrc/abelbirdnest-web abelbirdnest@SERVER_IP:/tmp/abelbirdnest-web
ssh abelbirdnest@SERVER_IP
sudo mv /tmp/abelbirdnest-web /etc/init.d/abelbirdnest-web
sudo chmod +x /etc/init.d/abelbirdnest-web
sudo rc-update add abelbirdnest-web default
```
Service menjalankan:
```bash
cd /var/www/abelbirdnest-web/AbelBirdNest-Stock/current
PORT=3007 NODE_ENV=production NODE_OPTIONS=--max-old-space-size=512 node server.js
```
## 5. Setelah Upload
Masuk server:
```bash
ssh abelbirdnest@SERVER_IP
cd /var/www/abelbirdnest-web/AbelBirdNest-Stock/current
```
Artifact release dibuat ramping untuk runtime Next.js standalone. Jangan menjalankan `npm install`, `npm run build`, atau `prisma migrate deploy` di server kecil.
Jika update berikutnya membawa migration database, jalankan migration dari mesin build/admin yang punya dependency lengkap dan akses ke database production. Untuk update UI/app tanpa migration, langsung restart service.
Load env hanya jika perlu tes manual:
```bash
set -a
. /var/www/abelbirdnest-web/AbelBirdNest-Stock/.env.production
set +a
```
Restart service:
```bash
sudo rc-service abelbirdnest-web restart
sudo rc-service abelbirdnest-web status
```
## 6. Verifikasi
```bash
curl -I http://127.0.0.1:3007/login
tail -n 100 /var/log/abelbirdnest-web.err
tail -n 100 /var/log/abelbirdnest-web.log
```
Jika memakai reverse proxy, cek domain public setelah service sehat.
## 7. Rollback Cepat
Lihat release yang tersedia:
```bash
ls -lt /var/www/abelbirdnest-web/AbelBirdNest-Stock/releases
```
Arahkan `current` ke release sebelumnya:
```bash
sudo ln -sfn /var/www/abelbirdnest-web/AbelBirdNest-Stock/releases/NAMA_RELEASE_LAMA /var/www/abelbirdnest-web/AbelBirdNest-Stock/current
sudo rc-service abelbirdnest-web restart
```

View File

@ -354,7 +354,7 @@ sudo systemctl restart abelbirdnest-web
## 13. Alternatif Deploy: Build di Lokal, Jalankan di Server
Repo ini sekarang mendukung output Next.js `standalone`, sehingga build bisa dilakukan di lingkungan Linux lokal/container lalu artifact di-upload ke server.
Repo ini sekarang mendukung output Next.js `standalone`, sehingga build dilakukan di lingkungan Linux lokal/container lalu artifact di-upload ke server. Server production tidak menjalankan `npm run build`.
### Kapan jalur ini dipakai
@ -366,8 +366,10 @@ Pakai cara ini jika:
### Prasyarat
- build harus dilakukan di Linux, atau container Linux
- build harus dilakukan di Linux Alpine, atau container Linux Alpine
- versi Node saat build harus sama dengan server
- server Alpine perlu `nodejs` dan `openssl` terpasang
- untuk server RAM 1 GB, runtime disarankan memakai `NODE_OPTIONS=--max-old-space-size=512`
- database migration tetap dijalankan di server
### Script yang disediakan
@ -386,16 +388,16 @@ deploy/scripts/upload-linux-release.sh
### Contoh build artifact di lokal
Pastikan `.env.production` sudah ada di root repo jika ingin ikut dibawa ke artifact.
Pastikan Docker tersedia di mesin lokal/build machine. Script default memakai image `node:20-alpine` agar artifact cocok dengan server Alpine Linux. `.env.production` production tetap disimpan di server, bukan dibawa di artifact.
```bash
./deploy/scripts/build-linux-release.sh
```
Artifact default yang dihasilkan:
Artifact default yang dihasilkan memakai timestamp:
```bash
abelbirdnest-release.tar.gz
abelbirdnest-release-YYYYMMDD-HHMMSS.tar.gz
```
Isi artifact:
@ -404,19 +406,19 @@ Isi artifact:
- static assets `.next/static`
- folder `public`
- folder `prisma`
- `.env.production` jika tersedia
- tidak membawa `.env.production`
### Contoh upload ke server
```bash
./deploy/scripts/upload-linux-release.sh abelbirdnest@server /var/www/abelbirdnest-web
./deploy/scripts/upload-linux-release.sh abelbirdnest@server /var/www/abelbirdnest-web/AbelBirdNest-Stock
```
Setelah upload, script akan:
- copy artifact ke server
- extract ke `/var/www/abelbirdnest-web/releases/<timestamp>`
- update symlink `/var/www/abelbirdnest-web/current`
- extract ke `/var/www/abelbirdnest-web/AbelBirdNest-Stock/releases/<timestamp>`
- update symlink `/var/www/abelbirdnest-web/AbelBirdNest-Stock/current`
### Langkah setelah upload di server
@ -424,13 +426,11 @@ Masuk ke server:
```bash
ssh abelbirdnest@server
cd /var/www/abelbirdnest-web/current
set -a
source .env.production
set +a
npx prisma migrate deploy
cd /var/www/abelbirdnest-web/AbelBirdNest-Stock/current
```
Artifact standalone dibuat untuk runtime. Jangan menjalankan `npm install`, `npm run build`, atau `prisma migrate deploy` di server kecil. Jika rilis membawa migration database, jalankan migration dari mesin build/admin yang punya dependency lengkap dan akses ke database production.
Untuk menjalankan standalone app secara manual:
```bash
@ -440,7 +440,7 @@ PORT=3007 node server.js
Jika memakai `systemd`, `WorkingDirectory` service harus diarahkan ke:
```bash
/var/www/abelbirdnest-web/current
/var/www/abelbirdnest-web/AbelBirdNest-Stock/current
```
dan `ExecStart` menjadi: