# DocFast CI/CD Deployment

This repository uses Forgejo Actions for automated deployment to production.

## Setup Instructions

### 1. Repository Secrets

Go to repository settings → Actions → Secrets and add these secrets:

- **SERVER_HOST**: `167.235.156.214`
- **SERVER_USER**: `root`
- **SSH_PRIVATE_KEY**: The private SSH key content from `/home/openclaw/.ssh/docfast`

### 2. How Deployment Works

**Trigger**: Push to `main` branch
**Process**:
1. SSH to production server
2. Pull latest code from git
3. Tag current Docker image for rollback
4. Build new Docker image
5. Stop current services
6. Start new services
7. Health check at `http://127.0.0.1:3100/health`
8. Rollback automatically if health check fails

### 3. Rollback Procedure

**Automatic Rollback**:
- Happens automatically if deployment fails health checks
- Reverts to the previously tagged image

**Manual Rollback**:
```bash
# On the production server
cd /root/docfast
./scripts/rollback.sh
```

**Emergency Rollback via SSH**:
```bash
ssh root@167.235.156.214
cd /root/docfast
docker compose down
docker tag docfast-docfast:rollback-YYYYMMDD-HHMMSS docfast-docfast:latest
docker compose up -d
```

### 4. Monitoring

- **Health Check**: `curl http://127.0.0.1:3100/health`
- **Service Status**: `docker compose ps`
- **Logs**: `docker compose logs -f`

### 5. File Structure

```
.forgejo/workflows/deploy.yml  # Main deployment workflow
scripts/rollback.sh           # Manual rollback script  
scripts/setup-secrets.sh      # Helper for setting up secrets
DEPLOYMENT.md                 # This documentation
```

### 6. Testing the Pipeline

1. Make a small change (e.g., bump version comment)
2. Commit and push to main branch
3. Check Actions tab in Forgejo to see deployment progress
4. Verify service is running with `curl http://127.0.0.1:3100/health`

## Troubleshooting

- **SSH Issues**: Ensure SSH key is properly added to secrets
- **Docker Build Issues**: Check server has enough disk space and memory
- **Health Check Fails**: Check if service is binding to correct port (3100)
- **Permission Issues**: Ensure user has Docker privileges on server