# GitHub Webhook Setup for cPanel Auto-Deployment

Once you push code to GitHub, cPanel automatically deploys it to your server.

---

## Setup Instructions

### Step 1: Get Your Webhook URL from cPanel

1. Log into **cPanel**
2. Search for **"Git Version Control"** or **"Git Repository"**
3. Click on your repository (noblecollectable)
4. Look for **"Webhook URL"** or **"Deploy Token"**
5. Copy the webhook URL (looks like: `https://yourdomain.com/cgi-sys/webhook/deployments/...`)

### Step 2: Add Webhook to GitHub Repository

1. Go to **GitHub** → Your repo (noblecollectable)
2. Settings → **Webhooks** (or Integrations)
3. Click **Add webhook**
4. Paste the cPanel webhook URL in **Payload URL**
5. Set **Content type** to `application/json`
6. Check boxes:
   - ☑ Just the `push` event
   - ☑ Active
7. Click **Add webhook**

### Step 3: Test the Webhook

1. Make a small change to a file (e.g., update README.md)
2. Commit and push:
   ```bash
   git add .
   git commit -m "Test webhook"
   git push origin master
   ```

3. Check cPanel for deployment status:
   - cPanel → Git Version Control
   - Look for deployment log
   - Should show: ✅ Deployment completed

4. Visit your site: `https://yourdomain.com/shop`
   - Verify changes are live

---

## How It Works

```
You push code to GitHub
         ↓
GitHub triggers webhook
         ↓
cPanel receives webhook
         ↓
cPanel pulls latest code
         ↓
cpanel.yml runs post-deployment actions:
  - Install WordPress (if missing)
  - Update plugins/themes
  - Set permissions
  - Clear cache
         ↓
Your site is updated!
```

---

## What Gets Deployed

✅ **Deployed:**
- Theme files (wp-content/themes/noblecollectable-child/)
- Plugin files (wp-content/plugins/noblecollectable-setup/)
- Documentation (*.md files)
- Configuration (cpanel.yml, docker-compose.yml)

❌ **NOT Deployed** (ignored by .gitignore):
- WordPress core files (wp-admin/, wp-includes/, wp-*.php)
- Uploads (wp-content/uploads/)
- Database
- wp-config.php
- .env files

---

## Post-Deployment Actions

After each push, cpanel.yml automatically:

1. **Installs WordPress** (if missing)
2. **Updates WordPress core** (via WP-CLI)
3. **Updates plugins** (via WP-CLI)
4. **Updates themes** (via WP-CLI)
5. **Sets proper permissions** (755 for directories)
6. **Optimizes database** (via WP-CLI)
7. **Clears cache** (via cPanel)
8. **Sends email notification** (success or failure)

---

## Troubleshooting

### Webhook Not Triggering

1. Check GitHub repo settings → Webhooks
2. Look for the webhook in Recent Deliveries
3. Click on it to see payload and response
4. If error, check:
   - Webhook URL is correct
   - cPanel account is active
   - Repository access is enabled

### Deployment Failed

1. cPanel → Git Version Control → View Log
2. Common issues:
   - File permissions incorrect (check chmod)
   - wp-config.php missing (create manually)
   - Database credentials wrong (update wp-config.php)
   - Plugin conflicts (check logs)

### Site Not Updating After Push

1. Verify webhook was triggered (GitHub Recent Deliveries)
2. Check cPanel deployment log
3. Hard refresh browser: `Ctrl+Shift+Del` (clear cache)
4. Check file permissions: `ls -la wp-content/`

---

## Manual Deployment (If Webhook Fails)

SSH to server and pull manually:

```bash
ssh yourusername@yourdomain.com
cd public_html/noblecollectable
git pull origin master
chmod -R 755 wp-content/
```

---

## Workflow Example

### Local Development

```bash
# Make changes locally
git add .
git commit -m "Update product styling"
git push origin master
```

### Automatic Deployment

GitHub webhook triggers → cPanel pulls → Site updates automatically ✅

### Verify Live

```bash
# Visit your site
https://yourdomain.com/shop
# Changes should be visible!
```

---

## Customizing cpanel.yml

Edit `cpanel.yml` to customize:

- Post-deployment scripts
- File permissions
- Notifications
- Branches to deploy

Then push changes:

```bash
git add cpanel.yml
git commit -m "Update deployment config"
git push origin master
```

---

## Security Notes

- ✅ Webhook URL is unique to your cPanel account
- ✅ Only deploys code changes (not database)
- ✅ Passwords/secrets should NOT be in .env (set in cPanel)
- ✅ .gitignore prevents wp-config.php, uploads, etc. from being overwritten
- ⚠️ Keep webhook URL private (don't share publicly)

---

## Support

If issues arise:

1. Check cPanel → Git Version Control logs
2. Check GitHub → Webhooks → Recent Deliveries
3. Check browser console for errors
4. Run manual `git pull` to verify repo is accessible

---

**Setup Complete!** 🚀

Your site now auto-deploys on every GitHub push. Try it:

```bash
git commit -m "Test auto-deploy"
git push origin master
# Visit site within 1-2 minutes
```
