Skip to content

Commit c056215

Browse files
committed
feat: add DEPLOYMENT_ENV variable for environment-specific website configuration (#335)
# Add DEPLOYMENT_ENV variable to improve environment handling This PR introduces a new `DEPLOYMENT_ENV` environment variable to better manage differences between production and preview deployments: - Added `DEPLOYMENT_ENV` to the CI workflow, automatically set to 'production' for main branch and 'preview' for pull requests - Updated `astro.config.mjs` to validate environment variables based on deployment type: - Production deployments require `PLAUSIBLE_PROXY_URL` to be set - Preview deployments make `PLAUSIBLE_PROXY_URL` optional (falls back to default) - Improved robots.txt configuration to use the new environment variable instead of `CF_PAGES_BRANCH` - Enhanced documentation in `DEPLOYMENT.md` with details about the new environment variable and validation requirements These changes make the deployment process more robust by ensuring all required environment variables are present before deployment and providing clearer error messages when configuration is incomplete.
1 parent 9246e58 commit c056215

File tree

3 files changed

+65
-90
lines changed

3 files changed

+65
-90
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ jobs:
152152
NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_ACCESS_TOKEN }}
153153
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
154154
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
155+
DEPLOYMENT_ENV: ${{ github.event_name == 'pull_request' && 'preview' || 'production' }}
155156
VITE_SUPABASE_URL: ${{ github.event_name == 'pull_request' && secrets.WEBSITE_PREVIEW_SUPABASE_URL || secrets.WEBSITE_PRODUCTION_SUPABASE_URL }}
156157
VITE_SUPABASE_ANON_KEY: ${{ github.event_name == 'pull_request' && secrets.WEBSITE_PREVIEW_SUPABASE_ANON_KEY || secrets.WEBSITE_PRODUCTION_SUPABASE_ANON_KEY }}
157158
PLAUSIBLE_PROXY_URL: ${{ secrets.WEBSITE_PLAUSIBLE_PROXY_URL }}

pkgs/website/DEPLOYMENT.md

Lines changed: 45 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,72 @@
11
# Website Deployment
22

3-
This document describes the deployment configuration for the pgflow documentation website.
3+
The pgflow documentation website is deployed to **Cloudflare Pages** via GitHub Actions.
44

5-
## Deployment Target
6-
7-
The website is deployed to **Cloudflare Pages** via GitHub Actions workflow.
8-
9-
- **Production URL**: https://www.pgflow.dev
10-
- **Preview URLs**: https://pr-{number}.pgflow.pages.dev (for pull requests)
5+
- **Production**: https://www.pgflow.dev
6+
- **Preview**: https://pr-{number}.pgflow.pages.dev
117

128
## Required GitHub Secrets
139

14-
The following secrets must be configured in the GitHub repository (Settings → Secrets and variables → Actions).
15-
16-
**Note**: Some secrets are namespaced (e.g., `WEBSITE_*`) at the GitHub level but are mapped to simpler names (e.g., `VITE_*`, `PLAUSIBLE_PROXY_URL`) when passed as environment variables to the build process.
17-
18-
### Cloudflare Configuration
19-
20-
- **`CLOUDFLARE_API_TOKEN`**
21-
- **GitHub Secret Name**: `CLOUDFLARE_API_TOKEN`
22-
- **Environment variable**: `CLOUDFLARE_API_TOKEN`
23-
- API token for deploying to Cloudflare Pages
24-
25-
- **`CLOUDFLARE_ACCOUNT_ID`**
26-
- **GitHub Secret Name**: `CLOUDFLARE_ACCOUNT_ID`
27-
- **Environment variable**: `CLOUDFLARE_ACCOUNT_ID`
28-
- Cloudflare account ID
29-
30-
### Supabase Configuration
31-
32-
Website deployment requires Supabase configuration for both preview and production environments:
10+
Configure these in repository Settings → Secrets and variables → Actions.
3311

34-
**Preview Environment:**
35-
- **`WEBSITE_PREVIEW_SUPABASE_URL`**
36-
- **GitHub Secret Name**: `WEBSITE_PREVIEW_SUPABASE_URL`
37-
- **Maps to environment variable**: `VITE_SUPABASE_URL` (in preview deployments)
38-
- Supabase project URL for preview deployments
12+
### Cloudflare
13+
- `CLOUDFLARE_API_TOKEN` - API token for Cloudflare Pages deployment
14+
- `CLOUDFLARE_ACCOUNT_ID` - Cloudflare account ID
3915

40-
- **`WEBSITE_PREVIEW_SUPABASE_ANON_KEY`**
41-
- **GitHub Secret Name**: `WEBSITE_PREVIEW_SUPABASE_ANON_KEY`
42-
- **Maps to environment variable**: `VITE_SUPABASE_ANON_KEY` (in preview deployments)
43-
- Supabase anonymous key for preview deployments
16+
### Supabase
4417

45-
**Production Environment:**
46-
- **`WEBSITE_PRODUCTION_SUPABASE_URL`**
47-
- **GitHub Secret Name**: `WEBSITE_PRODUCTION_SUPABASE_URL`
48-
- **Maps to environment variable**: `VITE_SUPABASE_URL` (in production deployments)
49-
- Supabase project URL for production
18+
**Preview:**
19+
- `WEBSITE_PREVIEW_SUPABASE_URL``VITE_SUPABASE_URL`
20+
- `WEBSITE_PREVIEW_SUPABASE_ANON_KEY``VITE_SUPABASE_ANON_KEY`
5021

51-
- **`WEBSITE_PRODUCTION_SUPABASE_ANON_KEY`**
52-
- **GitHub Secret Name**: `WEBSITE_PRODUCTION_SUPABASE_ANON_KEY`
53-
- **Maps to environment variable**: `VITE_SUPABASE_ANON_KEY` (in production deployments)
54-
- Supabase anonymous key for production
22+
**Production:**
23+
- `WEBSITE_PRODUCTION_SUPABASE_URL``VITE_SUPABASE_URL`
24+
- `WEBSITE_PRODUCTION_SUPABASE_ANON_KEY``VITE_SUPABASE_ANON_KEY`
5525

56-
### Analytics Configuration
26+
### Analytics
27+
- `WEBSITE_PLAUSIBLE_PROXY_URL``PLAUSIBLE_PROXY_URL` (required for production only)
28+
- Cloudflare Workers proxy URL for Plausible Analytics
29+
- Example: `https://your-worker.your-username.workers.dev`
30+
- Proxies requests to avoid ad blockers
5731

58-
- **`WEBSITE_PLAUSIBLE_PROXY_URL`** - Cloudflare Workers proxy URL for Plausible Analytics (website-specific)
59-
- **GitHub Secret Name**: `WEBSITE_PLAUSIBLE_PROXY_URL`
60-
- **Maps to environment variable**: `PLAUSIBLE_PROXY_URL` (used in `astro.config.mjs`)
61-
- Example value: `https://your-worker-name.your-username.workers.dev`
62-
- This proxies requests to Plausible to avoid ad blockers
63-
- If this URL becomes invalid, Plausible tracking will stop working
64-
- Note: This is separate from demo app analytics which uses its own proxy URL
32+
### Deployment Environment
33+
- `DEPLOYMENT_ENV` - Set automatically by workflow (`production` or `preview`)
34+
- Controls robots.txt and validates production requirements
35+
- Not set for build/test jobs (safe defaults apply)
6536

6637
## Deployment Workflow
6738

68-
The deployment is handled by the `deploy-website` job in `.github/workflows/ci.yml`:
39+
The `deploy-website` job in `.github/workflows/ci.yml`:
6940

70-
1. **Trigger**: Runs on push to `main` or when a pull request is opened/updated
71-
2. **Affected Check**: Only deploys if the website package is affected by changes (using Nx)
72-
3. **Build**: Builds the website with environment variables injected
73-
4. **Deploy**:
74-
- Production: Deploys to main branch on Cloudflare Pages
75-
- Preview: Deploys to PR-specific branch on Cloudflare Pages
41+
1. Triggers on push to `main` or PR updates
42+
2. Checks if website is affected (Nx)
43+
3. Sets `DEPLOYMENT_ENV` based on branch
44+
4. Builds with injected environment variables
45+
- Production: Requires `PLAUSIBLE_PROXY_URL`, enables indexing
46+
- Preview: Optional `PLAUSIBLE_PROXY_URL`, blocks indexing
47+
5. Deploys to Cloudflare Pages
7648

7749
## Local Development
7850

79-
For local development, the Plausible proxy URL falls back to a hardcoded value in `astro.config.mjs` if the `PLAUSIBLE_PROXY_URL` environment variable is not set.
80-
81-
To test with a specific proxy URL locally:
51+
No environment variables required. Optional overrides:
8252

8353
```bash
54+
# With custom proxy
8455
PLAUSIBLE_PROXY_URL=https://your-worker.your-username.workers.dev pnpm nx dev website
56+
57+
# Test production behavior
58+
DEPLOYMENT_ENV=production PLAUSIBLE_PROXY_URL=https://your-worker.your-username.workers.dev pnpm nx dev website
8559
```
8660

8761
## Troubleshooting
8862

89-
### Plausible Analytics Not Tracking
90-
91-
If Plausible stops tracking visitors:
92-
93-
1. Check if the Cloudflare Workers proxy URL is accessible:
94-
```bash
95-
curl -I https://your-worker-name.your-username.workers.dev/assets/script.hash.outbound-links.pageview-props.tagged-events.js
96-
```
97-
98-
2. If the URL fails (DNS resolution error or 404), update the `WEBSITE_PLAUSIBLE_PROXY_URL` secret in GitHub with the correct URL
99-
100-
3. The proxy URL may change if the Cloudflare Workers username changes
101-
102-
4. After updating the secret, redeploy the website (push to `main` or re-run the workflow)
103-
104-
### Deployment Fails
63+
**Analytics not tracking:**
64+
- Verify proxy URL: `curl -I https://your-worker.your-username.workers.dev/assets/script...js`
65+
- Update `WEBSITE_PLAUSIBLE_PROXY_URL` secret if needed
66+
- Redeploy website
10567

106-
- Verify all required secrets are configured in GitHub
107-
- Check the GitHub Actions workflow logs for specific error messages
108-
- Ensure the Cloudflare API token has the necessary permissions (Account → Cloudflare Pages - Edit)
68+
**Build errors:**
69+
- `DEPLOYMENT_ENV must be either 'production' or 'preview'` - Invalid value in workflow
70+
- `PLAUSIBLE_PROXY_URL required for production` - Missing `WEBSITE_PLAUSIBLE_PROXY_URL` secret
71+
- Check workflow logs for details
72+
- Verify Cloudflare API token permissions

pkgs/website/astro.config.mjs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ import { redirects } from './redirects.config.mjs';
1616
const GITHUB_REPO_URL = 'https://github.com/pgflow-dev/pgflow';
1717
const DISCORD_INVITE_URL = 'https://pgflow.dev/discord/';
1818
const EMAIL_URL = 'mailto:hello@pgflow.dev';
19+
20+
// Environment detection
21+
const DEPLOYMENT_ENV = process.env.DEPLOYMENT_ENV; // 'production' or 'preview' (optional)
22+
const isProduction = DEPLOYMENT_ENV === 'production';
23+
24+
// Validate DEPLOYMENT_ENV if set
25+
if (DEPLOYMENT_ENV && DEPLOYMENT_ENV !== 'production' && DEPLOYMENT_ENV !== 'preview') {
26+
throw new Error(`DEPLOYMENT_ENV must be either "production" or "preview", got: "${DEPLOYMENT_ENV}"`);
27+
}
28+
29+
// Require Plausible proxy URL only for production
30+
if (isProduction && !process.env.PLAUSIBLE_PROXY_URL) {
31+
throw new Error('PLAUSIBLE_PROXY_URL environment variable is required for production deployments');
32+
}
33+
1934
const PLAUSIBLE_PROXY = {
2035
url: process.env.PLAUSIBLE_PROXY_URL || 'https://wispy-pond-c6f8.jumski.workers.dev',
2136
eventPath: '/data/event',
@@ -64,17 +79,12 @@ export default defineConfig({
6479
starlight({
6580
favicon: '/favicons/favicon.ico',
6681
head: [
67-
// prevent robots from indexing the preview branches
68-
// it can be determined by checking the appropriate env variable
69-
// CF_PAGES_BRANCH != 'main'
82+
// prevent robots from indexing the preview deployments
7083
{
7184
tag: 'meta',
7285
attrs: {
7386
name: 'robots',
74-
content:
75-
process.env.CF_PAGES_BRANCH === 'main'
76-
? 'index,follow'
77-
: 'noindex,nofollow',
87+
content: isProduction ? 'index,follow' : 'noindex,nofollow',
7888
},
7989
},
8090
{
@@ -464,8 +474,8 @@ export default defineConfig({
464474
policy: [
465475
{
466476
userAgent: '*',
467-
allow: process.env.CF_PAGES_BRANCH === 'main' ? '/' : '',
468-
disallow: process.env.CF_PAGES_BRANCH === 'main' ? '' : '/',
477+
allow: isProduction ? '/' : '',
478+
disallow: isProduction ? '' : '/',
469479
},
470480
],
471481
}),

0 commit comments

Comments
 (0)