From 5bf2ba7f853b8e0e7d67e8c04731b68443203fe1 Mon Sep 17 00:00:00 2001 From: Wiktor Garbacz Date: Thu, 23 May 2024 04:25:42 -0700 Subject: [PATCH] Add support for Bazel remote cache and use for gcp_ubuntu_bazel build PiperOrigin-RevId: 636501527 Change-Id: I9b08da20a93d960ab7784fcbd2e05bf10a632470 --- kokoro/testutils/run_bazel_tests.sh | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/kokoro/testutils/run_bazel_tests.sh b/kokoro/testutils/run_bazel_tests.sh index a22d1db..22567bf 100755 --- a/kokoro/testutils/run_bazel_tests.sh +++ b/kokoro/testutils/run_bazel_tests.sh @@ -27,13 +27,16 @@ set -eEo pipefail trap print_debug_output ERR usage() { - echo "Usage: $0 [-mh] [-b ...] [-t ...] \\" - echo " [ ...]" + echo "Usage: $0 [-mh] [-c bazel_cache_name] [-b ...] \\" + echo " [-t ...] \\" + echo " [ ...]" echo " -m: Runs only the manual targets. If set, manual targets must be" echo " provided." echo " -b: Comma separated list of flags to pass to `bazel build`." echo " -t: Comma separated list of flags to pass to `bazel test`." echo " -h: Help. Print this usage information." + echo " -c: Bazel cache to use; creadentials are expected to be in a" + echo " cache_key file." exit 1 } @@ -44,6 +47,7 @@ MANUAL_TARGETS= BAZEL_CMD="bazel" BUILD_FLAGS=() TEST_FLAGS=() +CACHE_FLAGS=() ####################################### # Process command line arguments. @@ -54,11 +58,15 @@ TEST_FLAGS=() ####################################### process_args() { # Parse options. - while getopts "mhb:t:" opt; do + while getopts "mb:t:c:" opt; do case "${opt}" in m) MANUAL_ONLY="true" ;; b) BUILD_FLAGS=($(echo "${OPTARG}" | tr ',' '\n')) ;; t) TEST_FLAGS=($(echo "${OPTARG}" | tr ',' '\n')) ;; + c) CACHE_FLAGS=( + "--remote_cache=https://storage.googleapis.com/${OPTARG}" + "--google_credentials=cache_key" + );; *) usage ;; esac done @@ -85,6 +93,8 @@ process_args() { fi readonly BAZEL_CMD echo "Using: $(which ${BAZEL_CMD})" + + readonly CACHE_FLAGS } ####################################### @@ -118,19 +128,22 @@ main() { set -x cd "${workspace_dir}" if [[ "${MANUAL_ONLY}" == "false" ]]; then - time "${BAZEL_CMD}" build "${BUILD_FLAGS[@]}" -- ... + time "${BAZEL_CMD}" build "${CACHE_FLAGS[@]}" "${BUILD_FLAGS[@]}" -- ... # Exit code 4 means targets build correctly but no tests were found. See # https://bazel.build/docs/scripts#exit-codes. bazel_test_return=0 - time "${BAZEL_CMD}" test "${TEST_FLAGS[@]}" -- ... || bazel_test_return="$?" + time "${BAZEL_CMD}" test "${CACHE_FLAGS[@]}" "${TEST_FLAGS[@]}" -- ... \ + || bazel_test_return="$?" if (( $bazel_test_return != 0 && $bazel_test_return != 4 )); then return "${bazel_test_return}" fi fi # Run specific manual targets. if (( ${#MANUAL_TARGETS[@]} > 0 )); then - time "${BAZEL_CMD}" build "${BUILD_FLAGS[@]}" -- "${MANUAL_TARGETS[@]}" - time "${BAZEL_CMD}" test "${TEST_FLAGS[@]}" -- "${MANUAL_TARGETS[@]}" + time "${BAZEL_CMD}" build "${CACHE_FLAGS[@]}" "${BUILD_FLAGS[@]}" -- \ + "${MANUAL_TARGETS[@]}" + time "${BAZEL_CMD}" test "${CACHE_FLAGS[@]}" "${TEST_FLAGS[@]}" -- \ + "${MANUAL_TARGETS[@]}" fi ) }