
后端前端Web框架开发工具【免费下载链接】redwoodRedwoodGraphQL项目地址https://gitcode.com/gh_mirrors/re/redwood点击查看免费下载RedwoodJS 应用同时包含 apiGraphQL/服务端与 webReact 前端两侧测试策略的好坏直接决定发布质量。本文基于 RedwoodJS 官方 how-to 指南带领你从零创建一个带 Postgres 数据库的 Redwood 应用并逐步搭建 GitHub Actions 工作流在每次 push 与 pull request 上自动运行测试CI再进一步把数据库迁移自动部署到真实数据库CD全程用 GitHub Secrets 保护敏感凭据。读完本文你将拥有一套可复制、可运行、可继续演进的 RedwoodJS 自动化测试与部署流水线。背景CI、CD 与 GitHub 生态在动手写代码之前先明确几个贯穿全文的核心概念。持续集成CI持续集成Continuous IntegrationCI指在每次 push 或 pull request 时自动运行测试的实践。它是在代码合并进主分支main之前捕获 bug 的最有效手段之一——测试一旦失败合并就会被阻断问题被拦在主干之外。持续部署CD持续部署Continuous DeploymentCD指在每次测试成功之后自动把应用以及本文场景中的数据库部署到服务器的实践。它保证线上环境始终与最新代码保持同步把手动跑迁移、手动灌种子数据这类重复操作交给机器。GitHub Actions 与 GitHub SecretsGitHub Actions 是 GitHub 提供的托管式工作流服务让你在虚拟机上按事件push、pull_request 等执行一串命令可用于跑测试、部署应用或任意自动化任务。它对公共仓库免费私有仓库也有免费额度。GitHub Secrets 则是存储 API Key、密码等敏感信息的机制Secret 经过加密仅在 GitHub Actions 运行环境中暴露不会出现在日志或源码里。你可以用它在测试或部署脚本中安全传递敏感数据。两者配合构成了提交代码 → 自动测试 → 自动部署的完整闭环。第一步创建一个 Redwood 应用如果你还没有现成的 Redwood 项目先创建一个yarn create redwood-app rw-testing-gh-actions cd rw-testing-gh-actions随后验证脚手架自带的测试能正常通过yarn rw test一切正常时输出大致如下Jest 默认进入 watch 模式... PASS api api/src/directives/requireAuth/requireAuth.test.ts PASS api api/src/directives/skipAuth/skipAuth.test.ts Test Suites: 2 passed, 2 total Tests: 3 passed, 3 total Snapshots: 0 total Time: 1.669 s Ran all test suites. Watch Usage: Press w to show more.已有现成项目的读者可以跳过前两步直接进入 在 GitHub Actions 中运行测试但需确保自己的测试已基于 Postgres 数据库。第二步把数据库切换为 PostgresRedwood 新建项目默认使用 SQLite。为了让本地与 CI 环境行为一致我们需要把 Prisma 数据源切换为 PostgreSQL。2.1 准备好本地 Postgres 实例先确保本机有一个可用的 Postgres 实例macOS 可用brew install postgresql14安装并拿到连接串Redwood 应用需要靠它定位数据库。完整的本地安装与建库步骤参见 Local Postgres Setup其中也讲解了connection_limit参数在 Serverless 场景下的推荐用法。2.2 修改 Prisma schema把api/db/schema.prisma修改为如下内容——UserExample是 Redwood 脚手架自带的示例模型我们直接用它的服务测试来验证 CI 全链路datasource db { provider postgresql url env(DATABASE_URL) } generator client { provider prisma-client-js binaryTargets native } model UserExample { id Int id default(autoincrement()) email String unique name String? }2.3 配置连接字符串在项目根目录的.env文件中加入开发库与测试库两条连接串DATABASE_URLpostgres://postgres:postgreslocalhost:54322/postgres TEST_DATABASE_URLpostgres://postgres:postgreslocalhost:54322/postgres:::warning.env内含敏感信息务必确保它已被.gitignore忽略、绝不提交到仓库。Redwood 的项目模板默认就以.env*形式排除了这类文件参见 create-redwood-app 模板 中的!.env.example/!.env.defaults例外规则。:::开发库与测试库各需一条连接串测试库的用途会在后文源码视角章节详细展开也可参考 Redwood 测试文档 的 The Test Database 一节。2.4 编写 seed 脚本编辑scripts/seed.ts取消注释假用户数组并改用 Prisma 的createMany方法批量插入配合skipDuplicates: true可跳过重复记录PostgreSQL 下批量插入远快于逐条插入... const data: Prisma.UserExampleCreateArgs[data][] [ // To try this example data with the UserExample model in schema.prisma, // uncomment the lines below and run yarn rw prisma migrate dev // { name: alice, email: aliceexample.com }, { name: mark, email: markexample.com }, { name: jackie, email: jackieexample.com }, { name: bob, email: bobexample.com }, ] console.log( \nUsing the default ./scripts/seed.{js,ts} template\nEdit the file to add seed data\n ) // Note: if using PostgreSQL, using createMany to insert multiple records is much faster // see: https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#createmany Promise.all( // // Change to match your data model and seeding needs // data.map(async (data: Prisma.UserExampleCreateArgs[data]) { const record await db.userExample.createMany({ data, skipDuplicates: true, }) console.log(record) }) ) ...最后创建并应用初始迁移yarn rw prisma migrate dev --name init第三步生成 UserExample scaffold 以获得真实测试为了有一批货真价实的测试可用用 Redwood 的生成器把UserExample模型脚手架化。它会产出创建/编辑/删除用户所需的页面、组件与 service其中就包含与测试数据库交互的 service 测试yarn rw g scaffold UserExample再次确认一切正常yarn rw test此时应看到 web 与 api 两侧的测试同时运行例如PASS web web/src/lib/formatters.test.tsx PASS api api/src/services/userExamples/userExamples.test.ts Test Suites: 2 passed, 2 total Tests: 21 passed, 21 total Snapshots: 0 total Time: 3.587 s Ran all test suites related to changed files in 2 projects.在 GitHub Actions 中运行测试核心环节来了。在项目根目录创建.github/workflows/ci.yml若.github/workflows目录不存在则一并创建内容如下:::note下面的工作流仅在main分支有更新时触发你也可以按需配置成任意其他分支。:::name: Redwood CI on: push: branches: [main] pull_request: branches: [main] env: DATABASE_URL: postgres://postgres:postgreslocalhost:5432/postgres TEST_DATABASE_URL: postgres://postgres:postgreslocalhost:5432/postgres jobs: build: runs-on: ubuntu-latest strategy: matrix: node-version: [18.x] # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ services: # Label used to access the service container postgres: # Docker Hub image image: postgres # Provide the password for postgres env: POSTGRES_PASSWORD: postgres # Set health checks to wait until postgres has started options: - --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 ports: # Maps tcp port 5432 on service container to the host - 5432:5432 steps: - uses: actions/checkoutv3 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-nodev3 with: node-version: ${{ matrix.node-version }} # install all the dependencies - run: yarn install # build the redwood app - run: yarn rw build # run the api tests - run: yarn rw test api --no-watch # run the web tests - run: yarn rw test web --no-watch这段工作流值得逐块解读配置块作用on: push / pull_request在 push 到main或向main发起 PR 时触发env为整个 job 注入DATABASE_URL与TEST_DATABASE_URL指向服务容器内的 Postgresstrategy.matrix.node-version用矩阵并行测试多个 Node 版本此处为 18.xservices.postgres启动一个 Postgres 服务容器用pg_isready做健康检查并映射 5432 端口actions/checkoutv3拉取仓库代码actions/setup-nodev3安装指定版本 Node.jsyarn install安装全部依赖yarn rw build构建 Redwood 应用生成 GraphQL 类型、Prisma Client 等yarn rw test api --no-watch/yarn rw test web --no-watch分别以非监听模式运行 api 与 web 侧测试把改动 push 到 GitHub 的main分支后Redwood CI 工作流会依次执行创建 job名为 build初始化容器并拉起 postgres 实例检出checkout代码配置 Node.js安装 Redwood 应用依赖构建 Redwood 应用运行 api 测试运行 web 测试清理环境如果一切顺利你将体会到自动化测试的乐趣push 一个 commit → Action 自动运行 → 测试通过 → 收获绿色对勾。为了巩固这份喜悦可以故意改坏一个单元测试再 push观察它失败修复后再次 push观察它重新变绿。重复这个过程自动化就会成为习惯。只在 Pull Request 上运行 CI很多团队希望测试只在 PR 阶段把关合并进main后不再重复跑。删掉ci.yml中的push事件即可文件头部变成name: Redwood CI for Pull Requests on: pull_request: branches: [main] ...此后每当你打开一个 PR 或向已有 PR 推送新提交该工作流都会自动触发运行结束后可在 PR 的 Conversation 标签页查看执行结果。数据库迁移自动部署CDCI 只负责验证接下来用 CD 把数据库变更部署到真实环境。思路是在同一个 job 里先对本地服务容器数据库再跑一遍测试然后对外部真实数据库执行迁移与种子脚本。在.github/workflows下新建cd.ymlname: Redwood CD for database deployment on: push: branches: [main] env: DATABASE_URL: postgres://postgres:postgreslocalhost:5432/postgres TEST_DATABASE_URL: postgres://postgres:postgreslocalhost:5432/postgres jobs: build: runs-on: ubuntu-latest strategy: matrix: node-version: [18.x] # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ services: # Label used to access the service container postgres: # Docker Hub image image: postgres # Provide the password for postgres env: POSTGRES_PASSWORD: postgres # Set health checks to wait until postgres has started options: - --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 ports: # Maps tcp port 5432 on service container to the host - 5432:5432 steps: - uses: actions/checkoutv3 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-nodev3 with: node-version: ${{ matrix.node-version }} # install all the dependencies - run: yarn install # build the redwood app - run: yarn rw build # run the api tests - run: yarn rw test api --no-watch # run the web tests - run: yarn rw test web --no-watch # run migrations on the actual database - run: yarn rw prisma migrate deploy # run seed script in the actual db - run: yarn rw prisma db seed与 CI 工作流相比主要变化有两点只在push到main分支时触发不再监听 PR测试通过后追加两步yarn rw prisma migrate deploy把迁移应用到真实数据库yarn rw prisma db seed执行种子脚本填充数据注意migrate deploy与本地开发用的migrate dev语义不同deploy按顺序应用现有迁移文件、不生成新迁移正适合部署场景。用 GitHub Secrets 保护数据库凭据在上面的cd.yml里迁移针对的其实仍是本地容器数据库。要让变更真正落到外部真实数据库需要把连接串改为从 GitHub Secrets 读取进入 GitHub 仓库的Settings标签页依次点击Secrets → Actions → New repository secret在 Name 字段输入DATABASE_URL在 Value 字段粘贴真实连接串形如postgres://[USER_NAME]:[PASSWORD][HOST]:[PORT]/postgres点击Add secret完成创建之后即可在工作流中用${{ secrets.DATABASE_URL }}语法引用该 Secret覆盖env中的默认值env: DATABASE_URL: ${{ secrets.DATABASE_URL }}这样凭据只存在于加密的 Secret 中不会进入工作流日志。合并 PR 后数据库变更将按测试通过 → 部署到真实库的顺序自动完成。剩下的优化空间就交给你了——比如拆分 job、缓存依赖、失败告警等。源码视角yarn rw test与测试数据库的底层机制前文工作流中的--no-watch、TEST_DATABASE_URL并非魔法在仓库源码中都有明确对应实现理解它们能帮你排查 CI 中的疑难杂症。yarn rw test命令的行为test子命令定义在 packages/cli/src/commands/test.js它接受[filter..]位置参数默认值为当前项目所有 side即api与web并提供三个专属选项--watch默认true启动监听模式--collect-coverage默认false输出测试覆盖率--db-push默认true测试前自动把 Prisma schema 同步到测试数据库真正执行逻辑在 packages/cli/src/commands/testHandler.js有几个关键点与 CI 直接相关除上述三个专属 flag 与少数内部 flag 外其余参数会被原样转发给 Jest这也是--no-watch能生效的原因当watch为真且process.env.CI未设置时才会追加--watch/--watchAll在 GitHub Actions 这类 CI 环境中即使忘记加--no-watchwatch 模式也会被自动禁用避免进程挂起通过--projects api web分别指定两侧 Jest 项目运行 API 测试时若没有显式关闭db-push会在测试前同步数据库结构测试数据库是如何准备的Redwood 为了让 service 测试不污染开发库会使用独立的测试数据库。测试文档Testing 的 The Test Database 一节明确说明测试数据库位置由TEST_DATABASE_URL环境变量决定未设置时回退到.redwood/test.db。在 CI 工作流中我们同时设置了DATABASE_URL与TEST_DATABASE_URL正是为了让 Redwood 在跑 api 测试时使用 Postgres 容器内的数据库。这一逻辑在 packages/testing/config/jest/api/globalSetup.js 中实现它会加载.env、以TEST_DATABASE_URL或默认库作为DATABASE_URL然后执行yarn rw prisma db push --force-reset --accept-data-loss把 schema 快照灌入测试库若设置了TEST_DATABASE_STRATEGYreset则改为prisma migrate reset --force --skip-seed按顺序回放全部迁移。:::warning 自定义迁移 SQL 的坑db push只恢复当前 schema 快照不会按顺序执行迁移文件。如果你的迁移里有必须靠 SQL 语句生效的数据库配置如自定义触发器测试库会与真实环境不一致。此时可在.env中设置TEST_DATABASE_STRATEGYreset让测试库改用migrate reset完整回放迁移代价是测试启动时间随迁移数量增加。:::两侧的 Jest 预设新建 Redwood 项目的api/jest.config.js与web/jest.config.js只是薄薄一层配置分别指向redwoodjs/testing/config/jest/api与redwoodjs/testing/config/jest/web两个预设可参考 empty-project 的 api 配置 与 web 配置。预设内部处理了 Babel 转换、Mock Service Worker 环境、以及 API 侧的数据库初始化等逻辑这也是为什么 CI 中无需额外安装 Jest 配置即可直接运行yarn rw test api --no-watch。小结至此我们完成了一条完整的 RedwoodJS 自动化流水线本地切换 Postgres → 生成带测试的 scaffold → 在 GitHub Actions 中用服务容器跑 CI → 按需切换为 PR-only 触发 → 用 CD 工作流把迁移与种子数据部署到真实数据库 → 用 GitHub Secrets 保护连接串。再结合--no-watch的非监听模式、TEST_DATABASE_URL与TEST_DATABASE_STRATEGY的测试库策略你已经能在每次提交时自动验证 api 与 web 两侧的代码质量。正如哲学家 Alfred North Whitehead 所言文明的进步在于不断扩展那些无需思考即可完成的重要操作的数量。把测试与部署交给自动化正是把精力留给真正需要思考的代码。赞分享后端前端Web框架开发工具【免费下载链接】redwoodRedwoodGraphQL项目地址https://gitcode.com/gh_mirrors/re/redwood点击查看免费下载相关推荐在 GitHub Actions 中运行 RedwoodJS 测试CI/CD 与 Postgres 测试数据库完整指南在 GitHub Actions 中运行 RedwoodJS 测试CI/CD 与 Postgres 测试数据库完整指南 RedwoodJS 提供了一套开箱即用后端前端Web框架开发工具Redwood 在 GitHub Actions 中运行自动化测试与数据库持续部署v5 实战指南Redwood 在 GitHub Actions 中运行自动化测试与数据库持续部署v5 实战指南 本篇技术指南以 Redwood v5 为基准讲解如何把后端前端Web框架开发工具CANN/TensorFlow数据并行训练指南支持数据并行Allreduce AllReduce是主流的数据并行架构各个节点按照算法协同工作适用于对训练算力要求高、设备规模大的场景。本节介绍如何将T测试网页爬虫RPA创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考