From ce302c9175230a74277fbe2c258c8fa00d414d92 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Tue, 11 Jun 2024 19:59:25 +0200 Subject: [PATCH] Simplify CI setup and use prebuilt protoc (#90) --- .bazelrc | 9 +++++++++ .github/workflows/ci.bazelrc | 15 -------------- .github/workflows/ci.yaml | 39 ++++++------------------------------ .gitignore | 1 + MODULE.bazel | 36 ++++++++++++++++++++++++++++++++- docs/BUILD.bazel | 15 ++++++++++++++ examples/.bazelrc | 6 ++++++ tools/extensions.bzl | 11 ++++++++++ 8 files changed, 83 insertions(+), 49 deletions(-) delete mode 100644 .github/workflows/ci.bazelrc create mode 100644 tools/extensions.bzl diff --git a/.bazelrc b/.bazelrc index 0b41f01..03ffb33 100644 --- a/.bazelrc +++ b/.bazelrc @@ -13,6 +13,15 @@ common --check_direct_dependencies=off # Improve build caching by redacting environment variables. common --incompatible_strict_action_env +# Avoid building protoc from source. +common --incompatible_enable_proto_toolchain_resolution + +# Debug where options came from +common:ci --announce_rc +# Don't rely on test logs being easily accessible from the test runner, +# though it makes the log noisier. +common:ci --test_output=errors + # Load any settings specific to the current user. # .bazelrc.user should appear in .gitignore so that settings are not shared with team members # This needs to be last statement in this diff --git a/.github/workflows/ci.bazelrc b/.github/workflows/ci.bazelrc deleted file mode 100644 index 3b4aad2..0000000 --- a/.github/workflows/ci.bazelrc +++ /dev/null @@ -1,15 +0,0 @@ -# This file contains Bazel settings to apply on CI only. -# It is referenced with a --bazelrc option in the call to bazel in ci.yaml - -# Debug where options came from -build --announce_rc -# This directory is configured in GitHub actions to be persisted between runs. -# We do not enable the repository cache to cache downloaded external artifacts -# as these are generally faster to download again than to fetch them from the -# GitHub actions cache. -build --disk_cache=~/.cache/bazel -# Don't rely on test logs being easily accessible from the test runner, -# though it makes the log noisier. -test --test_output=errors -# Allows tests to run bazelisk-in-bazel, since this is the cache folder used -test --test_env=XDG_CACHE_HOME diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 279d5ea..9c6fbd7 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -26,42 +26,15 @@ jobs: matrix: os: [ubuntu-20.04, macos-12, windows-2022] bazelversion: [6.4.0, last_rc, last_green] - folder: [".", examples] - exclude: - - os: windows-2022 - folder: "." - - os: macos-12 - folder: "." - - bazelversion: last_green - folder: "." - - bazelversion: 6.4.0 + folder: [examples] + include: + - os: ubuntu-20.04 + bazelversion: last_rc folder: "." steps: - uses: actions/checkout@v4 - # Cache build artifacts so that the next ci build is incremental. - # Because github action caches cannot be updated after a build, we need to - # store the contents of each build in a unique cache key, then fall back to loading - # it on the next ci run. We use hashFiles(...) in the key and restore-keys- with - # the prefix to load the most recent cache for the branch on a cache miss. You - # should customize the contents of hashFiles to capture any bazel input sources, - # although this doesn't need to be perfect. If none of the input sources change - # then a cache hit will load an existing cache and bazel won't have to do any work. - # In the case of a cache miss, you want the fallback cache to contain most of the - # previously built artifacts to minimize build time. The more precise you are with - # hashFiles sources the less work bazel will have to do. - # We do not cache downloaded external artifacts as these are generally - # faster to download again than to fetch them from the GitHub actions - # cache. - - name: Mount bazel caches - uses: actions/cache@v4 - with: - path: | - ~/.cache/bazel - key: bazel-cache-${{ hashFiles('**/BUILD.bazel', '**/*.bzl', '**/MODULE.bazel', '**/.bazelrc') }} - restore-keys: bazel-cache- - - name: Configure Bazel version working-directory: ${{ matrix.folder }} run: echo "${{ matrix.bazelversion }}" > .bazelversion @@ -76,9 +49,9 @@ jobs: - name: bazel test //... working-directory: ${{ matrix.folder }} - run: bazel --bazelrc=${{ github.workspace }}/.github/workflows/ci.bazelrc --bazelrc=.bazelrc test //... + run: bazel test --config=ci //... - name: bazel coverage //... if: matrix.os == 'ubuntu-20.04' && matrix.folder == 'examples' working-directory: ${{ matrix.folder }} - run: bazel --bazelrc=${{ github.workspace }}/.github/workflows/ci.bazelrc --bazelrc=.bazelrc coverage //... + run: bazel coverage --config=ci //... diff --git a/.gitignore b/.gitignore index 52646d3..24f3a57 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ bazel-* .bazelrc.user .idea/ .ijwb/ +MODULE.bazel.lock diff --git a/MODULE.bazel b/MODULE.bazel index fff7db0..ebf76a0 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -12,4 +12,38 @@ bazel_dep(name = "buildifier_prebuilt", version = "6.1.0", dev_dependency = True bazel_dep(name = "gazelle", version = "0.33.0", dev_dependency = True, repo_name = "bazel_gazelle") bazel_dep(name = "platforms", version = "0.0.6", dev_dependency = True) bazel_dep(name = "rules_testing", version = "0.4.0", dev_dependency = True) -bazel_dep(name = "stardoc", version = "0.6.2", dev_dependency = True) +bazel_dep(name = "stardoc", dev_dependency = True) +git_override( + module_name = "stardoc", + commit = "3baa5d1761970c6285d2ac9c3adccfaac42f54c5", + remote = "https://github.com/bazelbuild/stardoc.git", +) + +bazel_dep(name = "rules_java", version = "7.6.1", dev_dependency = True) +bazel_dep(name = "rules_proto", version = "6.0.0", dev_dependency = True) +bazel_dep(name = "toolchains_protoc", version = "0.3.0", dev_dependency = True) + +protoc = use_extension( + "@toolchains_protoc//protoc:extensions.bzl", + "protoc", + dev_dependency = True, +) +protoc.toolchain( + # Creates a repository to satisfy well-known-types dependencies such as + # deps=["@com_google_protobuf//:any_proto"] + google_protobuf = "com_google_protobuf", + # Pin to any version of protoc + version = "v26.0", +) + +protobuf_java_repos = use_extension( + "//tools:extensions.bzl", + "protobuf_java_repo", + dev_dependency = True, +) +use_repo(protobuf_java_repos, "protobuf_java") + +register_toolchains( + "//docs/...", + dev_dependency = True, +) diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index 9aca974..6af6a4b 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -1,6 +1,7 @@ # This load statement must be in the docs/ package rather than anything users depend on # so that the dependency on stardoc doesn't leak to them. load("@aspect_bazel_lib//lib:docs.bzl", "stardoc_with_diff_test", "update_docs") +load("@rules_proto//proto:proto_lang_toolchain.bzl", "proto_lang_toolchain") stardoc_with_diff_test( name = "defs", @@ -8,3 +9,17 @@ stardoc_with_diff_test( ) update_docs(name = "update") + +# Precompiled proto toolchain for stardoc +proto_lang_toolchain( + name = "protoc_java_toolchain", + command_line = "--java_out=$(OUT)", + progress_message = "Generating java_proto_library %{label}", + runtime = "@protobuf_java//jar", +) + +toolchain( + name = "protoc_java_toolchain.registration", + toolchain = ":protoc_java_toolchain", + toolchain_type = "@rules_java//java/proto:toolchain_type", +) diff --git a/examples/.bazelrc b/examples/.bazelrc index 057c6b4..0610445 100644 --- a/examples/.bazelrc +++ b/examples/.bazelrc @@ -25,3 +25,9 @@ common:windows --noincompatible_strict_action_env common:windows --test_env=PATH # Allow build to start before all external deps have been fetched. common --experimental_merged_skyframe_analysis_execution + +# Debug where options came from +common:ci --announce_rc +# Don't rely on test logs being easily accessible from the test runner, +# though it makes the log noisier. +common:ci --test_output=errors diff --git a/tools/extensions.bzl b/tools/extensions.bzl new file mode 100644 index 0000000..3a131d9 --- /dev/null +++ b/tools/extensions.bzl @@ -0,0 +1,11 @@ +load("@bazel_skylib//lib:modules.bzl", "modules") +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_jar") + +def _protobuf_java_repo(): + http_jar( + name = "protobuf_java", + urls = ["https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/4.26.1/protobuf-java-4.26.1.jar"], + integrity = "sha256-CRkz5YcK+BB0gyb3rOSmc6ynISUxd1QoQvBEtUbxQoI=", + ) + +protobuf_java_repo = modules.as_extension(_protobuf_java_repo)