2019-06-06 18:16:48 +00:00
# `@actions/tool-cache`
> Functions necessary for downloading and caching tools.
## Usage
2019-08-04 13:00:04 +00:00
#### Download
You can use this to download tools (or other files) from a download URL:
2019-08-21 05:18:31 +00:00
```js
2019-08-04 13:00:04 +00:00
const tc = require('@actions/tool-cache');
2019-09-09 13:39:43 +00:00
const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0-linux-x64.tar.gz');
2019-08-04 13:00:04 +00:00
```
#### Extract
These can then be extracted in platform specific ways:
2019-08-21 05:18:31 +00:00
```js
2019-08-04 13:00:04 +00:00
const tc = require('@actions/tool-cache');
if (process.platform === 'win32') {
2020-02-10 03:15:26 +00:00
const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0-win-x64.zip');
2019-08-04 13:00:04 +00:00
const node12ExtractedFolder = await tc.extractZip(node12Path, 'path/to/extract/to');
2019-08-29 15:08:06 +00:00
2019-08-04 13:00:04 +00:00
// Or alternately
2020-02-10 03:15:26 +00:00
const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0-win-x64.7z');
2019-08-04 13:00:04 +00:00
const node12ExtractedFolder = await tc.extract7z(node12Path, 'path/to/extract/to');
}
else {
2019-09-09 13:39:43 +00:00
const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0-linux-x64.tar.gz');
2019-08-04 13:00:04 +00:00
const node12ExtractedFolder = await tc.extractTar(node12Path, 'path/to/extract/to');
}
```
#### Cache
2020-02-26 16:43:55 +00:00
Finally, you can cache these directories in our tool-cache. This is useful if you want to switch back and forth between versions of a tool, or save a tool between runs for self-hosted runners.
2019-08-04 13:00:04 +00:00
You'll often want to add it to the path as part of this step:
2019-08-21 05:18:31 +00:00
```js
2019-08-04 13:00:04 +00:00
const tc = require('@actions/tool-cache');
const core = require('@actions/core');
2019-09-09 13:39:43 +00:00
const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0-linux-x64.tar.gz');
2019-08-04 13:00:04 +00:00
const node12ExtractedFolder = await tc.extractTar(node12Path, 'path/to/extract/to');
const cachedPath = await tc.cacheDir(node12ExtractedFolder, 'node', '12.7.0');
core.addPath(cachedPath);
```
You can also cache files for reuse.
2019-08-21 05:18:31 +00:00
```js
2019-08-04 13:00:04 +00:00
const tc = require('@actions/tool-cache');
2019-12-11 23:05:18 +00:00
const cachedPath = await tc.cacheFile('path/to/exe', 'destFileName.exe', 'myExeName', '1.1.0');
2019-08-04 13:00:04 +00:00
```
#### Find
Finally, you can find directories and files you've previously cached:
2019-08-21 05:18:31 +00:00
```js
2019-08-04 13:00:04 +00:00
const tc = require('@actions/tool-cache');
const core = require('@actions/core');
const nodeDirectory = tc.find('node', '12.x', 'x64');
core.addPath(nodeDirectory);
```
You can even find all cached versions of a tool:
2019-08-21 05:18:31 +00:00
```js
2019-08-04 13:00:04 +00:00
const tc = require('@actions/tool-cache');
const allNodeVersions = tc.findAllVersions('node');
console.log(`Versions of node available: ${allNodeVersions}`);
```