Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

main.yml

261 lines7.5 KB

1name: CI/CD
2​
3on:
4 pull_request:
5 branches: [main]
6 push:
7 branches: [main]
8 workflow_dispatch:
9​
10permissions:
11 pull-requests: write
12 contents: read
13​
14jobs:
15 ci:
16 name: CI
17 runs-on: ubuntu-latest
18 steps:
19 - name: Checkout
20 uses: actions/checkout@v4
21​
22 - name: Setup Node.js
23 uses: actions/setup-node@v4
24 with:
25 node-version: "20"
26​
27 - name: Setup pnpm
28 uses: pnpm/action-setup@v4
29 with:
30 version: latest
31​
32 - name: Get pnpm store directory
33 shell: bash
34 run: |
35 echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
36​
37 - name: Setup pnpm cache
38 uses: actions/cache@v4
39 with:
40 path: ${{ env.STORE_PATH }}
41 key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
42 restore-keys: |
43 ${{ runner.os }}-pnpm-store-
44​
45 - name: Install dependencies
46 run: pnpm install --frozen-lockfile
47​
48 - name: Check
49 run: pnpm check
50​
51 - name: Type check
52 run: pnpm typecheck
53​
54 - name: Knip
55 run: pnpm knip
56​
57 - name: Test
58 run: pnpm test
59​
60 deploy-preview:
61 name: Deploy Preview
62 if: github.event_name == 'pull_request'
63 runs-on: ubuntu-latest
64 needs: ci
65 environment:
66 name: Preview
67 url: ${{ steps.deploy.outputs.deployment-url }}
68 steps:
69 - name: Find Existing Comment
70 id: find-comment
71 uses: actions/github-script@v7
72 with:
73 script: |
74 const comments = await github.rest.issues.listComments({
75 issue_number: context.issue.number,
76 owner: context.repo.owner,
77 repo: context.repo.repo,
78 });
79​
80 const botComment = comments.data.find(comment =>
81 comment.user.type === 'Bot' &&
82 comment.body.includes('🚀 Preview Deployment')
83 );
84​
85 if (botComment) {
86 console.log(`Found existing comment: ${botComment.id}`);
87 return botComment.id;
88 }
89 return null;
90​
91 - name: Create/Update Loading Comment
92 uses: actions/github-script@v7
93 with:
94 script: |
95 const commentId = '${{ steps.find-comment.outputs.result }}';
96 const commentBody = `## 🚀 Preview Deployment
97​
98 **Status:** ⏳ Loading...
99​
100 **Commit:** \`${{ github.event.pull_request.head.sha }}\`
101​
102 _Deployment in progress..._`;
103​
104 if (commentId && commentId !== 'null') {
105 await github.rest.issues.updateComment({
106 comment_id: commentId,
107 owner: context.repo.owner,
108 repo: context.repo.repo,
109 body: commentBody
110 });
111 console.log(`Updated existing comment to loading: ${commentId}`);
112 } else {
113 const response = await github.rest.issues.createComment({
114 issue_number: context.issue.number,
115 owner: context.repo.owner,
116 repo: context.repo.repo,
117 body: commentBody
118 });
119 console.log(`Created loading comment: ${response.data.id}`);
120 }
121​
122 - name: Checkout
123 uses: actions/checkout@v4
124​
125 - name: Setup Node.js
126 uses: actions/setup-node@v4
127 with:
128 node-version: "20"
129​
130 - name: Setup pnpm
131 uses: pnpm/action-setup@v4
132 with:
133 version: latest
134​
135 - name: Get pnpm store directory
136 shell: bash
137 run: |
138 echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
139​
140 - name: Setup pnpm cache
141 uses: actions/cache@v4
142 with:
143 path: ${{ env.STORE_PATH }}
144 key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
145 restore-keys: |
146 ${{ runner.os }}-pnpm-store-
147​
148 - name: Install dependencies
149 run: pnpm install --frozen-lockfile
150​
151 - name: Build
152 run: pnpm build
153​
154 - name: Deploy Preview
155 id: deploy
156 uses: cloudflare/wrangler-action@v3
157 with:
158 apiToken: ${{ secrets.CF_API_TOKEN }}
159 accountId: ${{ secrets.CF_ACCOUNT_ID }}
160 command: versions upload
161​
162 - name: Update Comment with Success
163 uses: actions/github-script@v7
164 with:
165 script: |
166 const deploymentUrl = '${{ steps.deploy.outputs.deployment-url }}';
167 const commentId = '${{ steps.find-comment.outputs.result }}';
168​
169 const commentBody = `## 🚀 Preview Deployment
170​
171 **Status:** ✅ Ready!
172​
173 **Preview URL:** [Open Preview](${deploymentUrl})
174​
175 **Commit:** \`${{ github.event.pull_request.head.sha }}\`
176​
177 _Built and deployed successfully_`;
178​
179 if (commentId && commentId !== 'null') {
180 await github.rest.issues.updateComment({
181 comment_id: commentId,
182 owner: context.repo.owner,
183 repo: context.repo.repo,
184 body: commentBody
185 });
186 console.log(`Updated existing comment to success: ${commentId}`);
187 } else {
188 // Find the loading comment we just created
189 const comments = await github.rest.issues.listComments({
190 issue_number: context.issue.number,
191 owner: context.repo.owner,
192 repo: context.repo.repo,
193 });
195 const loadingComment = comments.data.find(comment =>
196 comment.user.type === 'Bot' &&
197 comment.body.includes('⏳ Loading...')
198 );
200 if (loadingComment) {
201 await github.rest.issues.updateComment({
202 comment_id: loadingComment.id,
203 owner: context.repo.owner,
204 repo: context.repo.repo,
205 body: commentBody
206 });
207 console.log(`Updated loading comment to success: ${loadingComment.id}`);
208 } else {
209 await github.rest.issues.createComment({
210 issue_number: context.issue.number,
211 owner: context.repo.owner,
212 repo: context.repo.repo,
213 body: commentBody
214 });
215 console.log('Created new success comment');
216 }
217 }
218​
219 deploy-production:
220 name: Deploy Production
221 if: github.ref == 'refs/heads/main' && github.event_name == 'push'
222 runs-on: ubuntu-latest
223 needs: ci
224 environment:
225 name: Production
226 url: https://trytanstackdb.com
227 steps:
228 - name: Checkout
229 uses: actions/checkout@v4
230​
231 - name: Setup Node.js
232 uses: actions/setup-node@v4
233 with:
234 node-version: "20"
235​
236 - name: Setup pnpm
237 uses: pnpm/action-setup@v4
238 with:
239 version: latest
240​
241 - name: Get pnpm store directory
242 shell: bash
243 run: |
244 echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
245​
246 - name: Setup pnpm cache
247 uses: actions/cache@v4
248 with:
249 path: ${{ env.STORE_PATH }}
250 key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
251 restore-keys: |
252 ${{ runner.os }}-pnpm-store-
253​
254 - name: Install dependencies
255 run: pnpm install --frozen-lockfile
256​
257 - name: Deploy Production
258 run: pnpm deploy.prod
259 env:
260 CF_API_TOKEN: ${{ secrets.CF_API_TOKEN }}
261 CF_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}