# 🚀 Auto-Deploy Implementation Plan

**Status:** Phase 1 Complete - Ready for Full Implementation  
**Date:** 2026-03-08

---

## 📋 REQUIREMENTS SUMMARY

### From User
1. ✅ **Gitea Integration** - Auto-create/update repos on git.moreminimore.com
2. ✅ **Easypanel Auth** - Username/password (auto-generate token)
3. ✅ **Unified .env** - Single file for all skills
4. ✅ **Install Script** - Auto-sync all skills to OpenCode global
5. ✅ **Auto-Detection** - New vs existing projects

---

## 🏗️ ARCHITECTURE

### Skills Structure

```
opencode-skill/
├── .env.example              # Unified template (ALL skills)
├── scripts/
│   └── install-skills.sh     # Updated for unified .env
└── skills/
    ├── gitea-sync/           # NEW - Gitea automation
    │   ├── SKILL.md
    │   └── scripts/
    │       ├── sync.py       # Main script
    │       └── .env.example  # (uses unified .env)
    │
    ├── easypanel-deploy/     # UPDATED - Python script added
    │   ├── SKILL.md
    │   └── scripts/
    │       ├── deploy.py     # NEW - Auto-deploy with username/pass
    │       └── .env.example  # (uses unified .env)
    │
    └── website-creator/      # UPDATED - Auto-deploy integration
        ├── SKILL.md
        └── scripts/
            ├── create_astro_website.py  # Updated
            └── .env.example             # (uses unified .env)
```

### Unified .env File

**Location during development:** `/Users/kunthawatgreethong/Gitea/opencode-skill/.env`

**Location after install:** `~/.config/opencode/.env`

**Contents:**
```bash
# ===========================================
# UNIFIED OPENCODE SKILLS CONFIGURATION
# ===========================================

# Gitea Configuration
GITEA_URL=https://git.moreminimore.com
GITEA_API_TOKEN=your-gitea-api-token
GITEA_USERNAME=your-username

# Easypanel Configuration
EASYPANEL_URL=http://110.164.146.47:3000
EASYPANEL_USERNAME=your-username
EASYPANEL_PASSWORD=your-password
EASYPANEL_DEFAULT_PROJECT=default

# Umami Analytics (optional)
UMAMI_DOMAIN=analytics.example.com

# Admin (for all websites)
ADMIN_PASSWORD=your-secure-password
```

---

## 🎯 IMPLEMENTATION PHASES

### Phase 1: easypanel-deploy ✅ COMPLETE

**Created:**
- `scripts/deploy.py` - Full Python implementation
- `scripts/.env.example` - Credentials template
- `scripts/requirements.txt` - Dependencies

**Features:**
- Username/password authentication
- Auto-generates API token
- Follows exact workflow from SKILL.md
- Creates project → service → connects Git → deploys
- Checks deployment status

**Usage:**
```bash
cd skills/easypanel-deploy
python3 scripts/deploy.py \
  --project my-website \
  --service my-website-service \
  --git-url https://git.moreminimore.com/user/my-website.git
```

---

### Phase 2: gitea-sync ⏳ NEXT

**To Create:**
- New skill: `gitea-sync`
- Python script for Gitea API
- Auto-detect new/existing repos
- Push code automatically

**Features:**
```python
# sync.py - Planned functionality

def check_repo_exists(username, repo_name):
    """Check if repository exists on Gitea."""
    response = requests.get(
        f"{GITEA_URL}/api/v1/repos/{username}/{repo_name}",
        headers={"Authorization": f"token {GITEA_API_TOKEN}"}
    )
    return response.status_code == 200

def create_repo(repo_name, description=""):
    """Create new repository."""
    if check_repo_exists(GITEA_USERNAME, repo_name):
        print(f"✅ Repository exists: {repo_name}")
        return update_repo(repo_name)
    else:
        print(f"📦 Creating repository: {repo_name}")
        response = requests.post(
            f"{GITEA_URL}/api/v1/user/repos",
            headers={"Authorization": f"token {GITEA_API_TOKEN}"},
            json={
                "name": repo_name,
                "description": description,
                "private": False,
                "auto_init": True
            }
        )
        return response.json()

def push_code(repo_path, git_url):
    """Push code to Gitea repository."""
    subprocess.run(["git", "init"], cwd=repo_path)
    subprocess.run(["git", "add", "."], cwd=repo_path)
    subprocess.run(["git", "commit", "-m", "Initial commit"], cwd=repo_path)
    subprocess.run(["git", "remote", "add", "origin", git_url], cwd=repo_path)
    subprocess.run(["git", "push", "-u", "origin", "main"], cwd=repo_path)
```

**Usage:**
```bash
python3 scripts/sync.py \
  --repo my-website \
  --path ./my-website \
  --description "My PDPA-compliant website"
```

---

### Phase 3: website-creator Integration ⏳ PENDING

**Update:** `create_astro_website.py`

**Add auto-deploy workflow:**
```python
def auto_deploy(website_path, website_name, args):
    """Complete auto-deploy workflow."""
    
    # Step 1: Sync to Gitea
    print("📦 Syncing to Gitea...")
    git_url = f"https://git.moreminimore.com/{GITEA_USERNAME}/{website_name}.git"
    
    subprocess.run([
        "python3",
        f"{SKILLS_DIR}/gitea-sync/scripts/sync.py",
        "--repo", website_name,
        "--path", str(website_path),
        "--description", f"Auto-generated website: {website_name}"
    ])
    
    # Step 2: Deploy to Easypanel
    print("🚀 Deploying to Easypanel...")
    subprocess.run([
        "python3",
        f"{SKILLS_DIR}/easypanel-deploy/scripts/deploy.py",
        "--project", website_name,
        "--service", f"{website_name}-service",
        "--git-url", git_url,
        "--branch", "main",
        "--port", "80"
    ])
    
    # Step 3: Return deployment URL
    print("✅ Deployment complete!")
    print(f"🌐 URL: https://{website_name}.easypanel.app")
```

**Integration point:** At end of `main()` function, after website generation.

---

### Phase 4: install-skills.sh Update ⏳ PENDING

**Current behavior:**
- Prompts for each skill's .env separately
- Creates .env files in each skill directory

**New behavior:**
- Single unified .env at repo root
- Copies to `~/.config/opencode/.env`
- All skills read from same file

**Updated workflow:**
```bash
#!/bin/bash

# 1. Check for unified .env.example
if [ -f "${REPO_ROOT}/.env.example" ]; then
    # Prompt for unified .env
    create_unified_env
fi

# 2. Install skills
for skill in $SKILLS; do
    cp -r "${SKILLS_DIR}/${skill}" "$TARGET"
    
    # Create skill-specific .env that sources unified .env
    cat > "${TARGET}/${skill}/scripts/.env" << EOF
# Auto-generated - sources unified .env
# Edit ${HOME}/.config/opencode/.env instead
EOF
done

# 3. Copy unified .env to global location
cp "${REPO_ROOT}/.env" "${HOME}/.config/opencode/.env"
chmod 600 "${HOME}/.config/opencode/.env"
```

---

### Phase 5: Unified .env.example ⏳ PENDING

**Create:** `/Users/kunthawatgreethong/Gitea/opencode-skill/.env.example`

```bash
# ===========================================
# OPENCODE SKILLS - UNIFIED CONFIGURATION
# ===========================================
# Copy this file to .env and fill in your values
# This file is shared by ALL skills
# ===========================================

# Gitea Configuration
# Get API token from: https://git.moreminimore.com/user/settings/applications
GITEA_URL=https://git.moreminimore.com
GITEA_API_TOKEN=
GITEA_USERNAME=

# Easypanel Configuration
# Login credentials for auto-deployment
EASYPANEL_URL=http://110.164.146.47:3000
EASYPANEL_USERNAME=
EASYPANEL_PASSWORD=
EASYPANEL_DEFAULT_PROJECT=default

# Website Defaults
# Applied to all generated websites
ADMIN_PASSWORD=
UMAMI_DOMAIN=analytics.example.com

# Optional: Umami Analytics
# Leave empty if not using
UMAMI_WEBSITE_ID=
```

---

## 📊 DEPLOYMENT WORKFLOW

### Complete Auto-Deploy Flow

```
User runs:
python3 create_astro_website.py --name "mysite" --output "./mysite"
         ↓
1. Generate website structure
   - Astro project
   - PDPA pages
   - Docker files
         ↓
2. Auto-sync to Gitea (NEW)
   - Check if repo exists
   - Create if new
   - Update if exists
   - Push code
         ↓
3. Auto-deploy to Easypanel (NEW)
   - Authenticate (username/pass → token)
   - Create project
   - Create service
   - Connect Git
   - Set build type (Dockerfile)
   - Trigger deployment
         ↓
4. Return deployment URL
   ✅ https://mysite.easypanel.app
```

---

## 🔧 ENVIRONMENT VARIABLE FLOW

```
Development:
┌─────────────────────────────────────┐
│ /Users/kunthawatgreethong/Gitea/    │
│   opencode-skill/.env               │ ← User edits this
│                                     │
│ [GITEA_API_TOKEN=xxx]               │
│ [EASYPANEL_USERNAME=xxx]            │
│ [ADMIN_PASSWORD=xxx]                │
└──────────────┬──────────────────────┘
               │
               │ install-skills.sh reads
               ↓
┌─────────────────────────────────────┐
│ ~/.config/opencode/.env             │ ← Skills read this
│ (copied from repo root)             │
└──────────────┬──────────────────────┘
               │
               │ Python scripts load via:
               │ load_env() from parent
               ↓
┌─────────────────────────────────────┐
│ skills/
│  ├── gitea-sync/scripts/sync.py    │
│  ├── easypanel-deploy/scripts/     │
│  └── website-creator/scripts/      │
└─────────────────────────────────────┘
```

---

## 🎯 IMPLEMENTATION PRIORITY

### Must Have (MVP)
1. ✅ easypanel-deploy script - **DONE**
2. ⏳ gitea-sync script
3. ⏳ Unified .env.example
4. ⏳ Updated install-skills.sh

### Should Have
5. ⏳ website-creator integration
6. ⏳ Auto-deploy on generation

### Nice to Have
7. ⏳ Status checking
8. ⏳ Rollback capability
9. ⏳ Multi-project support

---

## ⚠️ KNOWN ISSUES TO FIX

### easypanel-deploy
- LSP errors (minor, script works)
- Need to test authentication flow
- Error handling needs improvement

### gitea-sync
- Not yet created
- Need Gitea API token from user

### install-skills.sh
- Doesn't handle unified .env yet
- Doesn't update existing installations

---

## 🧪 TESTING PLAN

### Test 1: easypanel-deploy
```bash
cd skills/easypanel-deploy
python3 scripts/deploy.py --help
# Should show all options
```

### Test 2: gitea-sync
```bash
cd skills/gitea-sync
python3 scripts/sync.py --help
# Should show all options
```

### Test 3: Unified .env
```bash
cd /Users/kunthawatgreethong/Gitea/opencode-skill
./scripts/install-skills.sh
# Should prompt for unified .env
# Should copy to ~/.config/opencode/.env
```

### Test 4: End-to-End
```bash
python3 scripts/create_astro_website.py \
  --name "test-site" \
  --auto-deploy  # NEW flag
# Should: generate → gitea → easypanel → URL
```

---

## 📝 NEXT STEPS

### Immediate (User Action Required)
1. **Provide Gitea API Token**
   - Go to: https://git.moreminimore.com/user/settings/applications
   - Generate new token
   - Add to .env file

2. **Verify Easypanel Credentials**
   - Test username/password
   - Confirm API access works

3. **Review This Plan**
   - Confirm architecture is correct
   - Approve before I continue implementation

### Next Implementation Session
1. Create gitea-sync skill
2. Create unified .env.example
3. Update install-skills.sh
4. Integrate with website-creator
5. Test complete workflow

---

## ❓ QUESTIONS FOR USER

1. **Gitea Organization**: Should repos be created under your personal account or an organization?
   - **Your answer:** Personal account ✅

2. **Easypanel Auth**: Confirm username/password works (not just API token)
   - **Your answer:** Username/password preferred ✅

3. **Unified .env Location**: Confirm locations
   - Dev: `/Users/kunthawatgreethong/Gitea/opencode-skill/.env` ✅
   - Production: `~/.config/opencode/.env` ✅

4. **Auto-Deploy Default**: Should auto-deploy be:
   - A) Always on (every website auto-deploys)
   - B) Optional (--auto-deploy flag)
   - C) Ask interactively

---

**Status:** Ready to proceed with Phase 2 (gitea-sync) pending your review of this plan.
