# Local Deployment - Quick Reference

**For: On-premise VPS (local network only)**

---

## 🎯 Approach: Pull-Based Deployment

```
GitHub (CI) → Build & Release → VPS pulls & Deploys
```

No inbound connections needed!

---

## 📋 Quick Setup (10 minutes)

### 1. Create GitHub Token
- https://github.com/settings/tokens
- Scope: `repo`
- Copy token

### 2. On VPS - Download Deploy Script
```bash
curl -L https://raw.githubusercontent.com/YOUR_USERNAME/btm-koperasi/main/deploy/deploy.sh \
  -o /usr/local/bin/deploy.sh

chmod +x /usr/local/bin/deploy.sh
```

### 3. Configure Deploy Script
```bash
nano /usr/local/bin/deploy.sh
```

Update:
```bash
GITHUB_REPO="YOUR_USERNAME/btm-koperasi"
GITHUB_TOKEN="YOUR_TOKEN_HERE"
```

### 4. Deploy
```bash
./deploy.sh main
```

---

## 🚀 Deployment Commands

### Manual Deploy
```bash
# SSH to VPS
ssh root@YOUR_VPS

# Deploy latest
./deploy.sh main

# Deploy from develop
./deploy.sh develop

# Force deploy (skip backup)
./deploy.sh --force

# Rollback
./deploy.sh --rollback
```

### Auto Deploy (Cron)
```bash
# Edit crontab
crontab -e

# Add hourly check
0 * * * * /usr/local/bin/deploy.sh main >> /var/log/btm-deploy.log 2>&1
```

---

## 📊 Workflow

```
Push to GitHub
    ↓
GitHub Actions (CI)
    ✅ Tests run
    ✅ Build app
    📦 Create Release
    ↓
VPS (scheduled or manual)
    📥 Download release
    💾 Create backup
    🚀 Deploy
    ✅ Health check
```

---

## 🔧 Common Tasks

### Check Version
```bash
cat /www/wwwroot/btm-koperasi/.deployed_version
```

### View Logs
```bash
# Deployment logs
tail -f /var/log/btm-deploy.log

# Application logs
tail -f /www/wwwroot/btm-koperasi/storage/logs/laravel.log

# Queue logs
tail -f /www/wwwlogs/btm-koperasi-worker.log
```

### Manual Rollback
```bash
# List backups
ls -lt /www/wwwroot/btm-koperasi-backup

# Restore
cd /www/wwwroot/btm-koperasi-backup
LATEST=$(ls -t | head -n1)
cp -r $LATEST/app/* /www/wwwroot/btm-koperasi/

# Restart services
systemctl reload php8.3-fpm
```

### Health Check
```bash
curl http://localhost/health
```

---

## 🔐 Required Files

| File | Location | Purpose |
|------|----------|---------|
| `deploy.sh` | `/usr/local/bin/` | Main deploy script |
| `nginx.conf` | `deploy/` | Nginx config template |
| `ci-build.yml` | `.github/workflows/` | CI workflow |

---

## 📝 GitHub Setup

### Required Workflow: `.github/workflows/ci-build.yml`

Triggers:
- Push to `main` → Create release
- Push to `develop` → Build artifact

Jobs:
1. **Lint** - Code quality
2. **Test** - Pest tests
3. **Build** - Create release package

---

## 🛡️ Security

### Store Token Securely
```bash
# Instead of hardcoding in script
echo "YOUR_TOKEN" > /root/.github_token
chmod 600 /root/.github_token

# Update deploy.sh
GITHUB_TOKEN=$(cat /root/.github_token)
```

### Restrict Script Access
```bash
chown root:root /usr/local/bin/deploy.sh
chmod 700 /usr/local/bin/deploy.sh
```

---

## 🐛 Troubleshooting

| Issue | Solution |
|-------|----------|
| Token rejected | Verify token is valid and not expired |
| Release not found | Check repo name, ensure releases exist |
| Permission denied | Check file permissions, run as root |
| Health check failed | Check PHP-FPM, Nginx, database |
| Queue not working | `supervisorctl restart btm-koperasi-worker:*` |

---

## 📈 Monitoring

### Simple Uptime Check
```bash
# Add to crontab
*/5 * * * * curl -f http://localhost/health || \
  echo "⚠️ Health check failed at $(date)" >> /var/log/health.log
```

### Disk Space
```bash
df -h
```

### Memory
```bash
free -h
```

---

## ✅ Checklist

### Initial Setup
- [ ] GitHub token created
- [ ] Deploy script downloaded
- [ ] Script configured
- [ ] Directories created
- [ ] Permissions set
- [ ] Test deployment successful

### Before Deploy
- [ ] Tests passing on GitHub
- [ ] Release created
- [ ] Team notified (if major)

### After Deploy
- [ ] Health check passed
- [ ] App accessible
- [ ] Logs checked

---

## 📖 Full Documentation

- **Complete Guide**: [LOCAL_DEPLOYMENT_GUIDE.md](LOCAL_DEPLOYMENT_GUIDE.md)
- **All Options**: [LOCAL_DEPLOYMENT_OPTIONS.md](LOCAL_DEPLOYMENT_OPTIONS.md)
- **CI/CD Overview**: [README_CICD.md](README_CICD.md)

---

**Deployment Time**: ~5-10 minutes
**Internet Required**: Outbound only (VPS → GitHub)
**Inbound Connections**: None
