-
-
Notifications
You must be signed in to change notification settings - Fork 206
179 lines (148 loc) · 6.09 KB
/
Copy pathpr-tests.yml
File metadata and controls
179 lines (148 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
name: PR Tests
on:
pull_request:
branches:
- main
push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
# TODO: Re-enable type checking after fixing remaining TypeScript errors
# Tracked in follow-up issue
# - name: Type check
# run: npm run type-check
- name: Run unit tests
run: npm test
- name: Build core package
run: npm run build:core
- name: Create fresh D1 database for PR
id: create-db
run: |
cd my-sonicjs-app
BRANCH_NAME="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
# Limit to 43 chars to allow for "sonicjs-pr-" prefix (11 chars) = 54 total
# Remove trailing hyphens to avoid invalid worker names
SAFE_BRANCH=$(echo "$BRANCH_NAME" | sed 's/[^a-zA-Z0-9-]/-/g' | cut -c1-43 | sed 's/-*$//')
DB_NAME="sonicjs-pr-${SAFE_BRANCH}"
echo "Creating fresh D1 database: $DB_NAME"
# Check if database already exists
EXISTING_DB=$(npx wrangler d1 list --json | jq -r ".[] | select(.name == \"$DB_NAME\") | .uuid" 2>/dev/null || echo "")
if [ -n "$EXISTING_DB" ]; then
echo "Database $DB_NAME already exists with ID: $EXISTING_DB"
DB_ID="$EXISTING_DB"
else
# Create new database
CREATE_OUTPUT=$(npx wrangler d1 create "$DB_NAME" 2>&1)
echo "$CREATE_OUTPUT"
# Extract database ID from creation output
DB_ID=$(echo "$CREATE_OUTPUT" | grep -oP 'database_id\s*=\s*"\K[^"]+' || echo "")
if [ -z "$DB_ID" ]; then
# Try alternative extraction method
DB_ID=$(npx wrangler d1 list --json | jq -r ".[] | select(.name == \"$DB_NAME\") | .uuid")
fi
fi
if [ -z "$DB_ID" ]; then
echo "Error: Failed to get database ID"
exit 1
fi
echo "Database ID: $DB_ID"
echo "db_id=$DB_ID" >> $GITHUB_OUTPUT
echo "db_name=$DB_NAME" >> $GITHUB_OUTPUT
# Update wrangler.toml with the new database ID
sed -i "s/database_id = \"[^\"]*\"/database_id = \"$DB_ID\"/" wrangler.toml
sed -i "s/database_name = \"[^\"]*\"/database_name = \"$DB_NAME\"/" wrangler.toml
echo "Updated wrangler.toml with new database"
grep -A2 "d1_databases" wrangler.toml
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
- name: Run D1 migrations
run: |
cd my-sonicjs-app
echo "Applying migrations to database: ${{ steps.create-db.outputs.db_name }}"
npx wrangler d1 migrations apply ${{ steps.create-db.outputs.db_name }} --remote
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
- name: Deploy to Cloudflare Workers Preview
id: deploy
run: |
cd my-sonicjs-app
# Deploy to preview with unique name based on PR/branch
# Cloudflare Workers has a 54 character limit for script names with previews
# Prefix "sonicjs-pr-" is 11 chars, so max branch name is 43 chars
BRANCH_NAME="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
# Limit to 43 chars to allow for "sonicjs-pr-" prefix (11 chars) = 54 total
# Remove trailing hyphens to avoid invalid worker names
SAFE_BRANCH=$(echo "$BRANCH_NAME" | sed 's/[^a-zA-Z0-9-]/-/g' | cut -c1-43 | sed 's/-*$//')
# Deploy and capture the URL (don't exit on error)
set +e
echo "Deploying to preview environment: sonicjs-pr-${SAFE_BRANCH}"
DEPLOY_OUTPUT=$(npx wrangler deploy --name "sonicjs-pr-${SAFE_BRANCH}" 2>&1)
DEPLOY_EXIT_CODE=$?
set -e
echo "=== Wrangler Deploy Output ==="
echo "$DEPLOY_OUTPUT"
echo "=== End Deploy Output ==="
if [ $DEPLOY_EXIT_CODE -ne 0 ]; then
echo "Error: Wrangler deploy failed with exit code $DEPLOY_EXIT_CODE"
exit 1
fi
# Extract the URL from wrangler output
PREVIEW_URL=$(echo "$DEPLOY_OUTPUT" | grep -oP 'https://[^\s]+\.workers\.dev' | head -1)
if [ -z "$PREVIEW_URL" ]; then
echo "Failed to extract preview URL from wrangler output"
exit 1
fi
echo "Preview URL: $PREVIEW_URL"
echo "preview_url=$PREVIEW_URL" >> $GITHUB_OUTPUT
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
- name: Wait for preview deployment
run: |
echo "Waiting for preview to be ready..."
for i in {1..30}; do
if curl -s -o /dev/null -w "%{http_code}" "${{ steps.deploy.outputs.preview_url }}" | grep -q "200\|301\|302"; then
echo "Preview is ready!"
exit 0
fi
echo "Attempt $i/30: Preview not ready yet, waiting..."
sleep 10
done
echo "Preview failed to become ready"
exit 1
- name: Install Playwright browsers
run: npx playwright install --with-deps chromium
- name: Run E2E tests against preview
run: npm run e2e
env:
CI: true
BASE_URL: ${{ steps.deploy.outputs.preview_url }}
- name: Upload test results
if: always()
uses: actions/upload-artifact@v4
with:
name: playwright-report
path: playwright-report/
retention-days: 7
- name: Upload test videos
if: failure()
uses: actions/upload-artifact@v4
with:
name: test-videos
path: test-results/
retention-days: 7