Skip to content

Commit 7e005ea

Browse files
bhuntclaude
andcommitted
chore: add CI/CD pipeline and deployment guides
- Add GitHub Actions workflow for testing and deployment - Deploy API to Cloudflare Workers on main branch - Deploy web frontend to Cloudflare Pages - Add deployment and setup guides - Add environment variables template 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 5f56cbb commit 7e005ea

4 files changed

Lines changed: 516 additions & 0 deletions

File tree

.env.example

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Cloudflare Configuration
2+
CLOUDFLARE_API_TOKEN=your_api_token_here
3+
CLOUDFLARE_ACCOUNT_ID=your_account_id_here
4+
5+
# Turso Database
6+
TURSO_URL=libsql://your-db.turso.io
7+
TURSO_AUTH_TOKEN=your_auth_token_here
8+
9+
# OpenRouter API
10+
OPENROUTER_API_KEY=your_api_key_here
11+
12+
# Worker Environment
13+
ENVIRONMENT=production

.github/workflows/deploy.yml

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
name: Build and Deploy
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
env:
10+
NODE_VERSION: '20'
11+
12+
jobs:
13+
test:
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
working-directory: ['binary-math-api', 'binary-math-web']
18+
19+
steps:
20+
- uses: actions/checkout@v4
21+
22+
- name: Setup Node.js
23+
uses: actions/setup-node@v4
24+
with:
25+
node-version: ${{ env.NODE_VERSION }}
26+
cache: 'npm'
27+
cache-dependency-path: ${{ matrix.working-directory }}/package-lock.json
28+
29+
- name: Install dependencies
30+
working-directory: ${{ matrix.working-directory }}
31+
run: npm ci
32+
33+
- name: Run tests
34+
working-directory: ${{ matrix.working-directory }}
35+
run: npm run test --if-present
36+
37+
- name: Build
38+
working-directory: ${{ matrix.working-directory }}
39+
run: npm run build --if-present
40+
41+
deploy-api:
42+
needs: test
43+
runs-on: ubuntu-latest
44+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
45+
46+
steps:
47+
- uses: actions/checkout@v4
48+
49+
- name: Setup Node.js
50+
uses: actions/setup-node@v4
51+
with:
52+
node-version: ${{ env.NODE_VERSION }}
53+
cache: 'npm'
54+
cache-dependency-path: binary-math-api/package-lock.json
55+
56+
- name: Install dependencies
57+
working-directory: binary-math-api
58+
run: npm ci
59+
60+
- name: Build API
61+
working-directory: binary-math-api
62+
run: npm run build --if-present
63+
64+
- name: Deploy to Cloudflare Workers
65+
working-directory: binary-math-api
66+
run: npx wrangler deploy --env production
67+
env:
68+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
69+
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
70+
TURSO_URL: ${{ secrets.TURSO_URL }}
71+
TURSO_AUTH_TOKEN: ${{ secrets.TURSO_AUTH_TOKEN }}
72+
OPENROUTER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }}
73+
74+
- name: Post deployment status
75+
if: always()
76+
run: |
77+
if [ "${{ job.status }}" = "success" ]; then
78+
echo "✅ Deployment to Cloudflare Workers successful"
79+
else
80+
echo "❌ Deployment failed"
81+
exit 1
82+
fi
83+
84+
deploy-web:
85+
needs: test
86+
runs-on: ubuntu-latest
87+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
88+
89+
steps:
90+
- uses: actions/checkout@v4
91+
92+
- name: Setup Node.js
93+
uses: actions/setup-node@v4
94+
with:
95+
node-version: ${{ env.NODE_VERSION }}
96+
cache: 'npm'
97+
cache-dependency-path: binary-math-web/package-lock.json
98+
99+
- name: Install dependencies
100+
working-directory: binary-math-web
101+
run: npm ci
102+
103+
- name: Build web
104+
working-directory: binary-math-web
105+
run: npm run build
106+
107+
- name: Deploy web to Cloudflare Pages
108+
working-directory: binary-math-web
109+
run: npx wrangler pages deploy dist --project-name=binary-math
110+
env:
111+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
112+
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}

CLOUDFLARE_SETUP.md

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# Cloudflare Workers Setup Guide
2+
3+
This guide walks through setting up Cloudflare Workers and Pages for the binary-math-system project.
4+
5+
## Prerequisites
6+
7+
- Cloudflare account (free tier works)
8+
- Domain registered with Cloudflare
9+
- Wrangler CLI installed: `npm install -g wrangler`
10+
11+
## Step 1: Get Your Cloudflare Credentials
12+
13+
### Account ID
14+
1. Go to https://dash.cloudflare.com
15+
2. Select your account
16+
3. In the right sidebar under "Account Details", find your **Account ID**
17+
4. Copy this to `CLOUDFLARE_ACCOUNT_ID` secret
18+
19+
### API Token
20+
1. Go to https://dash.cloudflare.com/profile/api-tokens
21+
2. Click "Create Token"
22+
3. Use "Edit Cloudflare Workers" template
23+
4. Grant permissions:
24+
- ✅ Account.Workers KV Storage
25+
- ✅ Account.Workers Scripts
26+
- ✅ Zone.Workers Routes
27+
5. Account Resources: All accounts or your specific account
28+
6. Copy token to `CLOUDFLARE_API_TOKEN` secret
29+
30+
## Step 2: Configure Your Domain
31+
32+
### Custom Domain Setup
33+
34+
For `api.binarymath.dev`:
35+
36+
1. Go to your domain settings in Cloudflare
37+
2. Navigate to **Workers****Routes**
38+
3. Create route:
39+
- Pattern: `api.binarymath.dev/*`
40+
- Worker: `binary-math-api-production`
41+
- Zone: Select your zone
42+
43+
### Pages Setup
44+
45+
For the web frontend:
46+
47+
1. Go to **Pages****Create a project**
48+
2. Select repository: `binary-math-system`
49+
3. Framework preset: **Vite**
50+
4. Build command: `npm run build`
51+
5. Build output directory: `dist`
52+
6. Environment variables:
53+
- Add any API endpoints needed
54+
7. Deploy
55+
56+
## Step 3: KV Namespace Setup
57+
58+
The API uses KV storage for caching:
59+
60+
1. Go to **Workers****KV**
61+
2. Create namespace: `binary-kv`
62+
3. Note the ID (format: `xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`)
63+
4. Add to `wrangler.toml`:
64+
```toml
65+
[[kv_namespaces]]
66+
binding = "KV"
67+
id = "your-kv-namespace-id"
68+
```
69+
70+
## Step 4: Database Configuration
71+
72+
The API uses Turso SQLite database:
73+
74+
### Get Turso Credentials
75+
1. Create account at https://turso.io
76+
2. Create database: `binary-math-db`
77+
3. Get connection URL and token
78+
4. Add to GitHub secrets:
79+
- `TURSO_URL`: Your database URL
80+
- `TURSO_AUTH_TOKEN`: Your auth token
81+
82+
### Initialize Database
83+
84+
The API will automatically run migrations on first deployment. To seed data:
85+
86+
```bash
87+
cd binary-math-api
88+
npx wrangler execute --name seed-problems
89+
```
90+
91+
## Step 5: Environment Variables
92+
93+
Add these to Cloudflare Workers environment:
94+
95+
1. Go to **Workers****Settings**
96+
2. Go to **Environment Variables**
97+
3. Add for production environment:
98+
99+
```
100+
TURSO_URL = libsql://your-db.turso.io
101+
TURSO_AUTH_TOKEN = your_token
102+
OPENROUTER_API_KEY = your_key_if_using_ai
103+
DATABASE_URL = libsql://your-db.turso.io?authToken=token
104+
```
105+
106+
## Step 6: Verify Deployment
107+
108+
After first automated deployment:
109+
110+
### Check Worker Status
111+
```bash
112+
wrangler deployments list --name binary-math-api
113+
```
114+
115+
### Test API Endpoint
116+
```bash
117+
curl https://api.binarymath.dev/health
118+
```
119+
120+
### Check Pages
121+
- Visit your Pages deployment URL
122+
- Should see the React app loaded
123+
124+
## Step 7: Configure CI/CD Secrets
125+
126+
Add these to GitHub repository secrets:
127+
- Settings → Secrets and variables → Actions
128+
129+
```
130+
CLOUDFLARE_API_TOKEN = your_token
131+
CLOUDFLARE_ACCOUNT_ID = your_account_id
132+
TURSO_URL = your_db_url
133+
TURSO_AUTH_TOKEN = your_db_token
134+
OPENROUTER_API_KEY = your_api_key (if used)
135+
```
136+
137+
## Troubleshooting
138+
139+
### Deployment Fails with "Invalid Token"
140+
- Verify `CLOUDFLARE_API_TOKEN` is correct
141+
- Token must have "Edit Cloudflare Workers" permission
142+
- May need to regenerate token
143+
144+
### Custom Domain Not Working
145+
- Verify domain DNS is using Cloudflare nameservers
146+
- Check Worker routes configuration
147+
- Ensure SSL/TLS is enabled
148+
149+
### Database Connection Fails
150+
- Verify `TURSO_URL` and `TURSO_AUTH_TOKEN` are correct
151+
- Check that database exists in Turso dashboard
152+
- Ensure environment variables are set in Cloudflare
153+
154+
### Pages Build Fails
155+
- Check build command: `npm run build`
156+
- Verify Node.js version compatibility
157+
- Check that all dependencies are in package.json
158+
159+
### Logs and Debugging
160+
161+
View worker logs:
162+
```bash
163+
wrangler tail --name binary-math-api
164+
```
165+
166+
View Pages deployment:
167+
```bash
168+
wrangler pages project list
169+
```
170+
171+
## Rollback
172+
173+
To revert to previous version:
174+
175+
```bash
176+
wrangler rollback --name binary-math-api
177+
```
178+
179+
## Monitoring
180+
181+
Set up monitoring in Cloudflare:
182+
1. Go to **Analytics****Workers**
183+
2. View request metrics and errors
184+
3. Set up alerts for error rates
185+
186+
## Costs
187+
188+
- **Workers**: 100,000 free requests/day
189+
- **KV**: 100,000 free read ops/day
190+
- **Pages**: Unlimited free deployments
191+
- **Custom domain**: Included with Cloudflare account
192+
193+
---
194+
195+
For additional help:
196+
- Cloudflare Workers Docs: https://developers.cloudflare.com/workers/
197+
- Wrangler Docs: https://developers.cloudflare.com/workers/wrangler/

0 commit comments

Comments
 (0)