Skip to content

Commit

Permalink
Save Bazel workspace cache only on specified workflow events. (#15)
Browse files Browse the repository at this point in the history
This is a mitigation for cache entries that are close to the GitHub Actions cache size limit. In particular, this can avoid the case where the cache entry for the default branch gets evicted by saving a cache entry for a pull request. That case can lead to cache misses, as GitHub actions only restores cache entries from the current branch or the default branch.
  • Loading branch information
SanjayVas authored Sep 29, 2021
1 parent 12b48ab commit 4fab592
Show file tree
Hide file tree
Showing 407 changed files with 65,385 additions and 8,362 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/test-bazel-build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ jobs:
uses: ./bazel-build-test
with:
workspace-path: .
cache-save-events: |
pull_request
push
build-options:
name: Run with build options
runs-on: ubuntu-20.04
Expand All @@ -91,6 +94,9 @@ jobs:
uses: ./bazel-build-test
with:
workspace-path: .
cache-save-events: |
pull_request
push
build-options: |
--show_result=1024
--test_summary=detailed
Expand Down Expand Up @@ -118,6 +124,9 @@ jobs:
uses: ./bazel-build-test
with:
workspace-path: .
cache-save-events: |
pull_request
push
target-patterns: |
passing_test_1
passing_test_2
10 changes: 9 additions & 1 deletion bazel-build-test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ inputs:
description: Version of the GitHub Actions cache key.
required: false
default: 0
cache-save-events:
# TODO(actions/toolkit#184): Use list type rather than newline-delimited
# string once it's available.
description: >-
Workflow trigger event names which the Bazel workspace cache is saved.
Newline-delimited.
required: false
default: push
build-options:
# TODO(actions/toolkit#184): Use list type rather than newline-delimited
# string once it's available.
Expand All @@ -43,4 +51,4 @@ runs:
using: node12
main: index.js
post: upload-testlogs.js
post-if: failure()
post-if: failure()
12 changes: 7 additions & 5 deletions bazel-build-test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
const core = require('@actions/core');
const cache = require('@actions/cache');
const exec = require('@actions/exec');
const github = require('@actions/github');

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

Expand All @@ -40,11 +41,10 @@ async function run() {
await cache.restoreCache(cachePaths, cacheKey, restoreKeys);
}

const buildOptions = ['--keep_going'].concat(
core.getInput('build-options').split('\n').filter(value => value !== ''));
const buildOptions =
['--keep_going'].concat(core.getMultilineInput('build-options'));
const testOptions = ['--test_output=errors'].concat(buildOptions);
const targetPatterns =
core.getInput('target-patterns').split('\n').filter(value => value !== '');
const targetPatterns = core.getMultilineInput('target-patterns');

await exec.exec(
'bazelisk', ['build'].concat(buildOptions).concat(targetPatterns),
Expand All @@ -56,7 +56,9 @@ async function run() {
throw new Error('Testing failed');
}

if (targetPatterns.includes("//...")) {
const cacheSaveEvents = core.getMultilineInput('cache-save-events');
if (targetPatterns.includes('//...') &&
cacheSaveEvents.includes(github.context.eventName)) {
try {
await cache.saveCache(cachePaths, cacheKey);
} catch (err) {
Expand Down
11 changes: 5 additions & 6 deletions bazel-build-test/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bazel-build-test",
"version": "1.0.0",
"version": "1.5.0",
"description": "Javascript GitHub Action for building and testing with Bazel",
"main": "index.js",
"scripts": {
Expand All @@ -9,10 +9,9 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@actions/artifact": "^0.5.0",
"@actions/cache": "^1.0.6",
"@actions/core": "^1.2.6",
"@actions/exec": "^1.0.4",
"@actions/glob": "^0.1.1"
"@actions/cache": "^1.0.7",
"@actions/core": "^1.5.0",
"@actions/exec": "^1.1.0",
"@actions/github": "^5.0.0"
}
}
1 change: 0 additions & 1 deletion node_modules/.bin/rimraf

This file was deleted.

Loading

0 comments on commit 4fab592

Please sign in to comment.