每个移动开发团队都深有体会:功能已经可以评审,但要交到相关方手里,就得在 TestFlight 或 Google Play 测试版审核的迷宫里打转。本该几分钟搞定的事,却变成数小时的等待、安装和管理测试版构建。
如果你的生产应用能直接从任何 Pull Request 把最新改动拉到设备上,无需重装,也不用等应用商店,会怎样?
这正是 PR 预览 所实现的效果。当开发者打开一个 Pull Request 时,一个 GitHub Action 会创建一个专用更新通道并发布更改。任何已安装该应用的人都能切换到该通道,测试功能,再切换回来——全程无需离开现有应用。
TestFlight 的问题
测试移动端功能的传统工作流程大致如下:
- 开发者打开 PR - Code 已可评审
- 等待 TestFlight - 15-30 分钟处理时间
- 查找并安装 - 测试人员寻找正确的 build
- 测试并重复 - 每次变更都意味着再次等待
这造成了瓶颈。QA 被阻塞,等待 build。产品经理无法快速验证功能。开发者在等待反馈时丢失上下文。行业估计这导致每个 PR 约 340 美元的生产力损失。
PR 预览如何运作
PR 预览使用 Capgo 的 channel 系统创建每个 PR 的更新流。流程如下:
- PR 打开或更新 - GitHub Action 触发
- Bundle 已上传 - 你的 JS/CSS 变更进入 PR 专属 channel
- 评论已发布 - 测试人员会在 PR 中收到操作说明
- 即时测试 - 切换频道,测试,再切回
无需安装新应用,也没有 TestFlight 的延迟。同一个生产应用可以从不同的更新频道拉取内容。
配置 PR 预览
在实现 PR 预览之前,你的项目需要配置 Capgo Live Updates。请遵循 Capgo 快速入门指南 (如果尚未配置)。
GitHub Actions 工作流
创建 .github/workflows/pr-preview.yml:
name: PR Preview
on:
pull_request:
types: [opened, synchronize]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Setup Bun
uses: oven-sh/setup-bun@v2
- name: Install Dependencies
run: bun install
- name: Build
run: bun run build
# Create a channel named after your PR (may already exist on synchronize)
- name: Create PR Channel
id: create_channel
continue-on-error: true
run: bunx @capgo/cli@latest channel add pr-${{ github.event.pull_request.number }} --self-assign
env:
CAPGO_TOKEN: ${{ secrets.CAPGO_TOKEN }}
# Upload the build to that channel
- name: Upload to Capgo
run: bunx @capgo/cli@latest bundle upload --channel pr-${{ github.event.pull_request.number }}
env:
CAPGO_TOKEN: ${{ secrets.CAPGO_TOKEN }}
# Post a comment with testing instructions (only on PR open)
- name: Comment on PR
if: github.event.action == 'opened'
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: ${{ github.event.pull_request.number }},
body: '📱 **Test this PR on device:**\n\nOpen your app and switch to channel: `pr-${{ github.event.pull_request.number }}`\n\nUse the shake menu or call `setChannel()` from your app.'
})
关键在于 --self-assign 创建频道时使用的标志。这样测试人员就能在应用内使用该标志切换到该频道。 setChannel() API。
配置 Capgo Token
- 前往你的 Capgo 控制台
- 前往 Settings > API Keys
- 使用以下命令生成新密钥:
all权限 - 将其添加为
CAPGO_TOKEN,并配置在您的 GitHub 仓库 secrets 中
测试人员如何切换频道
测试人员切换到 PR 通道有两种方式:
方案 1:摇一摇菜单(最简单)
在 Capacitor 配置中启用带频道选择器的摇动菜单:
// capacitor.config.ts
const config: CapacitorConfig = {
// ... your other config
plugins: {
CapacitorUpdater: {
shakeMenu: true,
allowShakeChannelSelector: true
}
}
};
测试人员摇一摇设备即可打开调试菜单,菜单中会显示可用频道列表和搜索栏。他们找到自己的 PR 频道(例如, pr-123),点击选中后,应用会自动下载并应用更新。测试完成后,再次摇动并切换回生产环境。
摇动菜单会自动处理整个流程:
- 通过以下方式获取所有可自分配的频道
listChannels() - 显示频道并支持搜索,方便查找特定 PR
- 选择后下载更新
- 提示重新加载,提供“立即重新加载”/“稍后”选项
方案 2:自定义 Channel 选择器 UI
在应用中构建一个频道切换器,列出可用的 PR 频道并让测试人员选择。这里用到两个关键 API:
listChannels()- 获取所有启用自分配的频道setChannel()- 将设备切换到选定的频道
import { CapacitorUpdater } from '@capgo/capacitor-updater';
// Get all available channels (including PR channels)
async function getAvailableChannels() {
const { channels } = await CapacitorUpdater.listChannels();
// Filter to show only PR channels
const prChannels = channels.filter(c => c.name.startsWith('pr-'));
return prChannels;
}
// Switch to a specific PR channel
async function switchToChannel(channelName: string) {
await CapacitorUpdater.setChannel({
channel: channelName,
triggerAutoUpdate: true // Immediately check for updates
});
}
// Return to production
async function switchBackToProduction() {
await CapacitorUpdater.unsetChannel({});
}
// Get current channel
async function getCurrentChannel() {
const { channel } = await CapacitorUpdater.getChannel();
return channel;
}
利用这些基础组件,你可以构建一个简单的 UI:
// Example: List PR channels and let user select
const channels = await getAvailableChannels();
const current = await getCurrentChannel();
// Display channels in your UI
channels.forEach(channel => {
console.log(`${channel.name} ${channel.name === current ? '(current)' : ''}`);
});
// When user selects a channel
await switchToChannel('pr-123');
有关完整的 React 组件示例,请参阅 我们的频道切换文章.
清理 PR 频道
当 PR 合并或关闭后,你需要清理该频道。再添加一个 workflow:
name: Cleanup PR Preview
on:
pull_request:
types: [closed]
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Delete PR Channel
run: bunx @capgo/cli@latest channel delete pr-${{ github.event.pull_request.number }}
env:
CAPGO_TOKEN: ${{ secrets.CAPGO_TOKEN }}
PR 关闭时会自动移除该 channel,保持 channel 列表整洁。
版本兼容性
只有当 JavaScript bundle 与已安装的 native 版本兼容时,PR preview 才能工作。如果你的 PR 包含 native code 变更(新增 Capacitor 插件、iOS/Android 修改),测试人员需要新的 native build。
Capgo 会自动检查版本兼容性。如果 PR 的 bundle 所针对的 native 版本与已安装版本不一致,更新将不会生效。这可以防止因 code 不兼容导致的崩溃。
对于确实需要 native 变更的 PR,你需要分发新的 TestFlight/Play Store build。PR preview 最适合处理不涉及 native code 的 JavaScript、CSS 和资源变更。
PR Preview 适合谁
QA 工程师
- PR 创建后就能立即测试功能
- 无需重新安装即可在多个 PR 间切换
- 在真机上验证修复并检查回归
- 无需再等待 TestFlight 处理
产品经理
- 在合并前审查功能
- 直接在 PR 上提供反馈
- 验证实现是否符合需求
- 缩短审查周期
开发者
- 更快获取变更反馈
- 立即向利益相关者演示功能
- 针对特定用户调试问题
- 减少管理 Beta 构建的时间
对比:传统方式 vs PR Previews
| 维度 | TestFlight/Beta | Capgo PR 预览 |
|---|---|---|
| 构建耗时 | 15-30 分钟 | <1 分钟 |
| 切换 PR | 重装需 5+ 分钟 | 10 秒 |
| 配置复杂度 | 需 App Store 凭据 | 仅需一个 workflow 文件 |
| 清理 | 手动 | 自动 |
| Native code 变更 | 必填 | 可选(仅 JS) |
最佳实践
- Channel 命名要清晰:使用
pr-{number}便于识别的命名规范 - 自动清理: PR 关闭时始终删除 Channel
- 限制访问: 仅在 debug/staging 构建中启用摇一摇菜单
- 记录流程:在 PR 模板中添加测试说明
- 优雅地处理失败:在发布评论前检查通道创建是否成功
何时不应使用 PR Previews
PR 预览仅适用于 JavaScript/CSS 更改。如果您的 PR 包含:
- 新的 Capacitor 插件
- iOS 原生 code 变更
- Android 原生 code 变更
- 影响原生构建的依赖更新
这些变更需要通过传统的 TestFlight/Play Store 分发。
与 Channel Surfing 结合使用
PR 预览与以下功能结合效果最佳: 切换频道。您的应用可以拥有:
production- 面向所有用户的稳定版本beta- 面向自愿参与用户的抢先体验版pr-123- 针对特定 PR 的功能预览
使用生产构建的测试人员可以切换到任意 PR 频道,测试功能后再切回——全程使用同一个已安装的应用。
资源
总结
PR 预览改变了团队评审和测试移动功能的方式。测试人员无需等待 TestFlight 处理,也无需管理多个 Beta 构建,只需使用已安装的应用,就能在几秒内切换到任意 PR 通道。
配置很简单——只需一个 GitHub Actions 工作流文件——收益会在整个团队中不断累积。QA 不再被阻塞,产品经理评审更快,开发者也能更快获得反馈。
先在一个仓库中添加该工作流,看看它如何改变你的评审流程。
继续阅读:把每个 Pull Request 变成可安装的预览版
如果你正在使用 把每个 Pull Request 变成可安装的预览版 要规划 Channel 路由和分阶段发布,请将其与 通道 有关 Channels 的实现细节, 通道 有关 Channels 的实现细节, 通道 有关 Channels 的实现细节, Beta 测试解决方案 适用于 Beta Testing Solution 产品工作流的,以及 版本定向解决方案 适用于 Version Targeting Solution 产品工作流的。