Skip to content

Commit

Permalink
Add restore-cache input param to bazel-build-test action. (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
SanjayVas authored Mar 10, 2021
1 parent ffcb7b4 commit 6b13049
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
5 changes: 5 additions & 0 deletions bazel-build-test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ inputs:
Options to pass to the Bazel build and test commands. Newline-delimited.
required: false
default: ''
restore-cache:
description: >-
Whether to restore any previously saved Bazel workspace cache. Boolean.
required: false
default: true
runs:
using: node12
main: index.js
Expand Down
25 changes: 25 additions & 0 deletions bazel-build-test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
const core = require('@actions/core');
const exec = require('@actions/exec');

/** True values in YAML 1.2 Core Schema. */
const YAML_TRUE_VALUES = Object.freeze(['true', 'True', 'TRUE']);
/** False values in YAML 1.2 Core Schema. */
const YAML_FALSE_VALUES = Object.freeze(['false', 'False', 'FALSE']);

const workspacePath = core.getInput('workspace-path');

async function execBash(commands) {
Expand All @@ -33,7 +38,27 @@ async function execBash(commands) {
return lines.join('\n');
}

/**
* Returns the boolean value of the specified input.
*
* TODO(actions/toolkit#723): Remove this once there's an equivalent method in
* core.
*/
function getInputBool(name) {
const inputStr = core.getInput(name);
if (YAML_TRUE_VALUES.includes(inputStr)) {
return true;
}
if (YAML_FALSE_VALUES.includes(inputStr)) {
return false;
}

throw new TypeError(
`${inputStr} is not a valid YAML 1.2 Core Schema boolean`);
}

module.exports = {
workspacePath: workspacePath,
execBash: execBash,
getInputBool: getInputBool,
};
8 changes: 5 additions & 3 deletions bazel-build-test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const core = require('@actions/core');
const cache = require('@actions/cache');
const exec = require('@actions/exec');

const {execBash, workspacePath} = require('./common');
const {execBash, getInputBool, workspacePath} = require('./common');

const validTestExitCodes = [
0, // Passes.
Expand All @@ -33,9 +33,11 @@ async function run() {
const outputBase = await execBash(['bazelisk info output_base'])

const cacheKey = `bazel-${execRootHash}-${treeHash}`;
const restoreKeys = [`bazel-${execRootHash}-`];
const cachePaths = [outputBase];
await cache.restoreCache(cachePaths, cacheKey, restoreKeys);
if (getInputBool('restore-cache')) {
const restoreKeys = [`bazel-${execRootHash}-`];
await cache.restoreCache(cachePaths, cacheKey, restoreKeys);
}

const buildOptions = ['--keep_going'].concat(
core.getInput('build-options').split('\n').filter(value => value !== ''));
Expand Down

0 comments on commit 6b13049

Please sign in to comment.