# runmacos

> 在雲端 macOS 建置機上執行 shell 腳本（可附帶或不附帶專案原始碼），並帶回你指定的檔案。

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

`runmacos` 會從團隊的環境，在配有 Xcode 的 macOS 建置機上執行一個 Bash 腳本。它不需要 `airbuild.yaml`。凡是沙箱本身做不到的事都可以用它：查看 macOS 與 Xcode 版本、執行 `swift test`、執行 macOS App 的工具。

它**僅適用於雲端團隊**。在 Mac 團隊上會停止並顯示 `runmacos is only available in cloud Teams; local Teams provide runtest.`

## 語法

```bash
runmacos [<project-id>] [--source <directory>] [--output <relative-path>]... --command '<shell script>' [--environment ID] [--no-cache]
runmacos --follow [--run ID]    # reattach; starts nothing
runmacos --cancel [--run ID]
runmacos --list-environments
runmacos --help
```

## 選項

| 選項 | 值 | 預設 | 作用 |
| --- | --- | --- | --- |
| `--command` | shell 腳本 | 必填 | 要執行的 Bash 腳本。在上傳內容的根目錄以 `-e -o pipefail` 執行。 |
| `<project-id>` | UUID | 無 | 上傳此專案，並在其根目錄執行。 |
| `--source` | 目錄 | 無 | 改為上傳此目錄。路徑相對於你目前的目錄。兩者都指定時，上傳的是 `--source`。 |
| `--output` | 相對路徑 | 無 | 要帶回的檔案或目錄。可重複指定多個。 |
| `--environment` | `--list-environments` 中的 ID | 預設 | 在另一個建置機環境（macOS 與 Xcode 版本）上執行。 |
| `--no-cache` | — | 啟用快取 | 不使用建置機快取執行，且不儲存任何快取。 |
| `--follow` | — | — | 重新連接到進行中的執行（任何模式皆可）。若執行已結束，會告知並重播其儲存的輸出。 |
| `--run` | 執行 ID | 正在執行的那次，否則為最新的一次 | 搭配 `--follow` 或 `--cancel`。 |
| `--cancel` | — | — | 要求執行停止；等待最多 30 秒以取得確認。 |
| `--list-environments` | — | — | 列出建置機環境。 |

未指定專案 ID 或 `--source` 時，腳本會在空目錄中執行，不會上傳任何原始碼；但仍會傳送一個包含指令及其輔助工具的小型套件。`runmacos` 不接受 `--platform`、`--sign`、`--unsigned`、`--script` 或 `--additional-options`。

## 在建置機上

- `PATH` 中有 `airbuild-xcode`。一次執行中第一個 `airbuild-xcode install` 或 `test` 會還原專案的 DerivedData，並在腳本結束後儲存；`archive` 和直接使用的 `xcodebuild` 則不會取得快取。請參閱 [airbuild-xcode](https://air.build/tw/docs/cli/airbuild-xcode/)。
- `PATH` 中有 `airbuild-cache`，可在多次執行之間保留其他檔案。腳本還原的任何內容都會在腳本結束後重新儲存，每次皆然。請參閱 [airbuild-cache](https://air.build/tw/docs/cli/airbuild-cache/)。
- 已安裝 Node、npm 和 pnpm。
- 不提供任何簽署素材。

## 輸出檔案

每個 `--output` 都必須是相對於工作目錄的具體路徑：不可使用絕對路徑、`..`、萬用字元、符號連結或特殊檔案，也不可有某個路徑位於另一個路徑之內。腳本失敗時仍會帶回已存在的輸出，並以腳本本身的非零結束代碼結束。腳本通過但缺少宣告的輸出時，會以結束代碼 `65` 失敗。

## 輸出

```text
• Build started in the Environment. Run: <run-id>
• It runs to completion even if this command is interrupted (reattach with: runmacos --follow --run <run-id>).
Airbuild remote execution
… the script's output …
✓ Command succeeded in <time>.

✓ Command finished.
  Build: <build-id>
  Artifact: <path to the collected outputs>
```

未指定 `--output` 時，會輸出 `No outputs requested; skipping artifact download.`

## 同時執行多個

`runmacos` 有自己的執行槽，與 `runbuild`、`runtest` 和 `runlinux` 分開。可同時進行多個 `runmacos` 執行。開始執行時會輸出執行 ID；有多個同時執行時，`--follow` 和 `--cancel` 需要 `--run ID`（或只有其中一個使用的專案 ID）。按下 Ctrl-C 只會中斷連接。

## 結束代碼

| 代碼 | 說明 |
| --- | --- |
| `0` | 腳本通過，且所有輸出都已寫出。 |
| `1` | 執行無法開始或已遺失。 |
| `2` | 選項有誤，或缺少 `--command`；沒有執行任何作業。 |
| `65` | 某個宣告的輸出不存在或無法匯出。 |
| `130` | 已取消，或以 Ctrl-C 中斷了連接。 |
| 其他 | 腳本本身的結束代碼。 |

## 錯誤

| 訊息 | 處理方式 |
| --- | --- |
| `--command requires a nonempty shell script and is only available with runmacos and runlinux.` | 傳入腳本。 |
| `--output requires a working-directory-relative path and is only available with runmacos and runlinux.` | 提供相對路徑。 |
| `--platform takes a platform (…) and is only available with runbuild and runtest.` | 移除 `--platform`。 |
| `runmacos does not accept build or signing options.` | 移除 `--sign` / `--unsigned`。 |
| `--follow and --cancel cannot submit a command, outputs, or test options.` | 追蹤或取消時不要附上指令。 |
| `Declared output was not produced: <path>` | 讓腳本寫出該檔案，或移除該 `--output`。 |
| `The command failed and its output files could not be downloaded.` | 再執行一次；結束代碼為腳本的結束代碼。 |
| `N runmacos executions are running; choose one with --run.` | 傳入它列出的其中一個執行 ID。 |
| `This Team's Environment is already running N runmacos executions, the limit.` | 等其中一個完成，或追蹤其中一個。 |

## 範例

```bash
# What the builder has, no source uploaded
runmacos --command 'xcodebuild -version; swift --version'
```

```bash
# Run a package's tests in the Project
runmacos 3f6c1a2e-8b4d-4f0a-9c1e-2d7b5a6e9f10 --command 'swift test'
```

```bash
# Build a worktree and bring back a report
runmacos --source ../feature-branch --output build/report.txt \
  --command 'mkdir -p build && swift build 2>&1 | tee build/report.txt'
```
