# 🎯 CI/CD Quick Start Guide

**Get deployed in 15 minutes!**

---

## 🚀 Step-by-Step Setup

### Step 1: Prepare Your VPS (5 minutes)

```bash
# 1. SSH into your server
ssh root@YOUR_SERVER_IP

# 2. Install aaPanel (if not installed)
# For Ubuntu/Debian:
wget -O install.sh https://www.aapanel.com/script/install_6.0_en.sh && bash install.sh aapanel

# For CentOS:
wget -O install.sh https://www.aapanel.com/script/install_6.0_en.sh && bash install.sh aapanel
```

**Wait for aaPanel installation to complete.** Note your login credentials.

---

### Step 2: Install Required Plugins (3 minutes)

1. **Login to aaPanel**: `http://YOUR_IP:8888`

2. **Install Recommended Stack**:
   - Go to **App Store**
   - Click **Recommended**
   - Install:
     - ✅ Nginx
     - ✅ PHP 8.3
     - ✅ MySQL 5.7+
     - ✅ phpMyAdmin

3. **Install Additional Plugins**:
   - Go to **App Store** → **Tool**
   - Install:
     - ✅ Supervisor

---

### Step 3: Run Setup Script (5 minutes)

```bash
# On your LOCAL computer, upload the setup script
scp deploy/setup-aapanel.sh root@YOUR_SERVER_IP:/tmp/

# SSH into server
ssh root@YOUR_SERVER_IP

# Run the setup script
chmod +x /tmp/setup-aapanel.sh
bash /tmp/setup-aapanel.sh
```

**Follow the prompts:**
- Enter database name: `btm_koperasi`
- Enter database username: `btm_user`
- Enter database password: `(create a secure password)`

**Save these credentials!**

---

### Step 4: Configure Website in aaPanel (2 minutes)

1. **Go to Website** → **Add site**

2. **Fill in:**
   - **Domain**: `your-domain.com` (or server IP for testing)
   - **Root Directory**: `/www/wwwroot/btm-koperasi/public`
   - **PHP Version**: PHP 8.3
   - **Database**: MySQL (use credentials from Step 3)

3. **Click Submit**

4. **Setup SSL**:
   - Click your website → **SSL**
   - Select **Let's Encrypt**
   - Enter email
   - Click **Apply**

---

### Step 5: Configure GitHub Secrets (3 minutes)

1. **Generate SSH Key** (on your local computer):

```bash
ssh-keygen -t ed25519 -C "github-deploy" -f ~/.ssh/github_deploy_btm
```

2. **Copy Public Key to Server**:

```bash
ssh-copy-id -i ~/.ssh/github_deploy_btm.pub root@YOUR_SERVER_IP
```

3. **Get Private Key**:

```bash
# macOS
cat ~/.ssh/github_deploy_btm | pbcopy

# Windows (Git Bash)
cat ~/.ssh/github_deploy_btm | clip

# Linux
cat ~/.ssh/github_deploy_btm
```

4. **Add to GitHub**:
   - Go to your repository → **Settings**
   - **Secrets and variables** → **Actions**
   - **New repository secret**

   Add these secrets:

   | Name | Value |
   |------|-------|
   | `SSH_PRIVATE_KEY` | (paste entire private key) |
   | `SSH_HOST` | `YOUR_SERVER_IP` |
   | `SSH_USERNAME` | `root` |
   | `DB_PASSWORD` | (password from Step 3) |
   | `APP_URL` | `your-domain.com` |

---

### Step 6: Test Deployment (2 minutes)

1. **Go to Actions** tab in GitHub

2. **Select "Deploy to Production"** workflow

3. **Click "Run workflow"**

4. **Choose options**:
   - Branch: `main`
   - Environment: `production`
   - Skip tests: `false` (for first deploy, test!)
   - Run migrations: `true`

5. **Click "Run workflow"**

6. **Watch the magic happen!** ✨

   - Tests run (~5 min)
   - Build creates (~2 min)
   - Deployment happens (~3 min)
   - Health check passes

---

## ✅ Verify Deployment

### 1. Check Website

```
https://your-domain.com
```

You should see your application!

### 2. Check Logs

```bash
# SSH to server
ssh root@YOUR_SERVER_IP

# View logs
tail -f /www/wwwlogs/btm-koperasi-access.log
```

### 3. Check Application Status

```bash
# Check PHP-FPM
systemctl status php8.3-fpm

# Check queue workers
supervisorctl status

# Check Laravel
cd /www/wwwroot/btm-koperasi
php artisan about
```

---

## 🎉 Done! You're Live!

### What Happens Now

- ✅ Every push to `main` auto-deploys
- ✅ Tests run before deployment
- ✅ Backups created automatically
- ✅ Auto-rollback on failure
- ✅ Zero-downtime deployment

---

## 📚 Next Steps

### Learn More

- **Full Guide**: [DEPLOYMENT_GUIDE.md](DEPLOYMENT_GUIDE.md)
- **Quick Reference**: [CD_QUICK_REFERENCE.md](CD_QUICK_REFERENCE.md)
- **CI Details**: [README_CICD.md](README_CICD.md)

### Optional Enhancements

1. **Setup Monitoring**:
   - Install Sentry for error tracking
   - Setup uptime monitoring

2. **Configure Slack Notifications**:
   - Add `SLACK_WEBHOOK_URL` secret
   - Get deployment notifications in Slack

3. **Setup Staging Environment**:
   - Duplicate setup on subdomain
   - Test before production

4. **Optimize Performance**:
   - Enable Redis cache
   - Setup CDN
   - Configure OPcache

---

## 🆘 Troubleshooting

### Deployment Failed?

1. **Check GitHub Actions logs**
   - Go to Actions tab
   - Click failed run
   - Find the error

2. **Common fixes**:

   **SSH Error:**
   ```bash
   # Test SSH connection
   ssh -i ~/.ssh/github_deploy_btm root@YOUR_SERVER_IP
   ```

   **Database Error:**
   ```bash
   # Check MySQL running
   systemctl status mysqld
   
   # Verify credentials in .env
   cat /www/wwwroot/btm-koperasi/.env
   ```

   **502 Error:**
   ```bash
   # Restart PHP-FPM
   systemctl restart php8.3-fpm
   ```

3. **Rollback if needed**:
   - Deployment auto-rollbacks on failure
   - Or manually restore from backup

---

## 📊 Deployment Flow

```
Push to main
    ↓
┌─────────────────┐
│ Run Tests       │ ✅ Must pass
└─────────────────┘
    ↓
┌─────────────────┐
│ Build App       │ 📦 Create artifact
└─────────────────┘
    ↓
┌─────────────────┐
│ Create Backup   │ 💾 Save current + DB
└─────────────────┘
    ↓
┌─────────────────┐
│ Deploy New      │ 🚀 Upload & activate
└─────────────────┘
    ↓
┌─────────────────┐
│ Health Check    │ ✅ Verify working
└─────────────────┘
    ↓
┌─────────────────┐
│ Done!           │ 🎉 Live!
└─────────────────┘
```

---

## 🎯 Quick Commands

### Local Testing
```bash
# Before push
composer test
npm run test -- --run
npm run build
```

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

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

# Check disk space
df -h

# Check memory
free -h
```

---

## 📞 Need Help?

1. **Check documentation** first
2. **Review logs** (GitHub + server)
3. **Test locally** to reproduce
4. **Contact team** if stuck

---

**Happy Deploying!** 🚀

**Total Setup Time**: ~20 minutes
**Deployment Time**: ~10 minutes
**Peace of Mind**: Priceless ✨
