相关文档

https://nitro.unjs.io/deploy/providers/cloudflare

部署流程

  • 授权登录wrangler

    npx wrangler login
  • 获取account_id

    npx wrangler whoami
  • nuxt3项目根目录创建wrangler.toml文件

    name = "yourprogramname"
    main = "./.output/server/index.mjs"
    workers_dev = true
    compatibility_date = "2022-09-10"
    account_id = ""
    route = "<设置自定义域名 (可选项)>"
    [site]
    bucket = ".output/public"
  • 打包项目

    NITRO_PRESET=cloudflare pnpm build
  • 部署到Cloudflare

    npx wrangler publish

    至此已成功手动部署项目到 Workers

使用 Github Actions 自动部署

  • 在 Github 该项目的 settings -> Secrets and variables -> Actions 创建一个CF_API_TOKEN,Token获取流程看这里

  • 创建文件 .github/workflows/cloudflare.yml

    name: cloudflare
    
    on:
      push:
        branches: [ master ]
      pull_request:
        branches: [ master ]
    
      workflow_dispatch:
    
    jobs:
      ci:
        runs-on: ${{ matrix.os }}
    
        strategy:
          matrix:
            os: [ ubuntu-latest ]
            node: [ 16 ]
    
        steps:
          - uses: actions/setup-node@v1
            with:
              node-version: ${{ matrix.node }}
    
          - name: Checkout
            uses: actions/checkout@master
    
          - name: Setup PNPM
            uses: pnpm/action-setup@v2.2.4
            with:
              version: 7.13.2
    
          - name: Cache node_modules
            uses: actions/cache@v2
            with:
              path: node_modules
              key: ${{ matrix.os }}-node-v${{ matrix.node }}-deps-${{ hashFiles(format('{0}{1}', github.workspace, '/pnpm-lock.yaml')) }}
    
          - name: Install Dependencies
            if: steps.cache.outputs.cache-hit != 'true'
            run: pnpm i
    
          - name: Build
            run: pnpm build
            env:
              NITRO_PRESET: cloudflare
    
          - name: Publish to Cloudflare
            uses: cloudflare/wrangler-action@2.0.0
            with:
              apiToken: ${{ secrets.CF_API_TOKEN }}
    
  • 搞定!