Deploy with Cloudflare
Cloudflare provides several services that integrate well with OpenCodeHub. This guide covers using Cloudflare Tunnel for secure access and R2 for storage.
Cloudflare Services Overview
Section titled “Cloudflare Services Overview”| Service | Use Case |
|---|---|
| Cloudflare Tunnel | Securely expose your server without opening ports |
| Cloudflare R2 | S3-compatible object storage |
| Cloudflare DNS | Fast DNS with DDoS protection |
| Cloudflare Pages | Deploy docs-site (static) |
Part 1: Cloudflare Tunnel
Section titled “Part 1: Cloudflare Tunnel”Cloudflare Tunnel (formerly Argo Tunnel) creates an encrypted tunnel from your server to Cloudflare’s edge, eliminating the need to open ports.
Prerequisites
Section titled “Prerequisites”- OpenCodeHub running on
localhost:3000 - Cloudflare account with a domain
cloudflaredCLI installed
Install cloudflared
Section titled “Install cloudflared”# Debian/Ubuntucurl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb -o cloudflared.debsudo dpkg -i cloudflared.deb
# Or via package managercurl -L https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-archive-keyring.gpgecho "deb [signed-by=/usr/share/keyrings/cloudflare-archive-keyring.gpg] https://pkg.cloudflare.com/cloudflared $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/cloudflared.listsudo apt update && sudo apt install cloudflaredAuthenticate
Section titled “Authenticate”cloudflared tunnel login# This opens a browser to authenticate with your Cloudflare accountCreate Tunnel
Section titled “Create Tunnel”# Create tunnelcloudflared tunnel create opencodehub
# Note the tunnel ID and credentials file path# Example: ~/.cloudflared/<tunnel-id>.jsonConfigure Tunnel
Section titled “Configure Tunnel”Create ~/.cloudflared/config.yml:
tunnel: <your-tunnel-id>credentials-file: /home/user/.cloudflared/<tunnel-id>.json
ingress: # Main application - hostname: git.yourdomain.com service: http://localhost:3000 originRequest: noTLSVerify: true connectTimeout: 30s # Important for git operations disableChunkedEncoding: true
# Catch-all (required) - service: http_status:404Route DNS
Section titled “Route DNS”cloudflared tunnel route dns opencodehub git.yourdomain.comRun Tunnel
Section titled “Run Tunnel”# Testcloudflared tunnel run opencodehub
# Install as servicesudo cloudflared service installsudo systemctl start cloudflaredsudo systemctl enable cloudflaredVerify
Section titled “Verify”Visit https://git.yourdomain.com - it should load OpenCodeHub!
Part 2: Cloudflare R2 Storage
Section titled “Part 2: Cloudflare R2 Storage”R2 is S3-compatible object storage with no egress fees.
Create R2 Bucket
Section titled “Create R2 Bucket”- Go to Cloudflare Dashboard → R2
- Click Create bucket
- Name:
opencodehub-repos - Location: Choose nearest region
Create API Tokens
Section titled “Create API Tokens”- Go to R2 → Manage R2 API Tokens
- Click Create API Token
- Permissions: Object Read & Write
- Specify bucket:
opencodehub-repos - Copy the Access Key ID and Secret Access Key
Configure OpenCodeHub
Section titled “Configure OpenCodeHub”Add to your .env:
STORAGE_TYPE=s3STORAGE_BUCKET=opencodehub-reposSTORAGE_REGION=autoSTORAGE_ENDPOINT=https://<account-id>.r2.cloudflarestorage.comS3_ACCESS_KEY=<your-r2-access-key-id>S3_SECRET_KEY=<your-r2-secret-access-key>Find your account ID in Cloudflare Dashboard → R2 → Overview.
Performance Note
Section titled “Performance Note”Part 3: Cloudflare DNS
Section titled “Part 3: Cloudflare DNS”Recommended DNS Settings
Section titled “Recommended DNS Settings”| Type | Name | Content | Proxy |
|---|---|---|---|
| CNAME | git | <tunnel-id>.cfargotunnel.com | Proxied (orange) |
If using Tunnel, the CNAME is created automatically.
Security Settings
Section titled “Security Settings”-
Go to SSL/TLS → Overview
-
Set mode to Full (strict)
-
Go to Security → WAF
-
Enable managed rules
Part 4: Deploy Docs to Cloudflare Pages
Section titled “Part 4: Deploy Docs to Cloudflare Pages”The documentation site can be deployed to Cloudflare Pages for free.
Connect Repository
Section titled “Connect Repository”- Go to Workers & Pages → Create application
- Select Pages → Connect to Git
- Choose your OpenCodeHub repository
Build Settings
Section titled “Build Settings”| Setting | Value |
|---|---|
| Build command | cd docs-site && npm install && npm run build |
| Build output directory | docs-site/dist |
| Root directory | / |
Environment Variables
Section titled “Environment Variables”| Variable | Value |
|---|---|
NODE_VERSION | 20 |
Custom Domain
Section titled “Custom Domain”- Go to Pages project → Custom domains
- Add
docs.yourdomain.com - Cloudflare will configure DNS automatically
Complete Architecture
Section titled “Complete Architecture”┌─────────────────────────────────────────────────────────┐│ Cloudflare Edge ││ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ││ │ DNS │ │ WAF │ │ CDN │ ││ └─────────────┘ └─────────────┘ └─────────────┘ ││ │ │ ││ ▼ ▼ ││ ┌─────────────────┐ ┌─────────────────┐ ││ │ Cloudflare │ │ Cloudflare │ ││ │ Tunnel │ │ Pages │ ││ │ (git.domain) │ │ (docs.domain) │ ││ └────────┬────────┘ └─────────────────┘ │└───────────┼─────────────────────────────────────────────┘ │ ▼ (encrypted tunnel) ┌───────────────────┐ │ Your Server │ │ ┌─────────────┐ │ │ │ OpenCodeHub │ │ │ │ :3000 │ │ │ └──────┬──────┘ │ │ │ │ │ ▼ │ │ ┌─────────────┐ │ │ │ PostgreSQL │ │ │ └─────────────┘ │ └───────────────────┘ │ ▼ (S3 API) ┌───────────────────┐ │ Cloudflare R2 │ │ (repo storage) │ └───────────────────┘Benefits of Cloudflare Setup
Section titled “Benefits of Cloudflare Setup”| Benefit | Description |
|---|---|
| No open ports | Tunnel handles all ingress |
| DDoS protection | Free, always-on |
| Free SSL | Automatic via Cloudflare |
| No egress fees | R2 has zero egress costs |
| Global CDN | Static assets cached at edge |
| Analytics | Built-in traffic analytics |
Troubleshooting
Section titled “Troubleshooting”Tunnel Connection Drops
Section titled “Tunnel Connection Drops”# Check tunnel statuscloudflared tunnel info opencodehub
# View logssudo journalctl -u cloudflared -f502 Bad Gateway
Section titled “502 Bad Gateway”- Verify OpenCodeHub is running on port 3000
- Check tunnel config points to correct port
- Verify
localhost:3000is accessible locally
R2 Access Denied
Section titled “R2 Access Denied”- Verify API token has correct bucket permissions
- Check Access Key ID and Secret are correct
- Verify endpoint URL format
Environment Example
Section titled “Environment Example”Complete .env for Cloudflare setup:
NODE_ENV=productionPORT=3000SITE_URL=https://git.yourdomain.com
# Database (local PostgreSQL)DATABASE_DRIVER=postgresDATABASE_URL=postgresql://opencodehub:password@localhost:5432/opencodehub
# SecurityJWT_SECRET=<64-char-hex>SESSION_SECRET=<64-char-hex>INTERNAL_HOOK_SECRET=<64-char-hex>
# Cloudflare R2 StorageSTORAGE_TYPE=s3STORAGE_BUCKET=opencodehub-reposSTORAGE_REGION=autoSTORAGE_ENDPOINT=https://<account-id>.r2.cloudflarestorage.comS3_ACCESS_KEY=<r2-access-key>S3_SECRET_KEY=<r2-secret-key>