# airbuild-cache

> 在多次雲端建置之間保留 node_modules 或套件儲存區等檔案，並列出或刪除團隊已儲存的快取。

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

`airbuild-cache` 可在多次雲端建置之間保留檔案。每次雲端建置都從一台全新的建置機開始；腳本用 `airbuild-cache` 還原某個目錄時，會取回上一次建置留下的副本，而 AirBuild 會在腳本結束時再次儲存它。

| 位置 | 可用功能 |
| --- | --- |
| 雲端 `runbuild`、`runtest`、`runmacos` 或 `runlinux` 腳本中 | 所有子指令。`airbuild-cache` 位於腳本的 `PATH` 中。 |
| 雲端團隊的環境中（建置之外） | `list`、`delete` 和 `hash`。 |
| Mac 團隊 | 沒有建置機快取：Mac 會自行保留檔案。`restore` 和 `save` 會輸出 `no Builder cache here; … skipped.`，且不做任何事。 |

## 語法

```bash
airbuild-cache restore --key KEY [--restore-key PREFIX]... [--shared] --path PATH...
airbuild-cache save --key KEY [--shared] --path PATH...
airbuild-cache hash FILE...
airbuild-cache list [--prefix STORED-PREFIX]
airbuild-cache delete --key STORED-KEY | --prefix STORED-PREFIX | --all
airbuild-cache --help
```

## 子指令

| 子指令 | 作用 |
| --- | --- |
| `restore` | 還原 `KEY` 的最新副本；若沒有，則還原鍵以某個 `--restore-key` 開頭的最新項目。它會記住這個鍵及其路徑：腳本結束後，不論成功或失敗，該次執行都會把這些路徑儲存為 `KEY` 的新副本，每次皆然。腳本只需要一個 `restore`。 |
| `save` | 立即將路徑儲存在 `KEY` 之下，除非 `KEY` 已存在（鍵的內容永遠不會變更）。對已被 `restore` 記住的鍵不做任何事。適用於沒有被還原過的檔案。 |
| `hash` | 輸出檔案名稱與內容的簡短摘要（16 個十六進位字元）。可用來建立在鎖定檔變更時跟著變更的鍵。不存在的檔案也會以「不存在」的狀態計入。 |
| `list` | 顯示團隊的項目，最近使用的排在最前面。 |
| `delete` | 刪除一個已儲存的鍵、某個已儲存前綴下的所有項目，或團隊的所有項目。 |

## 選項

| 選項 | 搭配 | 值 | 作用 |
| --- | --- | --- | --- |
| `--key` | `restore`、`save` | 鍵 | 鍵名：由 `A-Z a-z 0-9 . _ - + = : @` 組成，長度 1 到 200。不可為 `linux`。必填。 |
| `--key` | `delete` | 已儲存的鍵 | `list` 所顯示的鍵。 |
| `--restore-key` | `restore` | 前綴 | 備援：鍵以此開頭的最新項目。可依序重複指定多個。 |
| `--path` | `restore`、`save` | 路徑 | 要保留的檔案或目錄。可以是絕對路徑、`~/…`，或相對於目前目錄的路徑。加上引號的 `*` 會在儲存時才比對，因此可以指定尚不存在的檔案。可重複指定多個，至少一個。 |
| `--shared` | `restore`、`save` | — | 使用團隊的鍵（團隊所有專案都看得到），而非專案的鍵。 |
| `--prefix` | `list`、`delete` | 已儲存的前綴 | 只包含已儲存鍵以此開頭的項目。 |
| `--all` | `delete` | — | 團隊的所有項目。 |

`delete` 必須恰好指定 `--key`、`--prefix` 或 `--all` 其中之一。

## 鍵與儲存位置

除非傳入 `--shared`，否則鍵屬於專案。`list` 會顯示已儲存的鍵：

| 已儲存的鍵 | 內容 |
| --- | --- |
| `p/<project-id>/KEY/-<time>` | 專案的 `KEY` 副本 |
| `t/KEY/-<time>` | `--shared`（團隊）副本 |
| `p/<project-id>/linux/…`、`t/linux/…` | 同上，但儲存於 Linux 建置機 |

macOS 與 Linux 建置機會在同一個鍵下分別保存各自的項目。每個鍵只保留最新的副本：較舊的副本會在新副本上傳完成後刪除，最遲在下次還原該鍵時刪除。以 `p/<project-id>/` 作為前綴可清除整個專案的快取。

## AirBuild 自動保留的快取

以下快取不需要使用 `airbuild-cache`：

- **DerivedData（Mac）：** 不論在 `runbuild`、`runtest` 或 `runmacos` 中，一次執行的第一個 `airbuild-xcode install` 或 `test` 都會還原專案的 DerivedData，並在腳本結束後儲存。`archive` 不保留任何快取。
- **Gradle（Linux）：** `runbuild` 和 `runtest` 會自行還原 Gradle 的快取。`runlinux` 除非腳本要求，否則不還原任何內容。

在 `runbuild`、`runtest`、`runmacos` 或 `runlinux` 上使用 `--no-cache`，可在單次執行中關閉 `restore` 和 `save`。

## 輸出

`restore` 和 `save` 會在標準錯誤上回報，每次一行，以 `[airbuild-cache]` 開頭：

```text
[airbuild-cache] node-1a2b3c4d5e6f7a8b: miss.
[airbuild-cache] node-1a2b3c4d5e6f7a8b: restored (exact, 210 MB, 4.2 s).
[airbuild-cache] node-1a2b3c4d5e6f7a8b: restored (prefix from node-0f9e8d7c6b5a4f3e, 205 MB, 4.0 s).
[airbuild-cache] vendor: copied (80 MB, 0.3 s); uploads after the Build reports.
[airbuild-cache] vendor: already cached, not saved again.
[airbuild-cache] vendor: nothing to save (no listed path exists).
[airbuild-cache] caching is off for this run (--no-cache); restore skipped.
```

儲存的上傳會在建置回報結果之後才進行，因此絕不會延遲結果。

`list` 會輸出一行摘要和一個表格：

```text
Team cache: 12 entries, 3.4 GB of <quota>.
KEY                                              SIZE  LAST USED
p/<project-id>/node-1a2b3c4d5e6f7a8b/-1763460000  210 MB  2026-11-18 09:12
t/tools/-1763400000                               80 MB  2026-11-17 16:40 (uploading)
```

項目多於顯示數量時，會加上 `(the 500 most recently used; narrow with --prefix)`。`delete` 會輸出 `[airbuild-cache] deleted N entries (<size>).`

## 結束代碼與錯誤

快取問題絕不會讓腳本失敗：下載失敗、副本損壞或上傳被拒時，會輸出一行 `warning:`，而 `restore` 或 `save` 仍會以 `0` 結束。

| 代碼 | 說明 |
| --- | --- |
| `0` | 已完成，或已略過並附上訊息。 |
| `1` | `list` 或 `delete` 收到錯誤回應（`the cache answered HTTP …`）。 |
| `64` | 引數有誤；訊息會指出是哪一個。 |

| 訊息 | 處理方式 |
| --- | --- |
| `--key is required: 1-200 of A-Z a-z 0-9 . _ - + = : @.` | 傳入有效的 `--key`。 |
| `linux is reserved for the Linux Builder's entries; pick another key.` | 改用其他鍵。 |
| `name at least one --path.` | 加入 `--path`。 |
| `--restore-key needs a value and applies to restore.` | 只在 `restore` 中使用。 |
| `delete: name exactly one of --key, --prefix or --all.` | 只傳入一個選擇條件。 |
| `list takes --prefix only.` | 從 `list` 中移除 `--key` / `--all`。 |
| `hash: name at least one file.` | 傳入檔案。 |
| `no Builder cache here: list and delete work in cloud Build scripts and in a cloud Team's Environment.` | 請在雲端團隊中執行。 |
| `this Team has no Builder cache.` | 尚未儲存任何內容。 |

## 範例

```bash
# Keep node_modules, keyed on the lockfile, falling back to any older copy
airbuild-cache restore --key "node-$(airbuild-cache hash bun.lock)" --restore-key node- --path node_modules
bun install
```

```bash
# Share a package store across all of the Team's Projects
airbuild-cache restore --key pnpm-store --shared --path ~/.local/share/pnpm/store
```

```bash
# See what a Project has stored, then clear it
airbuild-cache list --prefix p/3f6c1a2e-8b4d-4f0a-9c1e-2d7b5a6e9f10/
airbuild-cache delete --prefix p/3f6c1a2e-8b4d-4f0a-9c1e-2d7b5a6e9f10/
```
