# airbuild-xcode

> 在 AirBuild 建置腳本中執行 xcodebuild install、archive 或 test，並套用 AirBuild 對組態、目的地、簽署與快取的預設值。

Source: https://air.build/tw/docs/cli/airbuild-xcode/

`airbuild-xcode` 會以一個動作呼叫一次 `xcodebuild`，並為你沒有傳入的部分加上 AirBuild 的預設值。它只存在於 `runbuild`、`runtest` 和 `runmacos` 執行的腳本中（包括這些腳本呼叫的腳本），位於雲端 macOS 建置機或 Mac 團隊的 Mac 上。其他地方都沒有安裝，Linux 建置機上也沒有。

執行 `install`、`archive`、`test` 和 `-showBuildSettings` 時，請用它取代 `xcodebuild`。其他 Xcode 操作請直接呼叫 `xcodebuild`。

## 語法

```bash
airbuild-xcode install|archive|test|-showBuildSettings [xcodebuild options and build settings]
airbuild-xcode --help
```

動作必須放在最前面。其他所有引數都會原封不動地傳給 `xcodebuild`，`airbuild-xcode` 並以 `xcodebuild` 的結束代碼結束。

## 動作

| 動作 | 用途 | 快取 |
| --- | --- | --- |
| `install` | 日常建置：`xcodebuild install` 會以增量方式將 scheme 的 Archive 目標建置到 `DSTROOT`。請放在 `build.script` 中。 | 在雲端建置中，一次執行的第一個 `install` 會還原專案的 DerivedData。 |
| `archive` | 用來製作已簽署 `.ipa` 或未簽署裝置套件的封存檔。請放在 `release.script` 中。 | 無：封存每次執行都會從頭開始。 |
| `test` | 執行測試。 | 在雲端建置中，一次執行的第一個 `test` 會還原專案的 DerivedData（與 `install` 的是不同的副本）。 |
| `-showBuildSettings` | 以建置時相同的預設值輸出建置設定。 | — |

簽署建置與裝置建置會拒絕 `install`：它們匯出的是封存檔，因此會執行 `release:` 配方。

## 預設值

`install`、`archive` 和 `-showBuildSettings` 會依照 `runbuild` 設定的 `AIRBUILD_SIGN`（預設 `0`）：

| 建置 | 組態 | 目的地 | 簽署 |
| --- | --- | --- | --- |
| 一般（`AIRBUILD_SIGN=0`） | `Debug` | `generic/platform=iOS Simulator` | `CODE_SIGN_IDENTITY=-`；保留專案的其他簽署設定 |
| 裝置套件（`AIRBUILD_SIGN=0`、`AIRBUILD_DEVICE=1`）：`runbuild --unsigned`，或未使用代管簽署的 **安裝到裝置** | `Release` | `generic/platform=iOS` | 以專案的 entitlements 進行 ad-hoc 簽署，不使用團隊或描述檔。模擬器的 `-destination` 和 `-sdk` 值會被移除。 |
| 簽署（`AIRBUILD_SIGN=1`）：`runbuild --sign` | `Release` | `generic/platform=iOS` | 由 AirBuild 準備的代管簽署 |

`test` 預設使用 iPhone 模擬器，執行環境為含有一般（非折疊）iPhone 的最新 iOS 版本，並優先選擇已開機的那台；另外加上 `CODE_SIGN_IDENTITY=-` 和 `-collect-test-diagnostics never`。它不會建立模擬器。

## 可以覆寫的項目

- 明確指定的 `-configuration`、`-destination`（測試時可指定多個），以及一般建置與測試時的 `CODE_SIGN_IDENTITY=…`，會取代預設值。
- `-collect-test-diagnostics on-failure` 會為 `test` 重新開啟模擬器診斷資料收集。
- 命令列上的 `ARCHS`、`COMPILATION_CACHE_*`、`SWIFT_ENABLE_PREFIX_MAPPING` 和 `CLANG_ENABLE_PREFIX_MAPPING` 優先於建置機的平台設定。在建置機上，除非你傳入 `ARCHS`，否則模擬器與 macOS 建置只會產生 arm64。
- 代管簽署會覆寫專案、`-xcconfig` 和命令列中的簽署設定：`CODE_SIGNING_ALLOWED`、`CODE_SIGNING_REQUIRED`、`CODE_SIGN_IDENTITY`、`CODE_SIGN_STYLE`、`DEVELOPMENT_TEAM`、`PROVISIONING_PROFILE`、`PROVISIONING_PROFILE_SPECIFIER` 和 `OTHER_CODE_SIGN_FLAGS`。`CODE_SIGN_ENTITLEMENTS` 會被保留。

## 版本號碼

由使用者從 AirBuild App 啟動的裝置封存或代管簽署封存，會被提高到專案的最低版本：只有在原始碼中的值較低時，才會覆寫 `MARKETING_VERSION` 和 `CURRENT_PROJECT_VERSION`。由 Agent 啟動的建置則一律不會：請自行在專案中設定建置編號。

## 輸出

在執行 `xcodebuild` 之前，`airbuild-xcode` 會將它選擇的設定輸出到標準錯誤：

```text
[airbuild-xcode] sign=0; configuration=Debug (default); destination=generic/platform=iOS\ Simulator (default).
[airbuild-xcode] CODE_SIGN_IDENTITY=default (-); project signing settings are preserved.
```

```text
[airbuild-xcode] default test device: iPhone 17 (iOS 26.1).
airbuild-xcode: skipping simulator diagnostics collection (simctl diagnose waits 600 s on Xcode 27); pass -collect-test-diagnostics on-failure to collect them.
[airbuild-xcode] action=test; destination=id=<UDID> (default); CODE_SIGN_IDENTITY=default (-).
```

你可能還會看到以下幾行：

| 輸出行 | 說明 |
| --- | --- |
| `[airbuild-xcode] archive starts over every run and keeps no cache; …` | 一般建置正在執行封存。若要增量建置，請在 `build:` 中使用 `install`，並把 `archive` 留在 `release:`。`runbuild` 會在結果旁以警告重複提示。 |
| `[airbuild-xcode] device build: dropped -destination …` | 已從裝置建置中移除模擬器目的地或 SDK。 |
| `[airbuild-xcode] device build: ad-hoc signed, no team or profile; CODE_SIGN_ENTITLEMENTS is preserved.` | 裝置套件建置。 |
| `[airbuild-xcode] Managed signing overrides via XCODE_XCCONFIG_FILE: …` | 簽署建置。 |
| `[airbuild-xcode] command-line ARCHS=… kept over the platform build settings.` | 你的設定優先於建置機的設定。 |
| `[airbuild-xcode] version floor: raised …` / `nothing raised.` | 從 App 啟動的建置所執行的最低版本步驟。 |

## 結束代碼與錯誤

`airbuild-xcode` 會以 `xcodebuild` 的結束代碼結束。在下列情況下，它會在執行 `xcodebuild` 之前以 `64` 停止：

| 訊息 | 處理方式 |
| --- | --- |
| `airbuild-xcode: start with install, archive, test or -showBuildSettings; use xcodebuild directly for other operations.` | 將動作放在最前面，或改呼叫 `xcodebuild`。 |
| `airbuild-xcode: use one leading action; run other Xcode operations separately.` | 移除 `-list`、`-version`、`-show…`、`-exportArchive`、`-resolvePackageDependencies` 等；改用 `xcodebuild` 執行它們。（允許使用 `-showBuildTimingSummary`。） |
| `airbuild-xcode: -configuration requires a value.`（或 `-destination`） | 提供值。 |
| `airbuild-xcode: a signed or device build exports an archive; put airbuild-xcode archive in the release: recipe of airbuild.yaml …` | 加入執行 `airbuild-xcode archive` 的 `release:` 配方。 |
| `airbuild-xcode: no available iOS simulator could be selected. Install an iOS simulator runtime and device in Xcode, or pass -destination explicitly.` | 傳入 `-destination`，或安裝模擬器執行環境。 |
| `airbuild-xcode: AIRBUILD_SIGN must be 0 or 1. Start managed signing through AirBuild.` | 不要自行設定 `AIRBUILD_SIGN`；請使用 `runbuild --sign`。 |
| `airbuild-xcode: signing preparation is incomplete. Start signing through AirBuild again.` | 再次執行 `runbuild --sign`。 |

## 範例

```bash
# build.script: the everyday, incremental build
airbuild-xcode install -workspace App.xcworkspace -scheme App \
  DSTROOT="$PWD/.airbuild/artifacts/install"
```

```bash
# release.script: the archive for --sign and --unsigned
rm -rf .airbuild/artifacts/App.xcarchive
airbuild-xcode archive -workspace App.xcworkspace -scheme App \
  -archivePath .airbuild/artifacts/App.xcarchive
```

```bash
# A test entry, with runtest's --additional-options
airbuild-xcode test -workspace App.xcworkspace -scheme App \
  -resultBundlePath .airbuild/artifacts/Tests.xcresult $additional_options
```
