Skip to content

Commit aae0e44

Browse files
Add GitHub Actions workflows for preview deployments and production deploy
- Add preview.yml: creates a Worker Preview on every PR using wrangler preview (prerelease from cloudflare/workers-sdk#12983), posts/updates a PR comment with the preview URL - Add deploy.yml: runs npm run deploy on pushes to main - Update wrangler to prerelease version from pkg.pr.new/wrangler@12983 - Replace deploy:preview script to call wrangler preview after build - Remove vite preview script (superseded by wrangler preview)
1 parent 9e0dbfa commit aae0e44

4 files changed

Lines changed: 333 additions & 749 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Deploys the Worker to production on pushes to main
2+
#
3+
# Required secret: CLOUDFLARE_API_TOKEN
4+
# Add it at: Settings → Secrets and variables → Actions → New repository secret
5+
6+
name: Deploy
7+
8+
on:
9+
push:
10+
branches: [main]
11+
12+
permissions:
13+
contents: read
14+
15+
jobs:
16+
deploy:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v4
20+
21+
- uses: actions/setup-node@v4
22+
with:
23+
node-version: lts/*
24+
cache: npm
25+
26+
- name: Install dependencies
27+
run: npm ci
28+
29+
- name: Deploy
30+
env:
31+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
32+
run: npm run deploy

.github/workflows/preview.yml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Creates a Worker Preview deployment on pull requests
2+
#
3+
# Required secret: CLOUDFLARE_API_TOKEN
4+
# Add it at: Settings → Secrets and variables → Actions → New repository secret
5+
6+
name: Preview
7+
8+
on:
9+
pull_request:
10+
types: [opened, synchronize, reopened]
11+
12+
permissions:
13+
contents: read
14+
pull-requests: write
15+
16+
jobs:
17+
preview:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v4
21+
22+
- uses: actions/setup-node@v4
23+
with:
24+
node-version: lts/*
25+
cache: npm
26+
27+
- name: Install dependencies
28+
run: npm ci
29+
30+
- name: Deploy preview
31+
id: preview
32+
env:
33+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
34+
run: |
35+
output=$(npm run deploy:preview 2>&1)
36+
echo "$output"
37+
# Extract the preview URL from wrangler output (https://*.workers.dev or similar)
38+
url=$(echo "$output" | grep -oP 'https://[^\s"]+\.workers\.dev[^\s"]*' | head -1)
39+
if [ -n "$url" ]; then
40+
echo "url=$url" >> "$GITHUB_OUTPUT"
41+
fi
42+
echo "output<<EOF" >> "$GITHUB_OUTPUT"
43+
echo "$output" >> "$GITHUB_OUTPUT"
44+
echo "EOF" >> "$GITHUB_OUTPUT"
45+
46+
- name: Post preview comment
47+
uses: actions/github-script@v7
48+
with:
49+
script: |
50+
const url = `${{ steps.preview.outputs.url }}`;
51+
const rawOutput = `${{ steps.preview.outputs.output }}`;
52+
53+
const body = url
54+
? `### Preview deployment\n\n${url}`
55+
: `### Preview deployment\n\n<details><summary>wrangler output</summary>\n\n\`\`\`\n${rawOutput}\n\`\`\`\n\n</details>`;
56+
57+
const { data: comments } = await github.rest.issues.listComments({
58+
owner: context.repo.owner,
59+
repo: context.repo.repo,
60+
issue_number: context.issue.number,
61+
});
62+
63+
const existing = comments.find(c =>
64+
c.user.login === 'github-actions[bot]' &&
65+
c.body.startsWith('### Preview deployment')
66+
);
67+
68+
if (existing) {
69+
await github.rest.issues.updateComment({
70+
owner: context.repo.owner,
71+
repo: context.repo.repo,
72+
comment_id: existing.id,
73+
body,
74+
});
75+
} else {
76+
await github.rest.issues.createComment({
77+
owner: context.repo.owner,
78+
repo: context.repo.repo,
79+
issue_number: context.issue.number,
80+
body,
81+
});
82+
}

0 commit comments

Comments
 (0)