# Deployment Guide

Multiple deployment methods for rmtPocketWatcher backend.

## Method 1: Portainer with Git Repository

**Best for:** Teams using Portainer for container management

### Setup in Portainer UI:

1. Go to Stacks → Add Stack → Git Repository
2. Configure:
   - **Name**: rmtpocketwatcher
   - **Repository URL**: Your Git repo URL
   - **Reference**: refs/heads/main
   - **Compose path**: deploy/portainer-stack.yml
3. Add environment variables:
   - `POSTGRES_PASSWORD`: your_secure_password
   - `IMAGE_TAG`: latest
4. Deploy

### Update via Webhook:

1. In Portainer, enable webhook for the stack
2. Add webhook URL to Gitea secrets: `PORTAINER_WEBHOOK_URL`
3. Push to main branch triggers auto-deploy

### Update via API Script:

```bash
export PORTAINER_URL="http://your-portainer:9000"
export PORTAINER_API_KEY="your_api_key"
export POSTGRES_PASSWORD="your_password"
export GIT_REPO_URL="https://your-git-repo.git"

chmod +x deploy/portainer-deploy.sh
./deploy/portainer-deploy.sh
```

## Method 2: Direct Docker via SSH

**Best for:** Simple deployments to a single Docker host

```bash
export DOCKER_HOST="your-server.com"
export DOCKER_USER="deploy"
export GIT_REPO="https://your-git-repo.git"

chmod +x deploy/docker-deploy.sh
./deploy/docker-deploy.sh
```

For local deployment:
```bash
export DOCKER_HOST="localhost"
export GIT_REPO="https://your-git-repo.git"
./deploy/docker-deploy.sh
```

## Method 3: Gitea Actions (Automated)

**Best for:** CI/CD pipeline automation

### Setup:

1. Add secrets to your Gitea repository:
   - `DOCKER_HOST`: your-server.com
   - `DOCKER_USER`: deploy
   - `DOCKER_SSH_KEY`: Your SSH private key

2. Push to main branch or modify backend files
3. Workflow automatically builds and deploys

### Manual trigger:
- Go to Actions → Deploy Backend to Docker → Run workflow

## Method 4: Portainer API via Gitea Actions

**Best for:** Portainer users with CI/CD

### Setup:

1. Get Portainer API key:
   - Portainer → User Settings → Access tokens → Add access token

2. Add secrets to Gitea:
   - `PORTAINER_URL`: http://your-portainer:9000
   - `PORTAINER_API_KEY`: your_api_key
   - `PORTAINER_STACK_ID`: stack_id (from Portainer URL)
   - `PORTAINER_WEBHOOK_URL`: webhook URL (optional)

3. Manually trigger workflow or use webhook

## Environment Variables

Required for all methods:
- `POSTGRES_PASSWORD`: Database password

Optional:
- `POSTGRES_USER`: Database user (default: rmtpw)
- `POSTGRES_DB`: Database name (default: rmtpocketwatcher)
- `BACKEND_PORT`: Backend port (default: 3000)
- `SCRAPE_INTERVAL`: Minutes between scrapes (default: 5)
- `SCRAPER_TIMEOUT`: Scraper timeout in ms (default: 30000)
- `SCRAPER_MAX_RETRIES`: Max retry attempts (default: 3)

## Monitoring

Check deployment status:
```bash
docker-compose ps
docker-compose logs -f backend
```

Via Portainer:
- Stacks → rmtpocketwatcher → Container logs

## Rollback

### Portainer:
1. Go to Stack → Editor
2. Change `IMAGE_TAG` to previous version
3. Update stack

### Direct Docker:
```bash
cd /opt/rmtpocketwatcher
git checkout <previous-commit>
docker-compose up -d --build backend
```

## Troubleshooting

**Stack won't start:**
- Check environment variables are set
- Verify database connection
- Check logs: `docker-compose logs backend`

**Portainer API fails:**
- Verify API key is valid
- Check endpoint ID matches your environment
- Ensure Portainer version supports Git deployment

**SSH deployment fails:**
- Verify SSH key has correct permissions (600)
- Check user has Docker permissions
- Ensure Git is installed on remote host
