# runbuild

> 依照專案的 airbuild.yaml 建置：日常建置、簽署建置，或未簽署的裝置安裝套件。

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

`runbuild` 會執行專案 `airbuild.yaml` 中的建置腳本來建置專案，然後把腳本建置出的成果帶回來。它在團隊的環境（也就是 Agent 工作的地方）中執行。

| 團隊 | 建置執行的位置 |
| --- | --- |
| 雲端團隊 | 專案會上傳到 macOS 建置機（`ios`）或 Linux 建置機（`android_beta`）。`pocketjs_beta` 則在團隊自己的主機上建置。 |
| Mac 團隊 | 在團隊自己的 Mac 上，使用該 Mac 的 Xcode。`android_beta` 只能在雲端團隊中建置。 |

`runbuild` 從不安裝 App。建置完成時，它會輸出用來安裝並開啟 App 的 `simulator` 指令；請自行在你選擇的裝置上執行（請參閱 [simulator](https://air.build/tw/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 工作區或專案及 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/tw/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/tw/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.`

安裝新版本前，請先停止執行中的 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` 時也是如此。 |
| `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
```
