From 6b13049c930b0e14ab8eea1a6bbe804f06e0605a Mon Sep 17 00:00:00 2001 From: Sanjay Vasandani Date: Wed, 10 Mar 2021 14:12:43 -0800 Subject: [PATCH] Add restore-cache input param to bazel-build-test action. (#6) --- bazel-build-test/action.yml | 5 +++++ bazel-build-test/common.js | 25 +++++++++++++++++++++++++ bazel-build-test/index.js | 8 +++++--- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/bazel-build-test/action.yml b/bazel-build-test/action.yml index 0f017c3..9baddc1 100644 --- a/bazel-build-test/action.yml +++ b/bazel-build-test/action.yml @@ -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 diff --git a/bazel-build-test/common.js b/bazel-build-test/common.js index 8975a95..9f4bbf7 100644 --- a/bazel-build-test/common.js +++ b/bazel-build-test/common.js @@ -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) { @@ -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, }; \ No newline at end of file diff --git a/bazel-build-test/index.js b/bazel-build-test/index.js index 088f537..093e6f8 100644 --- a/bazel-build-test/index.js +++ b/bazel-build-test/index.js @@ -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. @@ -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 !== ''));