# runmacos

> 在云端 macOS 构建机上运行 shell 脚本，可带或不带项目源代码，并取回你指定的文件。

Source: https://air.build/cn/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/cn/docs/cli/airbuild-xcode/)。
- `PATH` 中有 `airbuild-cache`，用于在多次运行之间保留其他文件。脚本恢复的内容在每次结束后都会重新保存。见 [airbuild-cache](https://air.build/cn/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'
```
