@actions/tool-cache

中文文档
4.0.0发布于 2026/01/30MITTypeScriptES Module构建来源已验证近 30 天下载 249.5万

动作工具缓存 lib

中文使用文档

@actions/tool-cache

下载和缓存工具所需的功能.

使用量

下载

您可以使用它从下载 URL 下载工具( 或其他文件) :

const tc = require('@actions/tool-cache');

const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0-linux-x64.tar.gz');

摘录

然后可以具体的方式从平台中提取:

const tc = require('@actions/tool-cache');

if (process.platform === 'win32') {
  const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0-win-x64.zip');
  const node12ExtractedFolder = await tc.extractZip(node12Path, 'path/to/extract/to');

  // Or alternately
  const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0-win-x64.7z');
  const node12ExtractedFolder = await tc.extract7z(node12Path, 'path/to/extract/to');
}
else if (process.platform === 'darwin') {
  const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0.pkg');
  const node12ExtractedFolder = await tc.extractXar(node12Path, 'path/to/extract/to');
}
else {
  const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0-linux-x64.tar.gz');
  const node12ExtractedFolder = await tc.extractTar(node12Path, 'path/to/extract/to');
}

缓存

最后,你可以把这些目录隐藏在我们的工具笼中. 如果您想要在工具的版本之间前后切换, 或者在运行之间保存一个工具, 供自行托管的跑取者使用, 这将会有用 .

作为这个步骤的一部分,你常常想把它添加到路径中:

const tc = require('@actions/tool-cache');
const core = require('@actions/core');

const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0-linux-x64.tar.gz');
const node12ExtractedFolder = await tc.extractTar(node12Path, 'path/to/extract/to');

const cachedPath = await tc.cacheDir(node12ExtractedFolder, 'node', '12.7.0');
core.addPath(cachedPath);

也可以缓存文件进行再利用.

const tc = require('@actions/tool-cache');

const cachedPath = await tc.cacheFile('path/to/exe', 'destFileName.exe', 'myExeName', '1.1.0');

查找

最后,您可以找到您先前缓存过的目录和文件:

const tc = require('@actions/tool-cache');
const core = require('@actions/core');

const nodeDirectory = tc.find('node', '12.x', 'x64');
core.addPath(nodeDirectory);

您甚至可以找到工具的所有缓存版本 :

const tc = require('@actions/tool-cache');

const allNodeVersions = tc.findAllVersions('node');
console.log(`Versions of node available: ${allNodeVersions}`);

版本与发布记录

@actions/tool-cache 共发布 28 个版本,首次发布于 2019/08/08;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.0.0latest2026/01/309 个月前
3.0.12026/01/289 个月前
3.0.02026/01/089 个月前
2.0.22025/01/161.8 年前
2.0.12022/05/134 年前
2.0.02022/05/134 年前

其他发布通道:1.5-preview → 1.5.4。

关于 @actions/tool-cache 的常见问题

@actions/tool-cache 是什么?

动作工具缓存 lib

如何安装 @actions/tool-cache?

在项目目录运行 npm install @actions/tool-cache,或使用 pnpm add / yarn add / bun add。

@actions/tool-cache 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@actions/tool-cache 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@actions/tool-cache 可以商用吗?

@actions/tool-cache 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@actions/tool-cache 的下载量有多少?

根据 npm 官方下载统计,@actions/tool-cache 在 2026/10/05 – 2026/11/03 这 30 天内被下载 2,495,269 次,日均约 83,176 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@actions/tool-cache 还在维护吗?

最新版本 4.0.0 发布于 2026/01/30(9 个月前);至今共发布 28 个版本,其中过去 12 个月发布了 3 个;首次发布于 2019/08/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。