/ opengrammar / .github / workflows / deploy-backend.yml
Swadhin Biswas fix(deploy): remove cached .vercel directory before pull Β· 3/28/2026
137ee96
275 lines Β· 12.4 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
name: πŸš€ Deploy Backend β€” All Platforms

on:
  push:
    branches: [main]
    paths:
      - 'opengrammar/backend/**'
      - '.github/workflows/deploy-backend.yml'
  workflow_dispatch:
    inputs:
      platform:
        description: 'Deploy to specific platform (leave empty for all)'
        required: false
        default: 'all'
        type: choice
        options:
          - all
          - cloudflare
          - vercel
          - netlify
          - deno
          - docker

concurrency:
  group: deploy-backend-${{ github.ref }}
  cancel-in-progress: false

jobs:
  # ─────────────────────────────────────────────────────────────────────────────
  # 1. Type-check before any deployment
  # ─────────────────────────────────────────────────────────────────────────────
  typecheck:
    name: βœ… Type Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: oven-sh/setup-bun@v1
        with:
          bun-version: latest
      - name: Install dependencies
        run: cd opengrammar/backend && bun install
      - name: TypeScript check
        run: cd opengrammar/backend && npx tsc --noEmit

  # ─────────────────────────────────────────────────────────────────────────────
  # 2. Cloudflare Workers
  # ─────────────────────────────────────────────────────────────────────────────
  deploy-cloudflare:
    name: ☁️ Deploy β†’ Cloudflare Workers
    runs-on: ubuntu-latest
    needs: typecheck
    if: >
      github.event_name == 'workflow_dispatch' &&
      (github.event.inputs.platform == 'all' || github.event.inputs.platform == 'cloudflare')
      || github.event_name == 'push'
    environment:
      name: cloudflare
      url: https://opengrammar-backend-production.thelabrats-dev.workers.dev
    steps:
      - uses: actions/checkout@v4
      - uses: oven-sh/setup-bun@v1
        with:
          bun-version: latest
      - name: Install dependencies
        run: cd opengrammar/backend && bun install
      - name: Deploy to Cloudflare Workers
        uses: cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          workingDirectory: opengrammar/backend
          command: deploy --env production

  # ─────────────────────────────────────────────────────────────────────────────
  # 3. Vercel
  # ─────────────────────────────────────────────────────────────────────────────
  deploy-vercel:
    name: β–³ Deploy β†’ Vercel
    runs-on: ubuntu-latest
    needs: typecheck
    if: >
      github.event_name == 'workflow_dispatch' &&
      (github.event.inputs.platform == 'all' || github.event.inputs.platform == 'vercel')
      || github.event_name == 'push'
    environment:
      name: vercel
      url: https://opengrammar-backend-psi.vercel.app
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - name: Install Vercel CLI
        run: npm install -g vercel
      - name: Deploy to Vercel
        run: |
          cd opengrammar/backend
          rm -rf .vercel
          vercel pull --yes --environment=production --token=${{ secrets.VERCEL_TOKEN }}
          vercel build --prod --token=${{ secrets.VERCEL_TOKEN }}
          vercel deploy --prebuilt --prod --token=${{ secrets.VERCEL_TOKEN }}
        env:
          VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
          VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}

  # ─────────────────────────────────────────────────────────────────────────────
  # 4. Netlify
  # ─────────────────────────────────────────────────────────────────────────────
  deploy-netlify:
    name: 🟩 Deploy β†’ Netlify
    runs-on: ubuntu-latest
    needs: typecheck
    if: >
      github.event_name == 'workflow_dispatch' &&
      (github.event.inputs.platform == 'all' || github.event.inputs.platform == 'netlify')
      || github.event_name == 'push'
    environment:
      name: netlify
      url: https://opengrammar-backend.netlify.app
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - name: Install dependencies
        run: cd opengrammar/backend && npm install
      - name: Install Netlify CLI
        run: npm install -g netlify-cli
      - name: Deploy to Netlify
        run: |
          cd opengrammar/backend
          netlify deploy --prod --dir=. --functions=netlify/functions \
            --auth=${{ secrets.NETLIFY_AUTH_TOKEN }} \
            --site=${{ secrets.NETLIFY_SITE_ID }}

  # ─────────────────────────────────────────────────────────────────────────────
  # 5. Deno Deploy
  # ─────────────────────────────────────────────────────────────────────────────
  deploy-deno:
    name: πŸ¦• Deploy β†’ Deno Deploy
    runs-on: ubuntu-latest
    needs: typecheck
    if: >
      github.event_name == 'workflow_dispatch' &&
      (github.event.inputs.platform == 'all' || github.event.inputs.platform == 'deno')
      || github.event_name == 'push'
    permissions:
      id-token: write
      contents: read
    environment:
      name: deno
      url: https://opengrammar-backend.deno.dev
    steps:
      - uses: actions/checkout@v4
      - name: Deploy to Deno Deploy
        uses: denoland/deployctl@v1
        with:
          project: opengrammar-backend
          entrypoint: opengrammar/backend/server-deno.ts
          root: .
        env:
          DENO_DEPLOY_TOKEN: ${{ secrets.DENO_DEPLOY_TOKEN }}

  # ─────────────────────────────────────────────────────────────────────────────
  # 6. Docker Hub
  # ─────────────────────────────────────────────────────────────────────────────
  deploy-docker:
    name: 🐳 Publish β†’ Docker Hub
    runs-on: ubuntu-latest
    needs: typecheck
    if: >
      github.event_name == 'workflow_dispatch' &&
      (github.event.inputs.platform == 'all' || github.event.inputs.platform == 'docker')
      || github.event_name == 'push'
    steps:
      - uses: actions/checkout@v4
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Extract metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ secrets.DOCKERHUB_USERNAME }}/opengrammar-backend
          tags: |
            type=raw,value=latest
            type=sha,prefix=sha-
            type=raw,value=2.0.0
      - name: Build and push (multi-arch)
        uses: docker/build-push-action@v5
        with:
          context: ./opengrammar/backend
          file: ./opengrammar/backend/Dockerfile
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

  # ─────────────────────────────────────────────────────────────────────────────
  # 7. npm Registry
  # ─────────────────────────────────────────────────────────────────────────────
  publish-npm:
    name: πŸ“¦ Publish β†’ npm
    runs-on: ubuntu-latest
    needs: typecheck
    if: >
      github.event_name == 'workflow_dispatch' &&
      (github.event.inputs.platform == 'all' || github.event.inputs.platform == 'docker')
      || github.event_name == 'push'
    steps:
      - uses: actions/checkout@v4
      - uses: oven-sh/setup-bun@v1
        with:
          bun-version: latest
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          registry-url: 'https://registry.npmjs.org'
      - name: Install dependencies
        run: cd opengrammar/backend && bun install
      - name: Build bundle
        run: cd opengrammar/backend && bun build server.ts --outdir dist --target node --format esm
      - name: Publish to npm
        run: |
          cd opengrammar/backend
          # Bump patch version based on commit SHA to avoid duplicate version errors
          node -e "
            const fs = require('fs');
            const pkg = JSON.parse(fs.readFileSync('package.json'));
            const sha = process.env.GITHUB_SHA?.slice(0,7) || 'dev';
            pkg.version = pkg.version.replace(/\\.\\d+$/, '') + '.' + Date.now().toString().slice(-6);
            fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2));
            console.log('Publishing version:', pkg.version);
          "
          npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

  # ─────────────────────────────────────────────────────────────────────────────
  # 8. Summary
  # ─────────────────────────────────────────────────────────────────────────────
  summary:
    name: πŸ“‹ Deployment Summary
    runs-on: ubuntu-latest
    needs:
      - deploy-cloudflare
      - deploy-vercel
      - deploy-netlify
      - deploy-deno
      - deploy-docker
      - publish-npm
    if: always()
    steps:
      - name: Generate summary
        run: |
          echo "## πŸš€ Deployment Results" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "| Platform | Status |" >> $GITHUB_STEP_SUMMARY
          echo "|---|---|" >> $GITHUB_STEP_SUMMARY
          echo "| ☁️ Cloudflare Workers | ${{ needs.deploy-cloudflare.result == 'success' && 'βœ… Deployed' || '❌ Failed' }} |" >> $GITHUB_STEP_SUMMARY
          echo "| β–³ Vercel | ${{ needs.deploy-vercel.result == 'success' && 'βœ… Deployed' || '❌ Failed' }} |" >> $GITHUB_STEP_SUMMARY
          echo "| 🟩 Netlify | ${{ needs.deploy-netlify.result == 'success' && 'βœ… Deployed' || '❌ Failed' }} |" >> $GITHUB_STEP_SUMMARY
          echo "| πŸ¦• Deno Deploy | ${{ needs.deploy-deno.result == 'success' && 'βœ… Deployed' || '❌ Failed' }} |" >> $GITHUB_STEP_SUMMARY
          echo "| 🐳 Docker Hub | ${{ needs.deploy-docker.result == 'success' && 'βœ… Published' || '❌ Failed' }} |" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "Commit: \`${{ github.sha }}\`" >> $GITHUB_STEP_SUMMARY