# runbuild

> 按项目的 airbuild.yaml 构建：日常构建、签名构建，或未签名的设备安装包。

Source: https://air.build/cn/docs/cli/runbuild/

`runbuild` 运行项目 `airbuild.yaml` 中的构建脚本来构建项目，然后取回脚本构建出的产物。它在团队的环境中运行，也就是智能体工作的地方。

| 团队 | 构建在哪里运行 |
| --- | --- |
| 云端团队 | 项目会上传到 macOS 构建机（`ios`）或 Linux 构建机（`android_beta`）。`pocketjs_beta` 在团队自己的主机上构建。 |
| Mac 团队 | 在团队自己的 Mac 上，使用这台 Mac 的 Xcode。`android_beta` 只能在云端团队中构建。 |

`runbuild` 从不安装 App。构建完成后，它会输出安装并打开 App 的 `simulator` 命令；由你自己在选定的设备上运行（见 [simulator](https://air.build/cn/docs/cli/simulator/)）。

## 用法

```bash
# Start a build
runbuild <project-id> [--platform <platform>] [--source <directory>] [--sign | --unsigned] [--environment ID] [--no-cache]

# Reattach to a build already in progress (starts nothing)
runbuild <project-id> --follow [--run ID]

# Other
runbuild --cancel [--run ID]
runbuild --list-environments
runbuild --help
```

`<project-id>` 是项目的 ID，一个 UUID。项目位于 `/projects/<project-id>` 下。

## 选项

| 选项 | 取值 | 默认值 | 作用 |
| --- | --- | --- | --- |
| `<project-id>` | UUID | 开始或跟进构建时必填 | 要构建的项目。只能有一个。 |
| `--platform` | `ios`、`pocketjs_beta`、`android_beta` | 默认清单 | 使用 `.airbuild/airbuild.<platform>.yaml` 而不是默认的 `airbuild.yaml` 构建。 |
| `--source` | 目录 | 项目目录 | 改为构建该目录（例如一个 git worktree）。如果该目录有自己的 `airbuild.yaml` 就用它，否则用项目的。结果照常记录到项目下。路径相对于当前目录。 |
| `--sign` | — | 关 | 签名构建。iOS：用于 TestFlight 和 App Store 的 `.ipa`，需要已连接的 Apple 团队。`android_beta`：`release:` 配方产出的 `.aab` 或 `.apk`，用该 App 的密钥签名。 |
| `--unsigned` | — | 关 | 仅限 iOS：未签名的设备 `.ipa` 及其 Xcode 归档（`.xcarchive.zip`，含 dSYM）。不需要 Apple 团队。这与 AirBuild App 中 **下载安装包** 提供给用户的是同一个安装包。必须有人签名后才能安装到设备上。 |
| `--environment` | `--list-environments` 中的 ID | 默认环境 | 云端团队：在该构建机环境（某个 macOS 和 Xcode 版本）上构建。它决定 App 用什么构建，而不是在哪里运行，启动可能更慢。 |
| `--no-cache` | — | 启用缓存 | 仅本次运行：从空缓存开始，且不保存任何缓存。 |
| `--follow` | — | — | 重新连接到正在运行的构建。如果构建已经结束，会说明这一点并重放那次运行保存的输出。它从不启动构建。 |
| `--run` | 运行 ID | 正在运行的那次，否则为最新一次 | 与 `--follow` 或 `--cancel` 同用：指定哪一次运行。有多个正在运行时必填。 |
| `--cancel` | — | — | 请求正在运行的构建停止，并最多等待 30 秒确认。 |
| `--list-environments` | — | — | 云端团队：列出构建机环境，默认环境排在最前。 |
| `-h`, `--help` | — | — | 打印命令帮助。 |

`--sign` 和 `--unsigned` 是两种不同的构建，只能传一个。`--environment` 和 `--no-cache` 仅在开始构建时有效；`--run` 仅与 `--follow` 和 `--cancel` 同用。`runbuild` 不接受 `--simulator`：构建完成后用 `simulator install <path> --simulator ID` 安装。

## 构建类型

| 构建 | 运行的脚本 | 脚本获得 | 结果 |
| --- | --- | --- | --- |
| 普通（`runbuild <id>`） | `build.script` | `AIRBUILD_SIGN=0` | 构建出的 `.app`（iOS）、`.pocket`（`pocketjs_beta`）或 `.apk`（`android_beta`）。App 的名称、Bundle ID 和图标会上报给 AirBuild。 |
| `--sign` | `release.script`，没有则用 `build.script` | `AIRBUILD_SIGN=1` | 签名的 `.ipa` 及其 dSYM（iOS），或签名的 `.aab` / `.apk`（`android_beta`）。 |
| `--unsigned` | `release.script`，没有则用 `build.script` | `AIRBUILD_SIGN=0`、`AIRBUILD_DEVICE=1` | 未签名的设备 `.ipa` 和 `.xcarchive.zip`。 |

- 签名的 iOS 构建会为 `signing.bundles` 中的每个 Bundle ID 准备签名，然后执行一次归档和 IPA 导出。它不会改动你自己的签名身份。请在构建前在项目中设置好构建号：AirBuild 不会修改它。
- iOS `--sign` 构建成功后，会把 App 的 dSYM 上传到 AirFeedback，这样崩溃报告就能显示函数名和源代码行。
- 使用 `--unsigned` 时，如果清单的产物不是 `.xcarchive`（或者没有清单），`runbuild` 会自己找到 Xcode workspace 或 project 以及 iOS App 的 scheme。
- `android_beta`：每次构建完成后，都会用该 App 唯一的密钥重新签名，即团队为 `signing.package` 持有的密钥，在 App 第一次构建时创建。这包括模拟器用的 `.apk`（也就是用户下载的 APK），以及使用 `--sign` 时的发布版 `.aab` 或 `.apk`。项目自己的 Gradle 签名会被替换。第一次时，运行会输出 `Created the signing key for <package>.`

## 缓存

云端构建会在多次构建之间保留缓存：

- **iOS：** 项目的 DerivedData。一次构建中第一次运行 `airbuild-xcode install` 或 `airbuild-xcode test` 时会把它取回，所以只重新编译改动过的部分。`airbuild-xcode archive`（签名构建和设备构建）不保留缓存。
- **`android_beta`：** Gradle 的构建缓存和编译好的构建脚本。
- **其他内容：** 由脚本使用 [airbuild-cache](https://air.build/cn/docs/cli/airbuild-cache/) 恢复。

每次构建完成、结果取回之后，各个缓存都会重新保存，且只保留最新的一份。`--no-cache` 可以对单次运行关闭这一功能。

## runbuild 从 airbuild.yaml 读取的内容

`runbuild` 先在项目根目录查找 `airbuild.yaml`，再到 `.airbuild/` 中查找。如果两处都有，它使用根目录的那个，并输出 `Using airbuild.yaml at the project root; .airbuild/airbuild.yaml is ignored while the root manifest exists.`

一个项目可以为每个平台各准备一份清单：

| 文件 | 平台 |
| --- | --- |
| `airbuild.yaml`（根目录，否则 `.airbuild/`） | 由其 `platform:` 决定；版本 2 和 3 为 `ios` |
| `.airbuild/airbuild.<platform>.yaml` | `<platform>`；其 `platform:` 必须一致 |

不带 `--platform` 时，`runbuild` 使用默认的 `airbuild.yaml`；如果没有默认清单且只有一份清单，就用那一份。使用按平台命名的文件时，会输出 `Platform <platform>: <path>.`

| 键 | 用途 |
| --- | --- |
| `version` | `2`、`3` 或 `4`。版本 2 只支持普通构建；签名需要版本 3 或更高。 |
| `platform` | 仅版本 4，必填：`ios`、`pocketjs_beta` 或 `android_beta`。版本 3 中不允许。 |
| `build.script` | 普通构建的 Bash 脚本。它在源代码根目录以 `-e -o pipefail`（不含 `-u`）运行。 |
| `build.artifact.path` | 构建脚本写出的产物：`.app`（iOS）、`.pocket` 包（`pocketjs_beta`）或 `.apk`（`android_beta`）。 |
| `release.script`、`release.artifact.path` | 由 `--sign` 和 `--unsigned` 运行。iOS 的路径以 `.xcarchive` 结尾；`android_beta` 以 `.aab` 或 `.apk` 结尾。 |
| `signing.bundles` | iOS：归档中每个 App 和扩展的 Bundle ID。App Clip 写成映射：`{ id: <clip ID>, app_clip_of: <parent app ID> }`。 |
| `signing.package` | `android_beta`：App 的 applicationId。没有它时，`runbuild` 会使用在 Gradle 构建文件中找到的那一个 `applicationId`，并提示你把它固定下来。 |
| `test` | 由 [runtest](https://air.build/cn/docs/cli/runtest/) 读取，而不是 `runbuild`。 |

一份最简的 iOS 清单：

```yaml title="airbuild.yaml"
version: 3
build:
  script: |
    airbuild-xcode install -project App.xcodeproj -scheme App \
      DSTROOT="$PWD/.airbuild/artifacts/install"
  artifact:
    path: .airbuild/artifacts/install/Applications/App.app
release:
  script: |
    airbuild-xcode archive -project App.xcodeproj -scheme App \
      -archivePath .airbuild/artifacts/App.xcarchive
  artifact:
    path: .airbuild/artifacts/App.xcarchive
signing:
  bundles:
    - com.example.app
```

## 输出

输出为纯文本，没有 JSON 模式。每行以 `•`（进度）、`✓`（完成）、`!`（警告）或 `✗`（失败）开头。一次云端 iOS 构建的输出如下：

```text
• Build started in the Environment. Run: <run-id>
• It runs to completion even if this command is interrupted (reattach with: runbuild <project-id> --follow --run <run-id>).
Airbuild remote build

✓ Build <build-id> created.
✓ Source uploaded (<size>).
✓ Build queued.
✓ Builder started.
… the build script's own output …
✓ Build succeeded in <time>.
✓ Built Example.
  App: /tmp/…/Example.app

  simulator terminate <bundle-id>
  simulator install <path to the .app>
  simulator launch <bundle-id>

✓ Build finished.
  Build: <build-id>
  App: /tmp/…/Example.app
```

结尾部分还可能出现 `Artifact:`、`Xcode archive:` 和 `Note:` 行。签名构建会输出 `Signed .ipa ready (…)`，并附 `Saved to:` 和 `dSYMs:`；未签名构建会输出 `Unsigned device .ipa ready (…)`，并附 `Saved to:` 和 `Xcode archive:`。macOS App 可以构建，但会输出 `macOS apps have no Team Simulator preview; use runmacos to run it.`（macOS App 没有团队模拟器预览，请用 runmacos 运行。）

安装新版本前先停止正在运行的 App：安装不会结束旧进程。不能单独打开的 App（例如贴纸包）只会被安装；请启动承载它的那个 App。

`--list-environments` 的输出：

```text
Builder environments (runbuild --environment ID):

  <id> (default)
      <name> · <platform>
      <summary>
```

## 同时运行多个构建

每个命令（`runbuild`、`runtest`、`runmacos`、`runlinux`）各有自己的槽位，并保留各自最新的结果。云端团队可以同时运行多个构建，每个构建使用自己的构建机；Mac 团队一次只运行一个。开始构建时会输出运行 ID。有多个正在运行时，`--follow` 和 `--cancel` 需要 `--run ID`（或只有其中一个使用的项目 ID）。按 Ctrl-C 只会断开显示，构建会继续进行。

## 退出码

| 退出码 | 含义 |
| --- | --- |
| `0` | 构建（或取消）完成。 |
| `1` | 构建失败，或被环境拒绝、丢失。`Reason:` 会说明原因。 |
| `2` | 选项或参数有误，什么也没有启动。找不到 `airbuild.yaml` 时也返回 2。 |
| `130` | 构建被取消，或你用 Ctrl-C 断开了连接。 |
| 其他 | 构建脚本自己的非零退出码。 |

## 错误

| 消息 | 怎么办 |
| --- | --- |
| `Expected a Project id (UUID), got: …` | 传入项目的 UUID。 |
| `airbuild.yaml is missing from the project root (and .airbuild/): …` | 添加一份清单。 |
| `--sign and --unsigned are two different builds; pass one.` | 二选一。 |
| `runbuild does not install. Once it has built, install with simulator install <path> --simulator ID.` | 去掉 `--simulator`；构建完成后再安装。 |
| `--environment takes an id from runbuild --list-environments.` | 使用该列表中显示的 ID。 |
| `Builder environments belong to cloud Teams; this Team builds with the Xcode on its owning Mac.` | 在 Mac 团队中，不要使用 `--environment` 和 `--list-environments`。 |
| `<name> is not a platform. Platforms: ios, pocketjs_beta, android_beta.` | 使用其中一个名称。 |
| `This project has no <platform> manifest (…). Its platforms: …` | 添加 `.airbuild/airbuild.<platform>.yaml`，或选择列出的平台。 |
| `This project has a manifest for several platforms and no default airbuild.yaml; choose one with --platform: …` | 传入 `--platform`。 |
| `Two manifests are for <platform>: … Keep one.` | 删除两个文件中的一个。 |
| `<path> says platform: <x>; a file named for a platform must be for that platform.` | 修正 `platform:`，或重命名文件。 |
| `Set version to the number 2, 3 or 4.` | 修正 `version`。 |
| `Version 4 needs platform: one of …` | 添加 `platform:`。 |
| `List every app and extension Bundle ID in signing.bundles before requesting signing.` | 添加 `signing.bundles`。 |
| `Signing requires an archive: add a release: recipe whose airbuild-xcode archive writes release.artifact.path ending in .xcarchive (or end build.artifact.path in .xcarchive).` | 添加一个执行归档的 `release:` 配方。 |
| `A signed Android build runs the manifest's release recipe. Add release: …` | 在 `android_beta` 清单中添加 `release:`。 |
| `A signed Android build needs signing.package: …` | 添加 `signing.package`。 |
| `The build finished without writing build.artifact.path (…).` | 让脚本写出清单中指定的文件，或修正路径。 |
| `A runbuild execution is already running in this Team's Environment.` | 跟进它（`runbuild <id> --follow`）或取消它。 |
| `This Team's Environment is already running N runbuild executions, the limit.` | 等其中一个完成，或跟进其中一个。 |
| `No build to follow.` | 还没有运行过任何构建；先开始一次构建。 |
| `No execution is running to cancel.` | 无需操作。 |
| `Cancellation was requested, but the execution has not confirmed it stopped within 30 seconds.` | 请求仍然有效。用 `--follow` 查看。 |
| `The build's result is gone (replaced by a newer build, or the Environment restarted).` | 重新构建。 |
| `The Build succeeded, but its app information could not be saved. Build again before installing.` | 重新构建。 |

## 示例

```bash
# Everyday build of the default manifest
runbuild 3f6c1a2e-8b4d-4f0a-9c1e-2d7b5a6e9f10
```

```bash
# Signed build for TestFlight
runbuild 3f6c1a2e-8b4d-4f0a-9c1e-2d7b5a6e9f10 --sign
```

```bash
# Android build, without caches
runbuild 3f6c1a2e-8b4d-4f0a-9c1e-2d7b5a6e9f10 --platform android_beta --no-cache
```

```bash
# Pick up a build after detaching
runbuild 3f6c1a2e-8b4d-4f0a-9c1e-2d7b5a6e9f10 --follow
```
