# CD Quick Reference Card

## 🚀 Quick Start

### 1. Server Setup (One-time)
```bash
# Upload and run setup script
scp deploy/setup-aapanel.sh root@YOUR_SERVER_IP:/tmp/
ssh root@YOUR_SERVER_IP
bash /tmp/setup-aapanel.sh
```

### 2. Configure aaPanel
- **Website Root**: `/www/wwwroot/btm-koperasi/public`
- **PHP Version**: 8.3
- **SSL**: Let's Encrypt (auto-renew)
- **Nginx Rewrite**: Laravel preset

### 3. GitHub Secrets
| Secret | Value |
|--------|-------|
| `SSH_PRIVATE_KEY` | Your SSH private key |
| `SSH_HOST` | VPS IP address |
| `SSH_USERNAME` | `root` |
| `DB_PASSWORD` | Database password |
| `APP_URL` | `your-domain.com` |

### 4. Deploy
- **Auto**: Push to `main` branch
- **Manual**: Actions → Deploy to Production → Run workflow

---

## 📊 Deployment Workflow

```
Push to main → Tests → Build → Backup → Deploy → Health Check → Done
                         ↓
                    On Failure: Auto Rollback
```

**Duration**: 5-10 minutes

---

## 🔧 Common Commands

### Server Management
```bash
# View logs
tail -f /www/wwwlogs/btm-koperasi-error.log
tail -f storage/logs/laravel.log

# Restart services
systemctl restart php8.3-fpm
supervisorctl restart btm-koperasi-worker:*

# Check disk space
df -h

# Check memory
free -h
```

### Database
```bash
# Backup database
mysqldump -u root -p btm_koperasi > backup.sql

# Restore database
mysql -u root -p btm_koperasi < backup.sql

# Check database size
mysql -u root -p -e "SELECT table_schema, SUM(data_length + index_length) / 1024 / 1024 AS 'Size (MB)' FROM information_schema.tables GROUP BY table_schema;"
```

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

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

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

---

## 🐛 Troubleshooting

| Issue | Solution |
|-------|----------|
| SSH connection failed | Check SSH key in secrets, test with `ssh -i key user@host` |
| 502 Bad Gateway | `systemctl restart php8.3-fpm` |
| Migration failed | Check DB credentials, verify MySQL running |
| Permission denied | `chown -R www:www /www/wwwroot/btm-koperasi` |
| Queue not working | `supervisorctl status`, `supervisorctl restart btm-koperasi-worker:*` |

---

## 📈 Monitoring

### Health Check URLs
- **Homepage**: `https://your-domain.com`
- **API**: `https://your-domain.com/api/health` (if available)

### Logs Location
- **Nginx**: `/www/wwwlogs/btm-koperasi-*.log`
- **Laravel**: `storage/logs/laravel.log`
- **Queue**: `/www/wwwlogs/btm-koperasi-worker.log`
- **Supervisor**: `/var/log/supervisor/`

### GitHub Actions
- **Logs**: Actions tab → Workflow run → Job logs
- **Artifacts**: Download test reports, screenshots

---

## 🔐 Security Checklist

- [ ] SSH key restricted to deployment user
- [ ] Firewall allows only 22, 80, 443
- [ ] `.env` permissions: 644, owned by www
- [ ] `APP_DEBUG=false` in production
- [ ] SSL auto-renewal enabled
- [ ] Regular system updates scheduled
- [ ] Database backups verified
- [ ] Secrets rotated periodically

---

## 📞 Emergency Contacts

| Role | Contact |
|------|---------|
| DevOps | [Your contact] |
| Developer | [Your contact] |
| Database Admin | [Your contact] |

**Rollback First, Investigate Later** - If production is broken, rollback immediately, then investigate the issue.

---

## 🎯 Deployment Schedule

| Environment | Auto-Deploy | Manual Deploy |
|-------------|-------------|---------------|
| **Staging** | Every push to `develop` | Anytime |
| **Production** | Push to `main` | Weekdays 9 AM - 4 PM only |

**Best Practice**: Deploy early in the day to allow time for fixes if needed.

---

## 📝 Pre-Deployment Checklist

- [ ] All tests passing
- [ ] Code review completed
- [ ] Changelog reviewed
- [ ] Database backup verified
- [ ] Team notified (if major changes)
- [ ] Rollback plan ready
- [ ] Monitoring tools ready

---

## 🎛️ Workflow Options

### Manual Deployment Options

| Option | Default | When to Change |
|--------|---------|----------------|
| **Environment** | production | Use staging for testing |
| **Skip Tests** | false | Only for urgent hotfixes |
| **Run Migrations** | true | Set false if migrations handled separately |

---

**Full Documentation**: See `DEPLOYMENT_GUIDE.md` for detailed instructions.

**Setup Script**: `deploy/setup-aapanel.sh`

**Workflow File**: `.github/workflows/cd-deploy.yml`
