Rush StackShopBlogEvents
跳至主要内容

启用构建缓存

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.node.org.cn/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 配置文件为每个项目单独配置。

例如

<your-project>/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.node.org.cn/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.node.org.cn
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 设置

在典型的配置中,用户具有只读访问权限,并且缓存由自动化帐户填充; 例如,每次合并 PR 后都会构建您的 main 分支的 CI 作业。 在上面的示例中,"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_IDAWS_SECRET_ACCESS_KEYAWS_SESSION_TOKEN,这些变量通常通过 AWS CLI 或其他 CI 工具设置。但是,如果存在 RUSH_BUILD_CACHE_CREDENTIAL,它将始终优先。

构建缓存功能仍在开发中。欢迎反馈!

一些相关的 GitHub 问题