Skip to content

Commit 3e177ba

Browse files
committed
Split preview deployment logic
1 parent 6af10f3 commit 3e177ba

File tree

7 files changed

+196
-199
lines changed

7 files changed

+196
-199
lines changed

.github/actions/check/action.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,8 @@ description: "Install dependencies and run formatting, linting, type checking, a
44
runs:
55
using: "composite"
66
steps:
7-
- name: Setup Bun
8-
uses: oven-sh/setup-bun@v2
9-
with:
10-
bun-version: latest
11-
12-
- name: Install dependencies
13-
shell: bash
14-
run: bun install --frozen-lockfile --prefer-offline
7+
- name: Setup Bun and dependencies
8+
uses: ./.github/actions/setup
159

1610
- name: Run checks
1711
shell: bash

.github/actions/setup/action.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: "Setup Bun and Dependencies"
2+
description: "Setup Bun and install dependencies"
3+
4+
runs:
5+
using: "composite"
6+
steps:
7+
- name: Setup Bun
8+
uses: oven-sh/setup-bun@v2
9+
with:
10+
bun-version: latest
11+
12+
- name: Install dependencies
13+
shell: bash
14+
run: bun install --frozen-lockfile --prefer-offline

.github/workflows/check.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ name: Code Quality Checks
22

33
on:
44
push:
5-
branches:
6-
- main
5+
branches: [main]
76
pull_request:
8-
branches:
9-
- main
7+
branches: [main]
108

119
jobs:
1210
check:

.github/workflows/deploy.yml

Lines changed: 0 additions & 187 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Cleanup Preview Environment
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
7+
jobs:
8+
cleanup:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Delete preview deployment
12+
uses: cloudflare/wrangler-action@v3
13+
continue-on-error: true
14+
with:
15+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
16+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
17+
command: delete startkit-preview-pr-${{ github.event.number }} --force
18+
19+
- name: Delete preview database
20+
uses: cloudflare/wrangler-action@v3
21+
continue-on-error: true
22+
with:
23+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
24+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
25+
command: d1 delete startkit-preview-pr-${{ github.event.number }} --force
26+
27+
- name: Comment PR with cleanup info
28+
uses: actions/github-script@v7
29+
with:
30+
script: |
31+
github.rest.issues.createComment({
32+
issue_number: context.issue.number,
33+
owner: context.repo.owner,
34+
repo: context.repo.repo,
35+
body: `🧹 **Preview environment cleaned up**
36+
37+
The preview deployment and database for this PR have been deleted.`
38+
})
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
name: Deploy Preview Environment
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
jobs:
8+
check:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v5
12+
- uses: ./.github/actions/check
13+
14+
deploy:
15+
needs: check
16+
runs-on: ubuntu-latest
17+
environment:
18+
name: preview
19+
url: https://startkit-preview-pr-${{ github.event.number }}.startkit-dev.workers.dev
20+
steps:
21+
- uses: actions/checkout@v5
22+
- uses: ./.github/actions/setup
23+
24+
- name: Setup preview database and run migrations
25+
id: setup-db
26+
run: |
27+
DB_NAME="startkit-preview-pr-${{ github.event.number }}"
28+
echo "db-name=$DB_NAME" >> $GITHUB_OUTPUT
29+
30+
# Create preview database and capture the database ID
31+
bunx wrangler d1 create "$DB_NAME" --output json > db-output.json
32+
DB_ID=$(cat db-output.json | jq -r '.result.uuid')
33+
echo "DB_ID=$DB_ID" >> $GITHUB_ENV
34+
35+
# Run migrations on preview database
36+
bunx wrangler d1 migrations apply "$DB_NAME" --remote
37+
env:
38+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
39+
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
40+
41+
- name: Build application
42+
run: bun run build
43+
env:
44+
BETTER_AUTH_URL: ${{ vars.BETTER_AUTH_URL || format('https://startkit-preview-pr-{0}.startkit-dev.workers.dev', github.event.number) }}
45+
BETTER_AUTH_SECRET: ${{ secrets.BETTER_AUTH_SECRET }}
46+
GITHUB_CLIENT_ID: ${{ secrets.OAUTH_GITHUB_CLIENT_ID }}
47+
GITHUB_CLIENT_SECRET: ${{ secrets.OAUTH_GITHUB_CLIENT_SECRET }}
48+
49+
- name: Create temporary wrangler config for preview
50+
run: |
51+
cat > wrangler.preview.jsonc << EOF
52+
{
53+
"name": "startkit-preview-pr-${{ github.event.number }}",
54+
"main": "@tanstack/react-start/server-entry",
55+
"compatibility_date": "2025-09-24",
56+
"compatibility_flags": ["nodejs_compat"],
57+
"observability": {
58+
"enabled": true
59+
},
60+
"vars": {
61+
"BETTER_AUTH_URL": "${{ vars.BETTER_AUTH_URL || format('https://startkit-preview-pr-{0}.startkit-dev.workers.dev', github.event.number) }}",
62+
"GITHUB_CLIENT_ID": "${{ secrets.OAUTH_GITHUB_CLIENT_ID }}"
63+
},
64+
"d1_databases": [
65+
{
66+
"binding": "DB",
67+
"database_name": "startkit-preview-pr-${{ github.event.number }}",
68+
"database_id": "${{ env.DB_ID }}"
69+
}
70+
]
71+
}
72+
EOF
73+
74+
- name: Deploy to Cloudflare Workers
75+
id: deploy
76+
uses: cloudflare/wrangler-action@v3
77+
with:
78+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
79+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
80+
command: deploy --config wrangler.preview.jsonc
81+
82+
- name: Comment PR with deployment info
83+
uses: actions/github-script@v7
84+
with:
85+
script: |
86+
github.rest.issues.createComment({
87+
issue_number: context.issue.number,
88+
owner: context.repo.owner,
89+
repo: context.repo.repo,
90+
body: `🚀 **Preview deployment is ready!**
91+
92+
**Preview URL:** ${{ steps.deploy.outputs.deployment-url }}
93+
**Database:** startkit-preview-pr-${{ github.event.number }}
94+
95+
This preview will be automatically deleted when the PR is closed.`
96+
})

0 commit comments

Comments
 (0)