啟用建置快取
Rush 始終支援增量建置分析器,讓 rush build
可以跳過自上次建置以來輸入檔案未變更的專案。您也可以透過在 custom-commands.json 中啟用 incremental
旗標來搭配自訂指令使用。我們將此稱為增量建置的「輸出保留」策略。由於建置輸出未儲存到任何地方,因此在簽出不同分支時,通常仍需要完整重建。
Rush 的建置快取透過建立每個專案建置輸出的 tar 封存檔來改善這一點。封存檔會快取,以便稍後如果 rush build
可以在快取中找到相符項目,它可以解壓縮封存檔,而不是建置該專案。這可以提供顯著的加速,例如將 30 分鐘的建置時間縮短到 30 秒。我們將此稱為增量建置的「快取還原」策略。
建置快取封存檔會儲存在兩個位置
在您本機磁碟上的快取資料夾中。這樣您就可以在不同的分支之間切換,而不會遺失您的增量建置狀態。您甚至可以設定集中式資料夾,以便在您機器上的多個登錄之間共用。預設位置是 common/temp/build-cache。
在雲端託管的儲存容器中。(選擇性)在典型的設定中,會將 CI 系統設定為寫入雲端儲存,並且會授予個別使用者唯讀存取權。例如,每次將 PR 合併到
main
分支時,CI 系統都會建置該基準,並將其上傳到雲端儲存。即使是第一次執行git clone
的使用者,他們的rush build
也會非常快速。
啟用本機磁碟快取
使用 build-cache.json 組態檔啟用建置快取功能。您可以從網站複製範本,或使用 rush init
來建立此檔案。
若要啟用基本本機磁碟快取,請加入這兩個設定
common/config/rush/build-cache.json
{
. . .
/**
* (Required) EXPERIMENTAL - Set this to true to enable the build cache feature.
*
* See https://rush.dev.org.tw/pages/maintainer/build_cache/ for details about this experimental feature.
*/
"buildCacheEnabled": true,
/**
* (Required) Choose where project build outputs will be cached.
*
* Possible values: "local-only", "azure-blob-storage", "amazon-s3"
*/
"cacheProvider": "local-only",
. . .
}
升級注意事項:此功能的早期版本是使用 experiments.json 中的
"buildCache": true
設定來啟用。這已被 build-cache.json 中的"buildCacheEnabled"
取代。
設定專案輸出資料夾
只有此變更,如果您執行 rush rebuild --verbose
,您會看到此警告
Project does not have a rush-project.json configuration file, or one provided by a rig,
so it does not support caching.
建置快取需要知道哪些資料夾應儲存在 tar 封存檔中。這些詳細資料會因工具鏈而異,因此使用 rush-project.json 組態檔針對每個專案個別設定。
例如
<您的專案>/config/rush-project.json
{
. . .
/**
* Specify the folders where your toolchain writes its output files. If enabled, the Rush build cache will
* restore these folders from the cache.
*
* The strings are folder names under the project root folder. These folders should not be tracked by Git.
* They must not contain symlinks.
*/
"projectOutputFolderNames": ["lib", "dist"]
. . .
}
設定專案輸入
預設情況下,下列輸入會併入 Rush 的快取金鑰中。換句話說,如果其中任何一項變更,則必須重建專案
專案資料夾下來源檔案的雜湊,會忽略
.gitignore
排除的任何檔案專案相依的其他工作區專案下來源檔案的雜湊
(適用於快取還原策略,但不適用於輸出保留策略)專案相依的所有外部 NPM 套件的版本,包括間接相依性
用於執行作業的 Rush 命令列參數
這些詳細資料可以使用 rush-project.json 組態檔自訂。例如,您可以包含/排除特定的 glob 模式,或指定會影響建置輸出的環境變數。建議使用 rig 套件,以避免必須將 rush-project.json 複製到每個專案資料夾中。
重要事項:請仔細設定這些設定。如果專案輸入/輸出未精確指定,則建置快取可能會產生不正確或不一致的結果。例如,還原的輸出可能遺失某些檔案。或者,它可能與完整重建所產生的輸出不同。這類問題可能難以重現和疑難排解。
如果您懷疑 Rush 建置快取可能設定錯誤,請試用 rush-audit-cache-plugin。它會在建置期間監看檔案寫入,以識別不屬於您的快取金鑰的輸入。
測試建置快取
現在您應該會看到專案已快取,如此範例記錄輸出所示
rush build --verbose
. . .
==[ example-project ]==============================================[ 1 of 5 ]==
This project was not found in the build cache.
Invoking: heft test --clean
. . .
Caching build output folders: lib
Successfully set cache entry.
"example-project" completed successfully in 11.27 seconds.
當我們第二次執行相同的指令時,Rush 會解壓縮封存檔,而不是叫用建置工作
rush build --verbose
. . .
==[ example-project ]==============================================[ 1 of 5 ]==
Build cache hit.
Clearing cached folders: lib, dist
Successfully restored output from the build cache.
example-project was restored from the build cache.
請注意,rush rebuild
不會從快取讀取,只有 rush build
會。若要在 rush rebuild
期間停用從快取寫入,請將 RUSH_BUILD_CACHE_WRITE_ALLOWED
環境變數設為 0
。
預設情況下,快取的 tar 封存檔會儲存在您的 common/temp/build-cache 資料夾下 (因此會由 rush purge
清理)。刪除這些檔案是安全的。
啟用雲端儲存
目前,cacheProvider
設定提供三種選擇
"local-only"
:無雲端儲存;封存檔只會保留在本機磁碟資料夾中"azure-blob-storage"
:Microsoft Azure Blob 儲存容器"amazon-s3"
:Amazon S3 儲存貯體
(上述提供者以 Rush 外掛程式建模。可以以相同方式實作自訂建置快取儲存提供者。)
以下示範如何設定 Azure Blob 容器的範例
common/config/rush/build-cache.json
{
. . .
/**
* (Required) EXPERIMENTAL - Set this to true to enable the build cache feature.
*
* See https://rush.dev.org.tw/pages/maintainer/build_cache/ for details about this experimental feature.
*/
"buildCacheEnabled": true,
/**
* (Required) Choose where project build outputs will be cached.
*
* Possible values: "local-only", "azure-blob-storage", "amazon-s3"
*/
"cacheProvider": "azure-blob-storage",
/**
* Use this configuration with "cacheProvider"="azure-blob-storage"
*/
"azureBlobStorageConfiguration": {
/**
* (Required) The name of the the Azure storage account to use for build cache.
*/
"storageAccountName": "example",
/**
* The name of the container in the Azure storage account to use for build cache.
*/
"storageContainerName": "my-container"
/**
* If set to true, allow writing to the cache. Defaults to false.
*/
"isCacheWriteAllowed": false
. . .
請注意,我們已設定 "isCacheWriteAllowed": false
,以防止一般使用者寫入容器。(稍後,我們會使用環境變數來為我們的 CI 工作覆寫此設定。)
使用者驗證
如果安全性不是您儲存庫的優先事項,您可以將您的儲存容器設定為允許未經身分驗證的匿名存取,以簡化使用者設定。容器會透過 HTTPS URL 存取,其中包含隨機雜湊,若沒有存取您的 Git 儲存庫,就很難猜測。這提供基本的基於隱晦的安全性。
然而,更注重安全性的組織會偏好即使是唯讀存取也需要驗證。Rush 提供 rush update-cloud-credentials 指令,讓使用者可以輕鬆設定
rush update-cloud-credentials --interactive
Rush Multi-Project Build Tool 5.45.6 (unmanaged) - https://rush.dev.org.tw
Node.js version is 12.20.1 (LTS)
Starting "rush update-cloud-credentials"
╔═════════════════════════════════════════════════════════════════════════╗
║ To sign in, use a web browser to open the page ║
║ https://microsoft.com/devicelogin and enter the code XAYBQEGRK ║
║ to authenticate. ║
╚═════════════════════════════════════════════════════════════════════════╝
認證會儲存在使用者主目錄下的 ~/.rush-user/credentials.json
中。
CI 設定
在典型的設定中,使用者具有唯讀存取權,且快取會由自動化帳戶填入;例如,CI 工作會在每次 PR 合併後建置您的 main
分支。在我們上面的範例中,"isCacheWriteAllowed": false
設定會防止使用者寫入快取。CI 工作可以透過設定 RUSH_BUILD_CACHE_WRITE_ALLOWED 環境變數,並在 RUSH_BUILD_CACHE_CREDENTIAL 環境變數中提供 CI 環境的認證來覆寫此設定。
認證
Azure 儲存體
對於 Azure Blob 儲存體,RUSH_BUILD_CACHE_CREDENTIAL
必須是一個序列化為查詢參數的 SAS 權杖。關於 SAS 權杖的詳細資訊,請參閱這篇文章。您可以透過儲存體帳戶的「設定 > 存取金鑰」頁面取得 SAS 權杖。
AWS
對於 Amazon S3,RUSH_BUILD_CACHE_CREDENTIAL
將會是您的 AWS 存取金鑰 ID 和 AWS 秘密存取金鑰,以冒號分隔,例如:<AccessKeyID>:<SecretAccessKey>
。您也可以傳遞假設 IAM 角色時需要的臨時工作階段權杖:<AccessKeyID>:<SecretAccessKey>:<SessionToken>
。
如果未設定 RUSH_BUILD_CACHE_CREDENTIAL
,建置快取會嘗試讀取環境變數 AWS_ACCESS_KEY_ID
、AWS_SECRET_ACCESS_KEY
和 AWS_SESSION_TOKEN
,這些變數通常會透過 AWS CLI 或其他 CI 工具設定。但是,如果存在 RUSH_BUILD_CACHE_CREDENTIAL
,它將始終具有優先權。
建置快取功能仍在開發中。歡迎提供意見回饋!
以下是一些相關的 GitHub 問題追蹤