|
| 1 | +# Deployment Guide |
| 2 | + |
| 3 | +This project has **two independent deployment systems** — a static frontend and a serverless backend API. They are deployed separately. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Architecture Overview |
| 8 | + |
| 9 | +| | Frontend | Backend API | |
| 10 | +|---|---|---| |
| 11 | +| **Stack** | Astro (static site) | SST + AWS Lambda | |
| 12 | +| **Hosting** | GitHub Pages | AWS API Gateway + Lambda | |
| 13 | +| **Deploy trigger** | GitHub Actions | Manual (`pnpm run sst:deploy`) | |
| 14 | +| **URL** | `ctrimm.github.io/co2-emission-tracker` | API Gateway endpoint (set via `PUBLIC_API_URL` secret) | |
| 15 | +| **Data source** | Calls the backend API | Supabase database | |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## Frontend — GitHub Pages |
| 20 | + |
| 21 | +The Astro site is compiled to static HTML/CSS/JS and deployed to GitHub Pages. |
| 22 | + |
| 23 | +### How it deploys |
| 24 | + |
| 25 | +The `deploy.yml` workflow runs on three triggers: |
| 26 | + |
| 27 | +1. **Push to `main`** — any merge or direct commit automatically redeploys |
| 28 | +2. **Manual trigger** — via the GitHub Actions UI or CLI: |
| 29 | + ```bash |
| 30 | + gh workflow run deploy.yml --repo ctrimm/co2-emission-tracker |
| 31 | + ``` |
| 32 | +3. **After the nightly cron** — the CO2 emissions check runs at 5 AM UTC; if it succeeds, a deploy is triggered automatically to pick up fresh data |
| 33 | + |
| 34 | +### Required GitHub Secrets |
| 35 | + |
| 36 | +| Secret | Description | |
| 37 | +|---|---| |
| 38 | +| `NEXT_PUBLIC_SUPABASE_URL` | Supabase project URL | |
| 39 | +| `SUPABASE_SERVICE_ROLE_KEY` | Supabase service role key (used by cron script) | |
| 40 | +| `PUBLIC_API_URL` | The deployed AWS API Gateway URL | |
| 41 | + |
| 42 | +### Build details |
| 43 | + |
| 44 | +- Node version: **20** |
| 45 | +- Package manager: **pnpm 9** |
| 46 | +- Build command: `pnpm run build` (sets `NODE_OPTIONS=--max-old-space-size=8192` for large builds) |
| 47 | +- Output directory: `./dist` |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## Backend API — SST (AWS) |
| 52 | + |
| 53 | +The backend is a set of AWS Lambda functions exposed via API Gateway, managed by [SST](https://sst.dev). This is **deployed separately from the frontend** and only needs to be redeployed when backend logic changes. |
| 54 | + |
| 55 | +### API Routes |
| 56 | + |
| 57 | +| Method | Path | Handler | |
| 58 | +|---|---|---| |
| 59 | +| `GET` | `/emissions-unique` | Latest unique emissions per domain | |
| 60 | +| `GET` | `/emissions/{domain}` | Emissions history for a specific domain | |
| 61 | +| `GET` | `/sites` | List of monitored sites | |
| 62 | +| `GET` | `/stats` | Aggregate statistics | |
| 63 | +| `GET` | `/trend` | Emissions trend data | |
| 64 | +| `GET` | `/leaderboard` | Leaderboard rankings | |
| 65 | + |
| 66 | +CORS is restricted to `https://co2.ignitebright.com` and `http://localhost:4321`. |
| 67 | + |
| 68 | +### Deploying the backend |
| 69 | + |
| 70 | +Prerequisites: AWS credentials configured locally. |
| 71 | + |
| 72 | +```bash |
| 73 | +# Deploy to production |
| 74 | +pnpm run sst:deploy --stage production |
| 75 | + |
| 76 | +# Deploy to a personal dev stage |
| 77 | +pnpm run sst:deploy --stage <your-name> |
| 78 | + |
| 79 | +# Local development (runs Lambda handlers locally) |
| 80 | +pnpm run sst:dev |
| 81 | +``` |
| 82 | + |
| 83 | +### Required environment variables for SST |
| 84 | + |
| 85 | +| Variable | Description | |
| 86 | +|---|---| |
| 87 | +| `PUBLIC_SUPABASE_URL` | Supabase project URL | |
| 88 | +| `PUBLIC_SUPABASE_ANON_KEY` | Supabase anonymous/public API key | |
| 89 | + |
| 90 | +These are stored as SST secrets (`MySupabaseUrl`, `MySupabaseAnonRoleKey`) and injected into the Lambda environment automatically. |
| 91 | + |
| 92 | +### Removal policy |
| 93 | + |
| 94 | +- **Production stage** — resources are **retained** on `sst remove` (safe) |
| 95 | +- **Other stages** — resources are **deleted** on `sst remove` |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +## Nightly Cron Job |
| 100 | + |
| 101 | +The `co2-emissions-cron.yml` workflow runs daily at **5 AM UTC**. It: |
| 102 | + |
| 103 | +1. Runs `scripts/co2-emissions-check.js` to measure CO2 emissions for tracked sites |
| 104 | +2. Writes results to Supabase |
| 105 | +3. On success, automatically triggers a frontend redeploy so the site reflects fresh data |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +## Local Development |
| 110 | + |
| 111 | +```bash |
| 112 | +# Install dependencies |
| 113 | +pnpm install |
| 114 | + |
| 115 | +# Run the Astro frontend |
| 116 | +pnpm run dev |
| 117 | + |
| 118 | +# Run SST backend locally (requires AWS credentials) |
| 119 | +pnpm run sst:dev |
| 120 | +``` |
0 commit comments