# runlinux

> 在装有 JDK 21 和 Android SDK 的云端 Linux 构建机上运行 shell 脚本（例如 Gradle 任务），并取回你指定的文件。

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

`runlinux` 从团队的环境中，在一台 Linux 构建机上运行一个 Bash 脚本。它不需要 `airbuild.yaml`。它是 [runmacos](https://air.build/cn/docs/cli/runmacos/) 的 Linux 版本：选项相同，但有自己的槽位。

构建机装有 JDK 21、Android SDK 和预热好的 `~/.gradle`，适合运行 `./gradlew lint`、`./gradlew dependencies` 等 Gradle 任务，以及沙盒里没有的其他 Linux 工具。它**没有 Node 和 Bun**：请在脚本中安装，或者改用带有 Node、npm 和 pnpm 的 `runmacos`。这里也没有 `airbuild-xcode`。

它**仅适用于云端团队**。在 Mac 团队中会停止并输出 `runlinux is only available in cloud Teams; local Teams provide runtest.`

要构建或测试 Android App，请使用 `runbuild --platform android_beta` 和 `runtest --platform android_beta`；其他事情才用 `runlinux`。

## 用法

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

## 选项

| 选项 | 取值 | 默认值 | 作用 |
| --- | --- | --- | --- |
| `--command` | shell 脚本 | 必填 | 要运行的 Bash 脚本，在上传内容的根目录以 `-e -o pipefail` 运行。 |
| `<project-id>` | UUID | 无 | 上传这个项目，并在其根目录运行。 |
| `--source` | 目录 | 无 | 改为上传这个目录。路径相对于当前目录。两者都给时，上传的是 `--source`。 |
| `--output` | 相对路径 | 无 | 要取回的文件或目录。可重复多次。 |
| `--environment` | `--list-environments` 中的 ID | 第一个 Linux 环境 | 在另一个 Linux 环境上运行。其他平台的环境会被拒绝。 |
| `--no-cache` | — | 启用缓存 | 本次运行关闭 `airbuild-cache restore` 和 `save`。 |
| `--follow` | — | — | 重新连接到正在进行的运行。如果运行已结束，会说明这一点并重放保存的输出。 |
| `--run` | 运行 ID | 正在运行的那次，否则为最新一次 | 与 `--follow` 或 `--cancel` 同用。 |
| `--cancel` | — | — | 请求运行停止；最多等待 30 秒确认。 |
| `--list-environments` | — | — | 列出构建机环境。 |

不给项目 ID 也不给 `--source` 时，脚本在一个空目录中运行，不上传任何源代码。

## 缓存

除了预热好的 `~/.gradle`，不会自动缓存任何东西。要在多次运行之间保留文件（`node_modules`、包存储、Gradle 的构建缓存），请在脚本中用 [airbuild-cache](https://air.build/cn/docs/cli/airbuild-cache/) 恢复：

```bash
airbuild-cache restore --key node --path node_modules
```

脚本恢复的内容在每次结束后都会重新保存，并在结果取回之后上传。Linux 和 macOS 构建机在同一个键下保存各自独立的条目。

## 输出文件和结果

`--output` 路径遵循与 [runmacos](https://air.build/cn/docs/cli/runmacos/) 相同的规则：具体的相对路径，不能包含 `..`、通配符、符号链接，也不能有重叠的路径。脚本失败时仍会取回已存在的输出，并以脚本的退出码退出。运行的结尾也相同：

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

## 退出码

| 退出码 | 含义 |
| --- | --- |
| `0` | 脚本通过，且每个输出都已写出。 |
| `1` | 运行无法启动或已丢失。 |
| `2` | 选项有误，或缺少 `--command`；什么也没有运行。 |
| `65` | 某个声明的输出缺失或无法导出。 |
| `130` | 已取消，或用 Ctrl-C 断开了连接。 |
| 其他 | 脚本自己的退出码。 |

## 错误

| 消息 | 怎么办 |
| --- | --- |
| `--command requires a nonempty shell script and is only available with runmacos and runlinux.` | 传入一个脚本。 |
| `--platform takes a platform (…) and is only available with runbuild and runtest.` | 去掉 `--platform`。 |
| `runlinux does not accept build or signing options.` | 去掉 `--sign` / `--unsigned`。 |
| `Declared output was not produced: <path>` | 让脚本写出它，或去掉对应的 `--output`。 |
| `N runlinux executions are running; choose one with --run.` | 传入它列出的某个运行 ID。 |
| `This Team's Environment is already running N runlinux executions, the limit.` | 等其中一个完成，或跟进其中一个。 |

脚本如果没有安装就调用 `node` 或 `bun`，会以 `command not found` 失败（退出码 127）。

## 示例

```bash
# List the app's dependencies and bring the list back
runlinux 3f6c1a2e-8b4d-4f0a-9c1e-2d7b5a6e9f10 \
  --command './gradlew dependencies > dependencies.txt' --output dependencies.txt
```

```bash
# Keep a downloaded tools directory between runs
runlinux 3f6c1a2e-8b4d-4f0a-9c1e-2d7b5a6e9f10 --command '
  airbuild-cache restore --key tools --path .tools
  ./scripts/fetch-tools.sh .tools'
```

```bash
# What the builder has
runlinux --command 'java -version; ls "$ANDROID_HOME"'
```
