Skip to content

Commit 32dee2b

Browse files
committed
Split preview deployment logic
1 parent 6af10f3 commit 32dee2b

File tree

9 files changed

+286
-199
lines changed

9 files changed

+286
-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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
continue-on-error: true
11+
steps:
12+
- uses: actions/checkout@v5
13+
- uses: ./.github/actions/setup
14+
15+
- name: Cleanup preview environment
16+
id: cleanup
17+
run: ./scripts/cleanup-preview.sh ${{ github.event.number }}
18+
continue-on-error: true
19+
env:
20+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
21+
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
22+
23+
- name: Comment PR with cleanup info
24+
uses: actions/github-script@v7
25+
with:
26+
script: |
27+
github.rest.issues.createComment({
28+
issue_number: context.issue.number,
29+
owner: context.repo.owner,
30+
repo: context.repo.repo,
31+
body: `🧹 **Preview environment cleaned up**
32+
33+
The preview deployment and database for this PR have been deleted.`
34+
})
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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: ./scripts/setup-preview-db.sh ${{ github.event.number }}
27+
env:
28+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
29+
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
30+
31+
- name: Build application
32+
run: bun run build
33+
env:
34+
BETTER_AUTH_URL: ${{ vars.BETTER_AUTH_URL || format('https://startkit-preview-pr-{0}.startkit-dev.workers.dev', github.event.number) }}
35+
BETTER_AUTH_SECRET: ${{ secrets.BETTER_AUTH_SECRET }}
36+
GITHUB_CLIENT_ID: ${{ secrets.OAUTH_GITHUB_CLIENT_ID }}
37+
GITHUB_CLIENT_SECRET: ${{ secrets.OAUTH_GITHUB_CLIENT_SECRET }}
38+
39+
- name: Create temporary wrangler config for preview
40+
run: |
41+
cat > wrangler.preview.jsonc << EOF
42+
{
43+
"name": "startkit-preview-pr-${{ github.event.number }}",
44+
"main": "@tanstack/react-start/server-entry",
45+
"compatibility_date": "2025-09-24",
46+
"compatibility_flags": ["nodejs_compat"],
47+
"observability": {
48+
"enabled": true
49+
},
50+
"vars": {
51+
"BETTER_AUTH_URL": "${{ vars.BETTER_AUTH_URL || format('https://startkit-preview-pr-{0}.startkit-dev.workers.dev', github.event.number) }}",
52+
"GITHUB_CLIENT_ID": "${{ secrets.OAUTH_GITHUB_CLIENT_ID }}"
53+
},
54+
"d1_databases": [
55+
{
56+
"binding": "DB",
57+
"database_name": "startkit-preview-pr-${{ github.event.number }}",
58+
"database_id": "${{ env.DB_ID }}"
59+
}
60+
]
61+
}
62+
EOF
63+
64+
- name: Deploy to Cloudflare Workers
65+
id: deploy
66+
uses: cloudflare/wrangler-action@v3
67+
with:
68+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
69+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
70+
command: deploy --config wrangler.preview.jsonc
71+
72+
- name: Comment PR with deployment info
73+
uses: actions/github-script@v7
74+
with:
75+
script: |
76+
github.rest.issues.createComment({
77+
issue_number: context.issue.number,
78+
owner: context.repo.owner,
79+
repo: context.repo.repo,
80+
body: `🚀 **Preview deployment is ready!**
81+
82+
**Preview URL:** ${{ steps.deploy.outputs.deployment-url }}
83+
**Database:** startkit-preview-pr-${{ github.event.number }}
84+
85+
This preview will be automatically deleted when the PR is closed.`
86+
})

0 commit comments

Comments
 (0)