Skip to content

Commit

Permalink
Use tool cache rather than worklow dependency cache for linters. (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
SanjayVas authored Jan 11, 2023
1 parent 9ef4cf5 commit b333769
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 36 deletions.
3 changes: 1 addition & 2 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 26 additions & 29 deletions setup-linters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

const cache = require('@actions/cache');
const core = require('@actions/core');
const crypto = require('crypto');
const fs = require('fs');
Expand Down Expand Up @@ -67,51 +66,48 @@ function buildTool(name, version, sha256, ext = '') {

async save() {
await this.validate();
try {
const cacheId = await cache.saveCache([this.path], this.cacheKey);
core.info(`Saved ${this.cacheKey} to cache`);
return cacheId;
} catch (err) {
if (err.name === cache.ReserveCacheError.name) {
core.warning(err);
} else {
throw err;
}
}
const cacheId =
await tc.cacheFile(this.path, this.basename, this.name, this.version);
core.info(`Saved ${this.cacheKey} to tool cache`);
return cacheId;
},

async restore() {
const restoredKey = await cache.restoreCache([this.path], this.cacheKey);
if (!restoredKey) {
return restoredKey;
const restoredPath = tc.find(this.name, this.version);
if (!restoredPath) {
return false;
}
await io.cp(restoredPath, this.path);

try {
await this.validate();
} catch (err) {
core.warning(err);
await io.rmRF(this.path);
return undefined;
return false;
}
core.info(`Restored ${this.cacheKey} from cache`);
return restoredKey;
core.info(`Restored ${this.cacheKey} from tool cache`);
return true;
},
};
}

function sha256HashFile(path) {
async function sha256HashFile(path) {
const hash = crypto.createHash('sha256').setEncoding('hex');
// TODO(actions/runner#772): Switch to using fs Promises API to create read
// stream once GitHub's JS actions can be run with Node.js 16+.
const input = fs.createReadStream(path);
return new Promise((resolve, reject) => {
input.on('end', () => {
hash.end();
resolve(hash.read());
const inputHandle = await fsPromises.open(path);
try {
const input = inputHandle.createReadStream();
return await new Promise((resolve, reject) => {
input.on('end', () => {
hash.end();
resolve(hash.read());
});
input.on('error', reject);
input.pipe(hash);
});
input.on('error', reject);
input.pipe(hash);
});
} finally {
await inputHandle.close();
}
}

async function installExecutable(tool, url) {
Expand All @@ -133,6 +129,7 @@ async function installExecutableFromArchive(tool, url) {
}

// Download archive to temporary directory.
core.info(`Downloading ${tool.name}`);
const tmpdir =
await fsPromises.mkdtemp(path.join(process.env.RUNNER_TEMP, tool.name));
const archiveName = tool.basename + '.tar.gz';
Expand Down
3 changes: 1 addition & 2 deletions setup-linters/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "setup-linters",
"version": "1.8.0",
"version": "1.8.2",
"description": "Javascript GitHub Action to set up linters.",
"main": "index.js",
"scripts": {
Expand All @@ -9,7 +9,6 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@actions/cache": "^3.0.6",
"@actions/core": "^1.10.0",
"@actions/io": "^1.1.2",
"@actions/tool-cache": "^2.0.1"
Expand Down

0 comments on commit b333769

Please sign in to comment.