Skip to content

Commit 2e556e4

Browse files
committed
feat: add PR preview deployments for demo app (#330)
# Add PR Preview Deployments for Demo App This PR enhances our CI/CD pipeline to support preview deployments for the demo app on pull requests. Key changes include: - Modified the `deploy-demo` job to run on both PRs and main branch pushes - Added dynamic environment selection (`preview` or `production`) based on the event type - Replaced hardcoded Supabase credentials with environment variables from GitHub secrets - Added conditional deployment logic to deploy to preview URLs for PRs and production for main branch - Updated the deployment comment action to include preview URLs for PR deployments - Added environment variable inputs to build configurations in project.json files to ensure proper cache invalidation when environment variables change These changes will allow reviewers to see live previews of the demo app for each PR before merging to main.
1 parent 778f391 commit 2e556e4

File tree

5 files changed

+48
-34
lines changed

5 files changed

+48
-34
lines changed

.github/workflows/ci.yml

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,13 @@ jobs:
185185
deploy-demo:
186186
needs: [build-and-test, edge-worker-e2e]
187187
runs-on: ubuntu-latest
188-
# Only run on main branch pushes (production) - skip PRs for now
189-
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
190-
environment: production
188+
environment: ${{ github.event_name == 'pull_request' && 'preview' || 'production' }}
191189
env:
192190
NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_ACCESS_TOKEN }}
193191
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
194192
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
195-
# Hardcoded for testing - these are public values
196-
VITE_SUPABASE_URL: https://bsgbmmbmlmcmdnheuwmt.supabase.co
197-
VITE_SUPABASE_ANON_KEY: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImJzZ2JtbWJtbG1jbWRuaGV1d210Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NjIzNDA2NzIsImV4cCI6MjA3NzkxNjY3Mn0.Uoy8iqxycrqd4b6LPMMXWWSYrP1BDRMrJVgM2_vtl6o
193+
VITE_SUPABASE_URL: ${{ github.event_name == 'pull_request' && secrets.DEMO_PREVIEW_SUPABASE_URL || secrets.DEMO_PRODUCTION_SUPABASE_URL }}
194+
VITE_SUPABASE_ANON_KEY: ${{ github.event_name == 'pull_request' && secrets.DEMO_PREVIEW_SUPABASE_ANON_KEY || secrets.DEMO_PRODUCTION_SUPABASE_ANON_KEY }}
198195
steps:
199196
- uses: actions/checkout@v4
200197
with:
@@ -208,19 +205,7 @@ jobs:
208205
- name: Verify NX_BASE and NX_HEAD are set
209206
run: echo "BASE=$NX_BASE HEAD=$NX_HEAD"
210207

211-
- name: Check if demo is affected
212-
id: check-affected
213-
run: |
214-
if pnpm nx show projects --affected -t build --base="$NX_BASE" --head="$NX_HEAD" | grep -q "^demo$"; then
215-
echo "affected=true" >> $GITHUB_OUTPUT
216-
echo "Demo is affected by changes"
217-
else
218-
echo "affected=false" >> $GITHUB_OUTPUT
219-
echo "Demo is not affected by changes - skipping deployment"
220-
fi
221-
222208
- name: Validate Supabase environment variables
223-
if: steps.check-affected.outputs.affected == 'true'
224209
run: |
225210
if [ -z "$VITE_SUPABASE_URL" ]; then
226211
echo "❌ Error: VITE_SUPABASE_URL is not set"
@@ -239,18 +224,22 @@ jobs:
239224
fi
240225
echo "✅ Supabase environment variables are valid"
241226
242-
- name: Deploy demo to production
227+
- name: Deploy demo
243228
id: deploy-demo
244-
if: steps.check-affected.outputs.affected == 'true'
229+
env:
230+
PREVIEW_NAME: pr-${{ github.event.pull_request.number }}
245231
run: |
246-
echo "Deploying demo to production (demo.pgflow.dev)..."
247-
pnpm nx run demo:deploy --skip-nx-cache
232+
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
233+
pnpm nx affected -t deploy:preview --projects=demo --base="$NX_BASE" --head="$NX_HEAD"
234+
else
235+
pnpm nx affected -t deploy --projects=demo --base="$NX_BASE" --head="$NX_HEAD"
236+
fi
248237
249238
- name: Post deployment comment
250-
if: always()
239+
if: success()
251240
uses: ./.github/actions/deployment-comment
252241
with:
253242
project-name: Demo
243+
preview-url: https://pr-${{ github.event.pull_request.number }}-pgflow-demo.jumski.workers.dev
254244
production-url: https://demo.pgflow.dev
255-
# No preview URL - we only deploy production from main branch
256245

apps/demo/project.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,16 @@
2626
"command": "vite build",
2727
"cwd": "apps/demo"
2828
},
29-
"outputs": ["{projectRoot}/.svelte-kit"]
29+
"outputs": ["{projectRoot}/.svelte-kit"],
30+
"inputs": [
31+
"{projectRoot}/wrangler.toml",
32+
{
33+
"env": "VITE_SUPABASE_URL"
34+
},
35+
{
36+
"env": "VITE_SUPABASE_ANON_KEY"
37+
}
38+
]
3039
},
3140
"preview": {
3241
"executor": "nx:run-commands",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Create extensions and required schemas for pgflow demo
2+
3+
-- Enable pg_cron extension
4+
CREATE EXTENSION IF NOT EXISTS pg_cron WITH SCHEMA pg_catalog;
5+
6+
-- Enable pg_net extension
7+
CREATE EXTENSION IF NOT EXISTS pg_net;
8+
9+
-- Create cron schema if it doesn't exist
10+
CREATE SCHEMA IF NOT EXISTS cron;
11+
12+
-- Create net schema if it doesn't exist
13+
CREATE SCHEMA IF NOT EXISTS net;
14+
15+
-- Grant permissions to postgres role
16+
GRANT USAGE ON SCHEMA cron TO postgres;
17+
GRANT USAGE ON SCHEMA net TO postgres;
18+
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA cron TO postgres;

apps/demo/supabase/seeds/watchdog_article_worker.sql

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
-- Enable required extensions
2-
CREATE EXTENSION IF NOT EXISTS pg_cron;
3-
CREATE EXTENSION IF NOT EXISTS pg_net;
4-
5-
-- Grant necessary permissions
6-
GRANT USAGE ON SCHEMA cron TO postgres;
7-
GRANT USAGE ON SCHEMA net TO postgres;
8-
91
-- Remove existing job if it exists to prevent duplicates
102
SELECT cron.unschedule(jobname)
113
FROM cron.job

pkgs/website/project.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
"executor": "nx:run-commands",
1010
"inputs": [
1111
"default",
12-
"^production"
12+
"^production",
13+
{
14+
"env": "VITE_SUPABASE_URL"
15+
},
16+
{
17+
"env": "VITE_SUPABASE_ANON_KEY"
18+
}
1319
],
1420
"outputs": ["{options.outputPath}"],
1521
"options": {

0 commit comments

Comments
 (0)