|
| 1 | +# Environment Configuration |
| 2 | + |
| 3 | +This project uses a layered environment configuration system powered by `.envrc` and `direnv`. |
| 4 | + |
| 5 | +## How It Works |
| 6 | + |
| 7 | +Environment variables are loaded in the following order (later files override earlier ones): |
| 8 | + |
| 9 | +1. **`.env`** - Base configuration shared across all environments (committed to git) |
| 10 | +2. **`.env.$ENV`** - Environment-specific configuration (committed to git) |
| 11 | +3. **`.env.local`** - Local overrides and secrets (NOT committed to git) |
| 12 | + |
| 13 | +The `$ENV` variable is automatically determined by your current git branch: |
| 14 | + |
| 15 | +| Git Branch | Environment | Loaded File | |
| 16 | +| ----------------------- | ------------- | ------------------- | |
| 17 | +| `main` or `master` | `production` | `.env.production` | |
| 18 | +| `dev` or `develop` | `development` | `.env.development` | |
| 19 | +| Other branches | `local` | `.env.local` | |
| 20 | +| Custom branch with file | Custom | `.env.$BRANCH_NAME` | |
| 21 | + |
| 22 | +## File Structure |
| 23 | + |
| 24 | +``` |
| 25 | +.env # Base config (PORT, COMPILER_VERSION, etc.) |
| 26 | +.env.development # Development-specific (test API keys, local DB) |
| 27 | +.env.production # Production-specific (placeholder values) |
| 28 | +.env.local # Your personal secrets (NEVER commit this!) |
| 29 | +.env.example # Template showing all available variables |
| 30 | +``` |
| 31 | + |
| 32 | +## Setup Instructions |
| 33 | + |
| 34 | +### 1. Enable direnv (if not already installed) |
| 35 | + |
| 36 | +```bash |
| 37 | +# macOS |
| 38 | +brew install direnv |
| 39 | + |
| 40 | +# Add to your shell config (~/.zshrc) |
| 41 | +eval "$(direnv hook zsh)" |
| 42 | +``` |
| 43 | + |
| 44 | +### 2. Allow the .envrc file |
| 45 | + |
| 46 | +```bash |
| 47 | +direnv allow |
| 48 | +``` |
| 49 | + |
| 50 | +You should see: `✅ Loaded environment: development (branch: dev)` |
| 51 | + |
| 52 | +### 3. Create your .env.local file |
| 53 | + |
| 54 | +```bash |
| 55 | +cp .env.example .env.local |
| 56 | +``` |
| 57 | + |
| 58 | +Then edit `.env.local` with your actual secrets and API keys. |
| 59 | + |
| 60 | +## What Goes Where? |
| 61 | + |
| 62 | +### `.env` (Committed) |
| 63 | + |
| 64 | +- Non-sensitive defaults |
| 65 | +- Port numbers |
| 66 | +- Version numbers |
| 67 | +- Public configuration |
| 68 | + |
| 69 | +### `.env.development` / `.env.production` (Committed) |
| 70 | + |
| 71 | +- Environment-specific defaults |
| 72 | +- Test API keys (development only) |
| 73 | +- Environment-specific feature flags |
| 74 | +- Non-secret configuration |
| 75 | + |
| 76 | +### `.env.local` (NOT Committed) |
| 77 | + |
| 78 | +- **ALL secrets and API keys** |
| 79 | +- Database connection strings |
| 80 | +- Authentication tokens |
| 81 | +- Personal overrides |
| 82 | + |
| 83 | +## Wrangler Integration |
| 84 | + |
| 85 | +The `wrangler.toml` configuration supports environment-based deployments: |
| 86 | + |
| 87 | +```bash |
| 88 | +# Development deployment |
| 89 | +wrangler deploy --env development |
| 90 | + |
| 91 | +# Production deployment |
| 92 | +wrangler deploy --env production |
| 93 | +``` |
| 94 | + |
| 95 | +Environment variables from `.env.local` are automatically available during local development (`wrangler dev`). |
| 96 | + |
| 97 | +For production deployments, secrets should be set using: |
| 98 | + |
| 99 | +```bash |
| 100 | +wrangler secret put ADMIN_KEY --env production |
| 101 | +wrangler secret put TURNSTILE_SECRET_KEY --env production |
| 102 | +``` |
| 103 | + |
| 104 | +## Troubleshooting |
| 105 | + |
| 106 | +### Environment not loading? |
| 107 | + |
| 108 | +```bash |
| 109 | +# Re-allow the .envrc |
| 110 | +direnv allow |
| 111 | + |
| 112 | +# Check what's loaded |
| 113 | +direnv exec . env | grep DATABASE_URL |
| 114 | +``` |
| 115 | + |
| 116 | +### Wrong environment? |
| 117 | + |
| 118 | +Check your git branch: |
| 119 | + |
| 120 | +```bash |
| 121 | +git branch --show-current |
| 122 | +``` |
| 123 | + |
| 124 | +The `.envrc` automatically maps your branch to an environment. |
| 125 | + |
| 126 | +### Variables not available? |
| 127 | + |
| 128 | +Make sure: |
| 129 | + |
| 130 | +1. You've created `.env.local` from `.env.example` |
| 131 | +2. You've run `direnv allow` |
| 132 | +3. The variable exists in one of the .env files |
| 133 | + |
| 134 | +## Security Best Practices |
| 135 | + |
| 136 | +- ✅ **DO** commit `.env`, `.env.development`, `.env.production` |
| 137 | +- ✅ **DO** use test/dummy values in committed files |
| 138 | +- ✅ **DO** put all secrets in `.env.local` |
| 139 | +- ❌ **DON'T** commit `.env.local` |
| 140 | +- ⚠️ **BE CAREFUL** with `.envrc` — it is committed as part of the env-loading system, so never put secrets or credentials in it |
| 141 | +- ❌ **DON'T** put real secrets in any committed file |
| 142 | +- ❌ **DON'T** commit production credentials |
| 143 | + |
| 144 | +## GitHub Actions Integration |
| 145 | + |
| 146 | +This environment system works seamlessly in GitHub Actions workflows. See [.github/ENV_SETUP.md](.github/ENV_SETUP.md) for detailed documentation. |
| 147 | + |
| 148 | +### Quick Start |
| 149 | + |
| 150 | +```yaml |
| 151 | +steps: |
| 152 | + - uses: actions/checkout@v4 |
| 153 | + |
| 154 | + - name: Load environment variables |
| 155 | + uses: ./.github/actions/setup-env |
| 156 | + |
| 157 | + - name: Use environment variables |
| 158 | + run: echo "Version: $COMPILER_VERSION" |
| 159 | +``` |
| 160 | +
|
| 161 | +The action automatically: |
| 162 | +
|
| 163 | +- Detects environment from branch name |
| 164 | +- Loads `.env` and `.env.$ENV` files |
| 165 | +- Exports variables to workflow |
| 166 | + |
| 167 | +## Environment Variables Reference |
| 168 | + |
| 169 | +See `.env.example` for a complete list of available variables and their purposes. |
0 commit comments