# runlinux

> 在配有 JDK 21 與 Android SDK 的雲端 Linux 建置機上執行 shell 腳本（例如 Gradle 任務），並帶回你指定的檔案。

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

`runlinux` 會從團隊的環境，在 Linux 建置機上執行一個 Bash 腳本。它不需要 `airbuild.yaml`。它是 [runmacos](https://air.build/tw/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/tw/docs/cli/airbuild-cache/) 還原：

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

腳本還原的任何內容都會在腳本結束後重新儲存，每次皆然，並在結果傳回後上傳。Linux 與 macOS 建置機會在同一個鍵下分別保存各自的項目。

## 輸出檔案與結果

`--output` 路徑遵循與 [runmacos](https://air.build/tw/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"'
```
