Skip to content

Commit 35d8496

Browse files
ericyangpanclaude
andcommitted
config(wrangler): restructure environment-specific routing
- Move aicodingstack.io route to env.production section - Add env.staging section with staging.aicodingstack.io route - Properly organize environment-specific configurations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 7fa4933 commit 35d8496

File tree

5 files changed

+222
-98
lines changed

5 files changed

+222
-98
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Cleanup Preview
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
7+
jobs:
8+
cleanup:
9+
name: Delete Preview Deployment
10+
runs-on: ubuntu-latest
11+
permissions:
12+
pull-requests: write
13+
14+
steps:
15+
- name: Delete Preview Deployment
16+
uses: cloudflare/wrangler-action@v3
17+
with:
18+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
19+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
20+
command: delete --name aicodingstack-pr-${{ github.event.pull_request.number }} --force
21+
continue-on-error: true
22+
23+
- name: Comment Cleanup Status
24+
uses: actions/github-script@v7
25+
with:
26+
script: |
27+
const prNumber = context.payload.pull_request.number;
28+
29+
await github.rest.issues.createComment({
30+
owner: context.repo.owner,
31+
repo: context.repo.repo,
32+
issue_number: prNumber,
33+
body: `### Preview deployment cleaned up\n\nThe preview deployment for PR #${prNumber} has been removed.`
34+
});

.github/workflows/deploy-preview.yml

Lines changed: 97 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,52 @@ name: Deploy Preview
22

33
on:
44
pull_request:
5-
branches: [main]
5+
push:
6+
branches-ignore: [main, develop]
67

78
concurrency:
8-
group: preview-${{ github.ref }}
9+
group: preview-${{ github.event.pull_request.number || github.ref_name }}
910
cancel-in-progress: true
1011

1112
jobs:
12-
deploy-preview:
13-
name: Deploy Preview to Cloudflare Pages
13+
deploy:
14+
name: Deploy Preview to Cloudflare Workers
1415
runs-on: ubuntu-latest
1516
permissions:
1617
contents: read
1718
pull-requests: write
19+
deployments: write
20+
1821
steps:
1922
- name: Checkout code
2023
uses: actions/checkout@v6
2124

25+
- name: Compute preview identifiers
26+
id: preview_meta
27+
run: |
28+
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
29+
CF_BRANCH="pr-${{ github.event.pull_request.number }}"
30+
CONTEXT_LABEL="PR #${{ github.event.pull_request.number }}"
31+
else
32+
RAW_BRANCH="${{ github.ref_name }}"
33+
SLUG="$(echo "$RAW_BRANCH" | tr '[:upper:]' '[:lower:]' | sed -E 's#[^a-z0-9]+#-#g; s#(^-+|-+$)##g' | cut -c1-40)"
34+
if [[ -z "$SLUG" ]]; then
35+
SLUG="branch"
36+
fi
37+
CF_BRANCH="br-$SLUG"
38+
CONTEXT_LABEL="Branch $RAW_BRANCH"
39+
fi
40+
41+
WORKER_NAME="aicodingstack-$CF_BRANCH"
42+
PREVIEW_URL="https://${WORKER_NAME}.pr-preview.workers.dev"
43+
44+
{
45+
echo "cf_branch=$CF_BRANCH"
46+
echo "worker_name=$WORKER_NAME"
47+
echo "preview_url=$PREVIEW_URL"
48+
echo "context_label=$CONTEXT_LABEL"
49+
} >> "$GITHUB_OUTPUT"
50+
2251
- name: Setup Node.js
2352
uses: actions/setup-node@v6
2453
with:
@@ -28,61 +57,72 @@ jobs:
2857
- name: Install dependencies
2958
run: npm ci
3059

31-
- name: Run validation tests
32-
run: npm run test:validate
33-
34-
- name: Generate manifests and metadata
35-
run: |
36-
npm run generate:manifests
37-
npm run generate:metadata
60+
- name: Run CI tests
61+
run: npm run test:ci
3862

3963
- name: Build with OpenNext
4064
run: npm run build
4165
env:
42-
BUILD_TIME: ${{ github.event.pull_request.updated_at }}
43-
44-
# TODO: Configure Cloudflare Pages deployment
45-
# You'll need to add these secrets to your repository:
46-
# - CLOUDFLARE_API_TOKEN
47-
# - CLOUDFLARE_ACCOUNT_ID
48-
#
49-
# Uncomment the steps below after adding the secrets:
50-
#
51-
# - name: Deploy to Cloudflare Pages (Preview)
52-
# id: deploy
53-
# uses: cloudflare/wrangler-action@v3
54-
# with:
55-
# apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
56-
# accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
57-
# command: pages deploy .open-next --project-name=aicodingstack --branch=preview-${{ github.event.pull_request.number }}
58-
#
59-
# - name: Comment preview URL
60-
# uses: actions/github-script@v7
61-
# with:
62-
# script: |
63-
# const previewUrl = '${{ steps.deploy.outputs.deployment-url }}';
64-
# const comment = `## 🚀 Preview Deployment
65-
#
66-
# Preview is ready!
67-
#
68-
# **Preview URL:** ${previewUrl}
69-
#
70-
# ### Build Details
71-
# - **Commit:** ${context.sha.substring(0, 7)}
72-
# - **Build Time:** ${{ github.event.pull_request.updated_at }}
73-
#
74-
# The preview will be automatically deleted when this PR is merged or closed.`;
75-
#
76-
# github.rest.issues.createComment({
77-
# issue_number: context.issue.number,
78-
# owner: context.repo.owner,
79-
# repo: context.repo.repo,
80-
# body: comment
81-
# });
82-
83-
- name: Preview deployment placeholder
66+
BUILD_TIME: ${{ github.event.pull_request.updated_at || github.event.head_commit.timestamp }}
67+
68+
- name: Deploy Preview
69+
uses: cloudflare/wrangler-action@v3
70+
with:
71+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
72+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
73+
command: deploy --branch ${{ steps.preview_meta.outputs.cf_branch }}
74+
75+
- name: Comment Preview URL
76+
if: github.event_name == 'pull_request'
77+
uses: actions/github-script@v7
78+
env:
79+
PREVIEW_URL: ${{ steps.preview_meta.outputs.preview_url }}
80+
with:
81+
script: |
82+
const prNumber = context.payload.pull_request.number;
83+
const previewUrl = process.env.PREVIEW_URL;
84+
const commitSha = context.sha.substring(0, 7);
85+
86+
// Find existing bot comment
87+
const { data: comments } = await github.rest.issues.listComments({
88+
owner: context.repo.owner,
89+
repo: context.repo.repo,
90+
issue_number: prNumber,
91+
});
92+
93+
const botComment = comments.find(c =>
94+
c.user.type === 'Bot' && c.body.includes('Preview deployment')
95+
);
96+
97+
const body = `### Preview deployment
98+
99+
| Status | URL |
100+
|--------|-----|
101+
| Ready | [${previewUrl}](${previewUrl}) |
102+
103+
**Commit:** \`${commitSha}\`
104+
**Updated:** ${new Date().toISOString()}`;
105+
106+
if (botComment) {
107+
await github.rest.issues.updateComment({
108+
owner: context.repo.owner,
109+
repo: context.repo.repo,
110+
comment_id: botComment.id,
111+
body
112+
});
113+
} else {
114+
await github.rest.issues.createComment({
115+
owner: context.repo.owner,
116+
repo: context.repo.repo,
117+
issue_number: prNumber,
118+
body
119+
});
120+
}
121+
122+
- name: Deployment summary
84123
run: |
85-
echo "⚠️ Preview deployment not yet configured"
86-
echo "To enable preview deployments:"
87-
echo "1. Add CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID to repository secrets"
88-
echo "2. Uncomment the deployment steps in .github/workflows/deploy-preview.yml"
124+
echo "### Preview Deployment" >> $GITHUB_STEP_SUMMARY
125+
echo "" >> $GITHUB_STEP_SUMMARY
126+
echo "- **Context:** ${{ steps.preview_meta.outputs.context_label }}" >> $GITHUB_STEP_SUMMARY
127+
echo "- **URL:** ${{ steps.preview_meta.outputs.preview_url }}" >> $GITHUB_STEP_SUMMARY
128+
echo "- **Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY

.github/workflows/deploy-production.yml

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@ on:
55
branches: [main]
66

77
concurrency:
8-
group: production
8+
group: production-deploy
99
cancel-in-progress: false
1010

1111
jobs:
1212
deploy-production:
13-
name: Deploy to Cloudflare Pages
13+
name: Deploy to Cloudflare Workers
1414
runs-on: ubuntu-latest
15+
permissions:
16+
contents: read
17+
deployments: write
1518
environment:
1619
name: production
1720
url: https://aicodingstack.io
21+
1822
steps:
1923
- name: Checkout code
2024
uses: actions/checkout@v6
@@ -28,45 +32,25 @@ jobs:
2832
- name: Install dependencies
2933
run: npm ci
3034

31-
- name: Run validation tests
32-
run: npm run test:validate
33-
34-
- name: Generate manifests and metadata
35-
run: |
36-
npm run generate:manifests
37-
npm run generate:metadata
35+
- name: Run CI tests
36+
run: npm run test:ci
3837

3938
- name: Build with OpenNext
4039
run: npm run build
4140
env:
4241
BUILD_TIME: ${{ github.event.head_commit.timestamp }}
4342

44-
# TODO: Configure Cloudflare Pages deployment
45-
# You'll need to add these secrets to your repository:
46-
# - CLOUDFLARE_API_TOKEN
47-
# - CLOUDFLARE_ACCOUNT_ID
48-
#
49-
# Uncomment the steps below after adding the secrets:
50-
#
51-
# - name: Deploy to Cloudflare Pages (Production)
52-
# uses: cloudflare/wrangler-action@v3
53-
# with:
54-
# apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
55-
# accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
56-
# command: pages deploy .open-next --project-name=aicodingstack --branch=main
57-
#
58-
# - name: Notify deployment success
59-
# run: |
60-
# echo "✅ Production deployment successful!"
61-
# echo "🌐 Site: https://aicodingstack.io"
62-
# echo "📝 Commit: ${{ github.sha }}"
43+
- name: Deploy to Production
44+
uses: cloudflare/wrangler-action@v3
45+
with:
46+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
47+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
48+
command: deploy --env production
6349

64-
- name: Production deployment placeholder
50+
- name: Deployment summary
6551
run: |
66-
echo "⚠️ Production deployment not yet configured"
67-
echo "To enable production deployments:"
68-
echo "1. Add CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID to repository secrets"
69-
echo "2. Uncomment the deployment steps in .github/workflows/deploy-production.yml"
70-
echo ""
71-
echo "Manual deployment command:"
72-
echo "npm run deploy"
52+
echo "### Production Deployment" >> $GITHUB_STEP_SUMMARY
53+
echo "" >> $GITHUB_STEP_SUMMARY
54+
echo "- **URL:** https://aicodingstack.io" >> $GITHUB_STEP_SUMMARY
55+
echo "- **Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY
56+
echo "- **Time:** $(date -u '+%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_STEP_SUMMARY
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Deploy Staging
2+
3+
on:
4+
push:
5+
branches: [develop]
6+
7+
concurrency:
8+
group: staging-deploy
9+
cancel-in-progress: true
10+
11+
jobs:
12+
deploy-staging:
13+
name: Deploy to Cloudflare Workers (Staging)
14+
runs-on: ubuntu-latest
15+
permissions:
16+
contents: read
17+
deployments: write
18+
environment:
19+
name: staging
20+
url: https://staging.aicodingstack.io
21+
22+
steps:
23+
- name: Checkout code
24+
uses: actions/checkout@v6
25+
26+
- name: Setup Node.js
27+
uses: actions/setup-node@v6
28+
with:
29+
node-version: '22'
30+
cache: 'npm'
31+
32+
- name: Install dependencies
33+
run: npm ci
34+
35+
- name: Run CI tests
36+
run: npm run test:ci
37+
38+
- name: Build with OpenNext
39+
run: npm run build
40+
env:
41+
BUILD_TIME: ${{ github.event.head_commit.timestamp }}
42+
43+
- name: Deploy to Staging
44+
uses: cloudflare/wrangler-action@v3
45+
with:
46+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
47+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
48+
command: deploy --env staging
49+
50+
- name: Deployment summary
51+
run: |
52+
echo "### Staging Deployment" >> $GITHUB_STEP_SUMMARY
53+
echo "" >> $GITHUB_STEP_SUMMARY
54+
echo "- **URL:** https://staging.aicodingstack.io" >> $GITHUB_STEP_SUMMARY
55+
echo "- **Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY
56+
echo "- **Time:** $(date -u '+%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_STEP_SUMMARY

wrangler.toml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ compatibility_flags = [
66
"global_fetch_strictly_public",
77
]
88

9-
[[routes]]
10-
pattern = "aicodingstack.io"
11-
custom_domain = true
12-
139
[assets]
1410
binding = "ASSETS"
1511
directory = ".open-next/assets"
@@ -24,4 +20,18 @@ head_sampling_rate = 0.1
2420
mode = "smart"
2521

2622
[limits]
27-
cpu_ms = 50
23+
cpu_ms = 50
24+
25+
# ============ Production ============
26+
[env.production]
27+
name = "aicodingstack"
28+
[[env.production.routes]]
29+
pattern = "aicodingstack.io"
30+
custom_domain = true
31+
32+
# ============ Staging ============
33+
[env.staging]
34+
name = "aicodingstack-staging"
35+
[[env.staging.routes]]
36+
pattern = "staging.aicodingstack.io"
37+
custom_domain = true

0 commit comments

Comments
 (0)