From 2b388bf863cf12d1de76ff491591d7a96ae7399c Mon Sep 17 00:00:00 2001 From: Steffen Smolka Date: Tue, 17 Dec 2024 17:26:05 -0800 Subject: [PATCH] CI: Test on several Bazel and Ubuntu versions. Also makes some improvements to the CI script taken from https://github.com/google/netkat. They will allow us to make better use of the cache while avoiding hitting GitHub's cache limits. Signed-off-by: Steffen Smolka Signed-off-by: smolkaj --- .github/workflows/ci-build-proto.yml | 78 +++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci-build-proto.yml b/.github/workflows/ci-build-proto.yml index 82736686..9a78b786 100644 --- a/.github/workflows/ci-build-proto.yml +++ b/.github/workflows/ci-build-proto.yml @@ -14,30 +14,92 @@ on: jobs: build: - runs-on: ubuntu-22.04 + + strategy: + matrix: + # We only test on the oldest version we want to support and latest. + # We trust that things also work for versions in the middle. + os: [ubuntu-22.04, ubuntu-latest] + # See Bazelisk README for legal values. + bazel_version: [7.x, latest] + # Don't abort other runs when one of them fails, to ease debugging. + fail-fast: false + + runs-on: ${{ matrix.os }} + env: - BAZEL: bazelisk-linux-amd64 + # This tells Bazelisk (installed as `bazel`) to use specified version. + # https://github.com/bazelbuild/bazelisk?tab=readme-ov-file#how-does-bazelisk-know-which-bazel-version-to-run + USE_BAZEL_VERSION: ${{ matrix.bazel_version }} + CACHE_KEY: ${{ matrix.os }}_bazel-${{ matrix.bazel_version }} + steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: submodules: recursive - name: Mount bazel cache - uses: actions/cache@v2 + uses: actions/cache/restore@v4 with: # See https://docs.bazel.build/versions/master/output_directories.html path: "~/.cache/bazel" # Create a new cache entry whenever Bazel files change. # See https://docs.github.com/en/actions/guides/caching-dependencies-to-speed-up-workflows - key: bazel-${{ runner.os }}-build-${{ hashFiles('**/*.bzl', '**/*.bazel') }} + key: ${{ env.CACHE_KEY }}-${{ hashFiles('**/*.bazel*', '**/*.bzl') }} restore-keys: | - bazel-${{ runner.os }}-build- + ${{ env.CACHE_KEY }} + + - name: Save start time + uses: josStorer/get-current-time@v2 + id: start-time + with: + # Unix timestamp -- seconds since 1970. + format: X - name: Build proto/ run: cd proto && bazel build //... && bazel test //... + - name: Build bazel/example/using-bzlmod/ + run: cd bazel/example/using-bzlmod && bazel build //... + - name: Build bazel/example/using-workspace/ run: cd bazel/example/using-workspace && bazel build //... + # This is a legacy example that doesn't work beyond Bazel 7.x. + if: ${{ env.USE_BAZEL_VERSION == '7.x' }} - - name: Build bazel/example/using-bzlmod/ - run: cd bazel/example/using-bzlmod && bazel build //... + - name: Save end time + # Always save the end time so we can calculate the build duration. + if: always() + uses: josStorer/get-current-time@v2 + id: end-time + with: + # Unix timestamp -- seconds since 1970. + format: X + + - name: Calculate build duration + # Always calculate the build duration so we can update the cache if needed. + if: always() + run: | + START=${{ steps.start-time.outputs.formattedTime }} + END=${{ steps.end-time.outputs.formattedTime }} + DURATION=$(( $END - $START )) + echo "duration=$DURATION" | tee "$GITHUB_ENV" + + - name: Compress cache + # Always compress the cache so we can update the cache if needed. + if: always() + run: rm -rf $(bazel info repository_cache) + + - name: Save bazel cache + uses: actions/cache/save@v4 + # Only create a new cache entry if we're on the main branch or the build takes >5mins. + # + # NOTE: Even though `always()` evaluates to true, and `true && x == x`, + # the `always() &&` prefix is not redundant! The call to `always()` has a + # side effect, which is to override the default behavior of automagically + # canceling this step if a previous step failed. + # (Don't blame me, blame GitHub Actions!) + if: always() && (github.ref_name == 'main' || env.duration > 300) + with: + path: "~/.cache/bazel" + key: ${{ env.CACHE_KEY }}-${{ hashFiles('**/*.bazel*', '**/*.bzl') }}-${{ github.run_id }}