mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
640e82bf71
Bumps [pnpm/action-setup](https://github.com/pnpm/action-setup) from 4 to 6. - [Release notes](https://github.com/pnpm/action-setup/releases) - [Commits](https://github.com/pnpm/action-setup/compare/v4...v6) --- updated-dependencies: - dependency-name: pnpm/action-setup dependency-version: '6' dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
136 lines
5.5 KiB
YAML
136 lines
5.5 KiB
YAML
name: Deploy API Worker
|
|
|
|
# API worker (Hono + D1 + R2) in worker/ — NOT part of the pnpm workspace, so it installs
|
|
# on its own with --ignore-workspace against worker/pnpm-lock.yaml.
|
|
# push main -> dev (api.dev.tokencircles.com)
|
|
# tag v* -> prod (api.tokencircles.com)
|
|
# workflow_dispatch -> choose dev | prod
|
|
#
|
|
# Applies D1 migrations before each deploy (wrangler records applied ones in d1_migrations,
|
|
# so re-runs only apply new files). One-time per env, NOT done here:
|
|
# pnpm run d1:create:{dev,prod} (paste the printed database_id into worker/wrangler.jsonc)
|
|
# pnpm run r2:create:{dev,prod}
|
|
# pnpm run secret:{dev,prod} (JWT_SECRET + GOOGLE_CLIENT_SECRET + TURNSTILE_SECRET)
|
|
# TURNSTILE_SECRET must be the secret belonging to the SAME widget as the site key that
|
|
# environment's frontend build uses (frontend/.env.dev / .env.production). Changing one
|
|
# without the other passes the widget client-side and 403s every sign-in server-side.
|
|
# Requires repo secrets: CLOUDFLARE_API_TOKEN + CLOUDFLARE_ACCOUNT_ID.
|
|
|
|
on:
|
|
push:
|
|
branches: [main]
|
|
tags: ['v*']
|
|
workflow_dispatch:
|
|
inputs:
|
|
environment:
|
|
description: 'Environment to deploy to'
|
|
required: true
|
|
default: 'dev'
|
|
type: choice
|
|
options: [dev, prod]
|
|
|
|
permissions:
|
|
contents: read
|
|
|
|
jobs:
|
|
deploy-worker:
|
|
name: Deploy API Worker
|
|
runs-on: ubuntu-latest
|
|
env:
|
|
DEPLOY_ENABLED: ${{ secrets.CLOUDFLARE_API_TOKEN != '' }}
|
|
steps:
|
|
- uses: actions/checkout@v7
|
|
|
|
- name: Resolve environment
|
|
id: env
|
|
run: |
|
|
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
|
ENV="${{ github.event.inputs.environment }}"
|
|
elif [[ "${{ github.ref }}" == refs/tags/v* ]]; then
|
|
ENV="prod"
|
|
else
|
|
ENV="dev"
|
|
fi
|
|
echo "env=$ENV" >> "$GITHUB_OUTPUT"
|
|
if [ "$ENV" = "prod" ]; then
|
|
echo "db=finance-manager" >> "$GITHUB_OUTPUT"
|
|
else
|
|
echo "db=finance-manager-dev" >> "$GITHUB_OUTPUT"
|
|
fi
|
|
echo "Deploying API worker to: $ENV"
|
|
|
|
- name: Setup pnpm
|
|
uses: pnpm/action-setup@v6
|
|
with:
|
|
version: 10
|
|
|
|
- name: Setup Node.js
|
|
uses: actions/setup-node@v7
|
|
with:
|
|
node-version: '22'
|
|
cache: 'pnpm'
|
|
|
|
- name: Install worker deps (outside the pnpm workspace)
|
|
working-directory: worker
|
|
# --frozen-lockfile, which the header above always claimed this did. It could not: the
|
|
# lockfile was gitignored, so this ran --no-frozen-lockfile and every deploy — including
|
|
# every PROD deploy — built with whatever npm served that morning. A deploy that cannot be
|
|
# reproduced cannot be rolled back to a known-good toolchain either. The lockfile is now
|
|
# committed and pins integrity hashes, so a republished or tampered tarball is refused
|
|
# rather than installed.
|
|
run: pnpm install --frozen-lockfile --ignore-workspace
|
|
|
|
- name: Typecheck
|
|
working-directory: worker
|
|
run: pnpm run typecheck
|
|
|
|
# Migrations can be destructive (e.g. 0002 recreates the profiles table). Snapshot the
|
|
# remote D1 to a SQL dump and keep it as an artifact before applying, so a bad migration
|
|
# is recoverable.
|
|
- name: Back up remote D1 before migrations (${{ steps.env.outputs.env }})
|
|
if: env.DEPLOY_ENABLED == 'true'
|
|
uses: cloudflare/wrangler-action@v4
|
|
with:
|
|
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
|
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
|
workingDirectory: worker
|
|
command: d1 export ${{ steps.env.outputs.db }} --remote --env ${{ steps.env.outputs.env }} --output d1-backup.sql
|
|
packageManager: pnpm
|
|
# wrangler-action@v3 installs an old default wrangler that can't run `d1 migrations
|
|
# apply` against this config or deploy an assets-only Worker. Pin a current 4.x.
|
|
wranglerVersion: '4.106.0'
|
|
|
|
- name: Upload D1 backup artifact
|
|
if: env.DEPLOY_ENABLED == 'true'
|
|
uses: actions/upload-artifact@v7
|
|
with:
|
|
name: d1-backup-${{ steps.env.outputs.env }}-${{ github.sha }}
|
|
path: worker/d1-backup.sql
|
|
retention-days: 30
|
|
|
|
- name: Apply D1 migrations (${{ steps.env.outputs.env }})
|
|
if: env.DEPLOY_ENABLED == 'true'
|
|
uses: cloudflare/wrangler-action@v4
|
|
with:
|
|
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
|
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
|
workingDirectory: worker
|
|
command: d1 migrations apply ${{ steps.env.outputs.db }} --remote --env ${{ steps.env.outputs.env }}
|
|
packageManager: pnpm
|
|
# wrangler-action@v3 installs an old default wrangler that can't run `d1 migrations
|
|
# apply` against this config or deploy an assets-only Worker. Pin a current 4.x.
|
|
wranglerVersion: '4.106.0'
|
|
|
|
- name: Deploy worker (${{ steps.env.outputs.env }})
|
|
if: env.DEPLOY_ENABLED == 'true'
|
|
uses: cloudflare/wrangler-action@v4
|
|
with:
|
|
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
|
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
|
workingDirectory: worker
|
|
command: deploy --env ${{ steps.env.outputs.env }}
|
|
packageManager: pnpm
|
|
# wrangler-action@v3 installs an old default wrangler that can't run `d1 migrations
|
|
# apply` against this config or deploy an assets-only Worker. Pin a current 4.x.
|
|
wranglerVersion: '4.106.0'
|