-
Notifications
You must be signed in to change notification settings - Fork 0
138 lines (116 loc) · 4.44 KB
/
preview.yml
File metadata and controls
138 lines (116 loc) · 4.44 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
name: Preview Environment
on:
issue_comment:
types: [created]
workflow_dispatch:
inputs:
pr_number:
description: "PR number to deploy"
required: true
type: number
permissions:
contents: read
pull-requests: write
concurrency:
group: preview-${{ github.event.issue.number || inputs.pr_number }}
cancel-in-progress: true
jobs:
deploy-preview:
runs-on: ubuntu-latest
if: |
(github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
contains(github.event.comment.body, '/deploy-preview')) ||
github.event_name == 'workflow_dispatch'
env:
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_DATABASE_ID: ${{ secrets.CLOUDFLARE_DATABASE_ID }}
PR_NUMBER: ${{ github.event_name == 'workflow_dispatch' && inputs.pr_number || github.event.issue.number }}
steps:
- name: Add reaction to comment
if: github.event_name == 'issue_comment'
uses: actions/github-script@v7
with:
script: |
github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: 'rocket'
});
- name: Get PR branch
id: pr-branch
uses: actions/github-script@v7
with:
script: |
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: process.env.PR_NUMBER
});
core.setOutput('ref', pr.data.head.ref);
core.setOutput('sha', pr.data.head.sha);
- name: Checkout PR branch
uses: actions/checkout@v4
with:
ref: ${{ steps.pr-branch.outputs.sha }}
- name: Setup PNPM
uses: pnpm/action-setup@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 22
cache: pnpm
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Generate wrangler.toml from example
run: |
cp wrangler.toml.example wrangler.toml
sed -i "s/PLACEHOLDER_DATABASE_ID/${CLOUDFLARE_DATABASE_ID}/g" wrangler.toml
- name: Generate API wrangler.toml from example
working-directory: apps/api
run: |
cp wrangler.toml.example wrangler.toml
sed -i "s/PLACEHOLDER_DATABASE_ID/${CLOUDFLARE_DATABASE_ID}/g" wrangler.toml
- name: Deploy API to preview environment
working-directory: apps/api
run: |
WORKER_NAME="tabitabi-api-preview-pr-${PR_NUMBER}"
pnpm wrangler deploy --name ${WORKER_NAME} --env preview
- name: Get API preview URL
id: api-url
run: |
echo "url=https://tabitabi-api-preview-pr-${PR_NUMBER}.oranda.workers.dev/api/v1" >> $GITHUB_OUTPUT
- name: Build web with preview API URL
run: pnpm run build
env:
VITE_API_URL: ${{ steps.api-url.outputs.url }}
- name: Deploy Web to Pages preview
id: pages-deploy
working-directory: apps/web
run: |
BRANCH_NAME="preview-pr-${PR_NUMBER}"
OUTPUT=$(pnpm wrangler pages deploy .svelte-kit/cloudflare --project-name=tabitabi --branch=${BRANCH_NAME})
echo "$OUTPUT"
URL=$(echo "$OUTPUT" | grep -oP 'https://[a-z0-9-]+\.tabitabi\.pages\.dev' | head -1)
echo "url=${URL}" >> $GITHUB_OUTPUT
- name: Comment PR with preview URLs
uses: actions/github-script@v7
with:
script: |
const apiUrl = '${{ steps.api-url.outputs.url }}';
const webUrl = '${{ steps.pages-deploy.outputs.url }}';
const comment = `## 🚀 Preview Environment Ready
Your preview environment has been deployed:
- **Web App**: ${webUrl}
- **API**: ${apiUrl}
The Web app is configured to access the preview API automatically.
To update this preview, comment \`/deploy-preview\` again.
> **Note**: Preview environments share the same database as production.`;
github.rest.issues.createComment({
issue_number: process.env.PR_NUMBER,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});