# Auto-update cPanel from GitHub (test.duiclick.com)

Three ways to auto-deploy when code is pushed to GitHub. Pick **one**.

---

## Before you start

1. Domain **test.duiclick.com** document root → `.../test.duiclick.com/public`
2. `.env` exists on server (never commit `.env` to GitHub)
3. Private repo: you need a **GitHub Personal Access Token** (read access)

### Important: one app folder only

On some cPanel accounts there are **two copies** of the app:

| Path | Role |
|------|------|
| `/home/duiclick/test.duiclick.com` | Git clone (where `git pull` runs) |
| `/home/duiclick/public_html/test.duiclick.com` | Live site (what the browser uses) |

If these are different, `git pull` updates the wrong folder and new features (e.g. Opening Balance) never appear.

**Find which folder is live:**

```bash
/bin/bash ~/test.duiclick.com/scripts/deploy/cpanel-find-live-app.sh
```

**Fix A (best):** cPanel → **Domains** → **test.duiclick.com** → set Document Root to  
`/home/duiclick/test.duiclick.com/public`

**Fix B:** Sync git clone to public_html after each pull:

```bash
/bin/bash ~/test.duiclick.com/scripts/deploy/cpanel-sync-to-public-html.sh
```

---

## Method A — GitHub Actions → SSH (recommended)

Every push to `main` deploys automatically.

### 1. Enable SSH on cPanel

cPanel → **SSH Access** → Generate or import SSH key  
Note: hostname, username, port (usually 22)

### 2. Clone repo on server (one time)

SSH or Terminal:

```bash
cd ~
git clone https://github.com/mhr01828/Laravel-11-Project.git test.duiclick.com
cd test.duiclick.com
cp .env.example .env
nano .env   # DB + APP_URL=https://test.duiclick.com
php artisan key:generate
php artisan migrate --seed --force
php artisan storage:link
chmod +x scripts/deploy/*.sh
```

For **private** repo when cloning:

```bash
git clone https://YOUR_GITHUB_TOKEN@github.com/mhr01828/Laravel-11-Project.git test.duiclick.com
```

### 3. Add GitHub Secrets

GitHub repo → **Settings → Secrets and variables → Actions → New repository secret**

| Secret | Example |
|--------|---------|
| `CPANEL_SSH_HOST` | `test.duiclick.com` or server IP |
| `CPANEL_SSH_USER` | your cPanel username |
| `CPANEL_SSH_KEY` | private SSH key (full content) |
| `CPANEL_SSH_PORT` | `22` (optional) |
| `CPANEL_APP_DIR` | `/home/duiclick/test.duiclick.com` (optional) |

### 4. Done

Push to `main` → Actions tab runs **Deploy to cPanel** → site updates in ~1 minute.

---

## Method B — cPanel Git + Cron (no GitHub Actions)

Pulls GitHub every 5 minutes.

### 1. Connect GitHub in cPanel

cPanel → **Git Version Control** → **Create**

| Field | Value |
|-------|--------|
| Clone URL | `https://github.com/mhr01828/Laravel-11-Project.git` |
| Repository Path | `test.duiclick.com` |
| Branch | `main` |

For private repo, use:

`https://YOUR_TOKEN@github.com/mhr01828/Laravel-11-Project.git`

Click **Create** → **Update from Remote**

### 2. Edit `.cpanel.yml`

In File Manager, open `/home/USER/test.duiclick.com/.cpanel.yml`  
Replace `REPLACE_CPANEL_USER` with your real cPanel username.

Or use **Deploy HEAD Commit** after each manual pull.

### 3. Add cron job

cPanel → **Cron Jobs** → every 5 minutes:

```bash
/bin/bash /home/duiclick/test.duiclick.com/scripts/deploy/cpanel-auto-pull.sh >> /home/duiclick/logs/saas-deploy.log 2>&1
```

Create log folder: `mkdir -p /home/duiclick/logs`

### 4. Done

Within 5 minutes of a GitHub push, server auto-pulls and runs migrations.

---

## Method C — cPanel Git only (manual one-click)

cPanel → **Git Version Control** → your repo → **Manage**

1. **Update from Remote** (pull from GitHub)
2. **Deploy HEAD Commit** (runs `.cpanel.yml`)

No fully automatic — two clicks per update.

---

## First-time `.env` checklist

```env
APP_ENV=production
APP_DEBUG=false
APP_URL=https://test.duiclick.com

DB_HOST=localhost
DB_DATABASE=...
DB_USERNAME=...
DB_PASSWORD=...
```

---

## Verify deploy

```bash
tail -20 ~/logs/saas-deploy.log
# or
tail -20 ~/test.duiclick.com/storage/logs/laravel.log
```

---

## Security

- Never put GitHub tokens or SSH keys in the repository
- Use GitHub **Secrets** for Actions
- Keep `.env` only on the server
