跳转到正文

自动选择 Live Update 或 Native Build

大多数 Capacitor 发布仅涉及 JavaScript,应以 live update的形式发布。部分更改涉及原生 code,需要从 Capgo 构建获取新的二进制文件。本指南介绍如何让 GitHub Actions、GitLab CI 或其他任何 CI/CD 平台在每次推送时自动选择正确的路径——无需人工判断。

Capgo 已经知道哪条路径是安全的。在你的 Web 构建完成后(且在上传或请求原生构建之前),运行:

终端窗口
npx @capgo/cli@latest bundle releaseType com.example.app --channel production
# → OTA ship with bundle upload
# → native ship with Capgo Build

OTA 表示原生包与通道上已上线的版本一致。 native 表示插件、Capacitor 版本或其他原生依赖项已更改——仅通过空中下载(OTA)包无法安全地更新这些设备。

releaseType 比较 原生包元数据 (Capacitor/Cordova 插件及版本)。它不会 不会 查看 ios/, android/,或 capacitor.config.*下的所有编辑。请先在 git 中限制这些路径,然后使用 releaseType 检查依赖兼容性——以下示例同时执行了这两项操作。

查看 原生兼容性 查看完整规则及手动 bundle compatibility 表格。

  • Capgo 应用已注册,且 Capgo API 密钥 在 CI 密钥中作为 CAPGO_TOKEN
  • Live Updates 上传正常(bundle upload)—— 参见 CI/CD 集成
  • 如果预期有原生任务,请在 CI 中配置 Capgo 构建凭据 — 参见 GitHub 操作 或 凭据
  • 一个已存在且与生产环境匹配的 channel(示例中使用 production)
  • 在……上的 channel metadata 策略,以便每次 upload 都能携带 --auto-min-update-version (一次性):
终端窗口
npx @capgo/cli@latest channel set production com.example.app --disable-auto-update metadata
  1. 照常构建 web assets。
  2. 如果提交涉及 ios/, android/,或 capacitor.config.*,则强制走原生构建路径。
  3. 否则询问 Capgo releaseType 该提交是否适合 OTA 更新。
  4. 如果 OTA,使用 --fail-on-incompatible 以及 --auto-min-update-version.
  5. 如果 native,运行 Capgo Build,然后上传对应的 bundle --auto-min-update-version 这样通道的原生元数据才会推进。请 不会 使用 --fail-on-incompatible 针对该基线上传——新的原生包应该不同。参见 Native + OTA Channel 工作流 查看通道级 FAQ。

一个工作流,先对原生路径做门禁,再根据 releaseType:

.github/workflows/capgo-release.yml
name: Capgo Release
on:
push:
branches: [main]
jobs:
decide:
runs-on: ubuntu-latest
outputs:
release_type: ${{ steps.verdict.outputs.type }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v6
with:
node-version: '24'
cache: 'npm'
- run: npm ci
- run: npm run build
- name: Decide OTA vs native
id: verdict
env:
CAPGO_TOKEN: ${{ secrets.CAPGO_TOKEN }}
run: |
BEFORE="${{ github.event.before }}"
if [ -z "$BEFORE" ] || [ "$BEFORE" = "0000000000000000000000000000000000000000" ]; then
BEFORE="$(git rev-parse HEAD~1 2>/dev/null || echo '')"
fi
if [ -z "$BEFORE" ] || git diff --name-only "$BEFORE" "${{ github.sha }}" \
| grep -qE '^(ios/|android/|capacitor\.config\.)'; then
TYPE=native
echo "Native path/config changed (or no prior commit) — forcing native"
else
TYPE=$(npx @capgo/cli@latest bundle releaseType com.example.app --channel production | tr -d '[:space:]')
fi
echo "type=$TYPE" >> "$GITHUB_OUTPUT"
echo "Capgo release type: $TYPE"
live_update:
needs: decide
if: needs.decide.outputs.release_type == 'OTA'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v6
with:
node-version: '24'
cache: 'npm'
- run: npm ci
- run: npm run build
- name: Upload live update
env:
CAPGO_TOKEN: ${{ secrets.CAPGO_TOKEN }}
run: |
npx @capgo/cli@latest bundle upload com.example.app \
--channel production \
--fail-on-incompatible \
--auto-min-update-version
native_build:
needs: decide
if: needs.decide.outputs.release_type == 'native'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
platform: [ios, android]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v6
with:
node-version: '24'
cache: 'npm'
- run: npm ci
- run: npm run build
- run: npx cap sync ${{ matrix.platform }}
- name: Capgo Build ${{ matrix.platform }}
env:
CAPGO_TOKEN: ${{ secrets.CAPGO_TOKEN }}
BUILD_CERTIFICATE_BASE64: ${{ secrets.BUILD_CERTIFICATE_BASE64 }}
P12_PASSWORD: ${{ secrets.P12_PASSWORD }}
CAPGO_IOS_PROVISIONING_MAP: ${{ secrets.CAPGO_IOS_PROVISIONING_MAP }}
APPLE_KEY_ID: ${{ secrets.APPLE_KEY_ID }}
APPLE_ISSUER_ID: ${{ secrets.APPLE_ISSUER_ID }}
APPLE_KEY_CONTENT: ${{ secrets.APPLE_KEY_CONTENT }}
APP_STORE_CONNECT_TEAM_ID: ${{ secrets.APP_STORE_CONNECT_TEAM_ID }}
ANDROID_KEYSTORE_FILE: ${{ secrets.ANDROID_KEYSTORE_FILE }}
KEYSTORE_KEY_ALIAS: ${{ secrets.KEYSTORE_KEY_ALIAS }}
KEYSTORE_KEY_PASSWORD: ${{ secrets.KEYSTORE_KEY_PASSWORD }}
KEYSTORE_STORE_PASSWORD: ${{ secrets.KEYSTORE_STORE_PASSWORD }}
PLAY_CONFIG_JSON: ${{ secrets.PLAY_CONFIG_JSON }}
run: |
npx @capgo/cli@latest build request com.example.app \
--platform ${{ matrix.platform }} \
--build-mode release
native_bundle:
needs: [decide, native_build]
if: needs.decide.outputs.release_type == 'native'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v6
with:
node-version: '24'
cache: 'npm'
- run: npm ci
- run: npm run build
- name: Upload bundle for new native baseline
env:
CAPGO_TOKEN: ${{ secrets.CAPGO_TOKEN }}
run: |
# Channel must already be on metadata (see Prerequisites above)
npx @capgo/cli@latest bundle upload com.example.app \
--channel production \
--auto-min-update-version

替换 com.example.app 并按照说明配置签名密钥 用于 Capgo Build 的 GitHub Actions.

GitLab 评估 rules 在 pipeline 创建时,因此用 shell 分支 if 在一个 deploy job 中(或生成一个 动态子 pipeline 如果你需要独立的 native matrix job):

.gitlab-ci.yml
image: node:24
stages:
- build
- deploy
variables:
APP_ID: com.example.app
CHANNEL: production
build_web:
stage: build
script:
- npm ci
- npm run build
artifacts:
paths:
- dist/
- node_modules/
expire_in: 1 hour
only:
- main
deploy:
stage: deploy
needs: [build_web]
script:
- |
BEFORE="${CI_COMMIT_BEFORE_SHA:-}"
if [ -z "$BEFORE" ] || [ "$BEFORE" = "0000000000000000000000000000000000000000" ]; then
BEFORE="$(git rev-parse HEAD~1 2>/dev/null || echo '')"
fi
if [ -z "$BEFORE" ] || git diff --name-only "$BEFORE" "$CI_COMMIT_SHA" \
| grep -qE '^(ios/|android/|capacitor\.config\.)'; then
TYPE=native
else
TYPE=$(npx @capgo/cli@latest bundle releaseType "$APP_ID" --channel "$CHANNEL" | tr -d '[:space:]')
fi
echo "Capgo release type: $TYPE"
if [ "$TYPE" = "OTA" ]; then
npx @capgo/cli@latest bundle upload "$APP_ID" \
--channel "$CHANNEL" \
--fail-on-incompatible \
--auto-min-update-version
elif [ "$TYPE" = "native" ]; then
npx cap sync
npx @capgo/cli@latest build request "$APP_ID" --platform ios --build-mode release
npx @capgo/cli@latest build request "$APP_ID" --platform android --build-mode release
npx @capgo/cli@latest bundle upload "$APP_ID" \
--channel "$CHANNEL" \
--auto-min-update-version
else
echo "Unexpected release type: $TYPE" >&2
exit 1
fi
only:
- main

商店 CAPGO_TOKEN 以及 Capgo Build 签名变量,作为 masked/protected CI/CD 变量。

同样的三个步骤在任何地方都适用:

步骤命令
结论npx @capgo/cli@latest bundle releaseType APP_ID --channel production
OTA 路径npx @capgo/cli@latest bundle upload APP_ID --channel production --fail-on-incompatible --auto-min-update-version
Native 路径npx @capgo/cli@latest build request APP_ID --platform ios (或 android) --build-mode release

将 shell 的退出码 / stdout 映射到平台的条件判断中(或保留一个带 shell 的单一 job if,就像上面的 GitLab 那样):

  • Azure Pipelines — 在脚本步骤中设置输出变量,然后使用 condition: eq(variables['releaseType'], 'OTA')
  • Bitbucket Pipelines — 写入 RELEASE_TYPE=… 改为 $BITBUCKET_PIPELINES_VARIABLES_PATH,然后在下面声明它 output-variables,并在后续步骤中分支 condition: state: RELEASE_TYPE == "OTA" (仅文件产物无法驱动 condition)
  • CircleCI — when 在配置编译时求值,因此请使用运行时 shell 进行分支 if (或动态配置 / 续接),而不是 when
  • Jenkins — 将 stdout 捕获到环境变量中,并使用 when { environment name: 'RELEASE_TYPE', value: 'OTA' }

路径过滤器是一种成本优化手段,不能替代 Capgo 检查。建议排除仅包含文档的路径,而不是维护脆弱的允许列表——Web 构建通常还依赖于 vite.config.*, tsconfig*.json,以及框架配置文件:

on:
push:
branches: [main]
paths-ignore:
- '**.md'
- 'docs/**'
- '.github/**'

如果改用 allowlist,请包含 Web 和 Native 构建读取的所有输入,而不仅仅是 src/ 以及 package.json.

当 CI 选择 native 时:

  1. Capgo Build 会生成签名二进制文件,并可提交至 TestFlight / Play(参见 配置).
  2. 使用 --auto-min-update-version (元数据策略)上传对应的 JS bundle,以便 channel 记录新的 native 包——否则,下一个仅包含 JS 的提交仍会返回 native.
  3. 用户安装新二进制文件后,后续仅包含 JavaScript 的提交将恢复为 OTA 再次尝试。