|
14 | 14 |
|
15 | 15 | jobs: |
16 | 16 | build: |
17 | | - runs-on: ubuntu-22.04 |
| 17 | + |
| 18 | + strategy: |
| 19 | + matrix: |
| 20 | + # We only test on the oldest version we want to support and latest. |
| 21 | + # We trust that things also work for versions in the middle. |
| 22 | + os: [ubuntu-22.04, ubuntu-latest] |
| 23 | + # See Bazelisk README for legal values. |
| 24 | + bazel_version: [7.x, latest] |
| 25 | + # Don't abort other runs when one of them fails, to ease debugging. |
| 26 | + fail-fast: false |
| 27 | + |
| 28 | + runs-on: ${{ matrix.os }} |
| 29 | + |
18 | 30 | env: |
19 | | - BAZEL: bazelisk-linux-amd64 |
| 31 | + # This tells Bazelisk (installed as `bazel`) to use specified version. |
| 32 | + # https://github.com/bazelbuild/bazelisk?tab=readme-ov-file#how-does-bazelisk-know-which-bazel-version-to-run |
| 33 | + USE_BAZEL_VERSION: ${{ matrix.bazel_version }} |
| 34 | + CACHE_KEY: ${{ matrix.os }}_bazel-${{ matrix.bazel_version }} |
| 35 | + |
20 | 36 | steps: |
21 | | - - uses: actions/checkout@v2 |
| 37 | + - uses: actions/checkout@v4 |
22 | 38 | with: |
23 | 39 | submodules: recursive |
24 | 40 |
|
25 | 41 | - name: Mount bazel cache |
26 | | - uses: actions/cache@v4 |
| 42 | + uses: actions/cache/restore@v4 |
27 | 43 | with: |
28 | 44 | # See https://docs.bazel.build/versions/master/output_directories.html |
29 | 45 | path: "~/.cache/bazel" |
30 | 46 | # Create a new cache entry whenever Bazel files change. |
31 | 47 | # See https://docs.github.com/en/actions/guides/caching-dependencies-to-speed-up-workflows |
32 | | - key: bazel-${{ runner.os }}-build-${{ hashFiles('**/*.bzl', '**/*.bazel') }} |
| 48 | + key: ${{ env.CACHE_KEY }}-${{ hashFiles('**/*.bazel*', '**/*.bzl') }} |
33 | 49 | restore-keys: | |
34 | | - bazel-${{ runner.os }}-build- |
| 50 | + ${{ env.CACHE_KEY }} |
| 51 | +
|
| 52 | + - name: Save start time |
| 53 | + uses: josStorer/get-current-time@v2 |
| 54 | + id: start-time |
| 55 | + with: |
| 56 | + # Unix timestamp -- seconds since 1970. |
| 57 | + format: X |
35 | 58 |
|
36 | 59 | - name: Build proto/ |
37 | 60 | run: cd proto && bazel build //... && bazel test //... |
38 | 61 |
|
| 62 | + - name: Build bazel/example/using-bzlmod/ |
| 63 | + run: cd bazel/example/using-bzlmod && bazel build //... |
| 64 | + |
39 | 65 | - name: Build bazel/example/using-workspace/ |
40 | 66 | run: cd bazel/example/using-workspace && bazel build //... |
| 67 | + # This is a legacy example that doesn't work beyond Bazel 7.x. |
| 68 | + if: ${{ env.USE_BAZEL_VERSION == '7.x' }} |
41 | 69 |
|
42 | | - - name: Build bazel/example/using-bzlmod/ |
43 | | - run: cd bazel/example/using-bzlmod && bazel build //... |
| 70 | + - name: Save end time |
| 71 | + # Always save the end time so we can calculate the build duration. |
| 72 | + if: always() |
| 73 | + uses: josStorer/get-current-time@v2 |
| 74 | + id: end-time |
| 75 | + with: |
| 76 | + # Unix timestamp -- seconds since 1970. |
| 77 | + format: X |
| 78 | + |
| 79 | + - name: Calculate build duration |
| 80 | + # Always calculate the build duration so we can update the cache if needed. |
| 81 | + if: always() |
| 82 | + run: | |
| 83 | + START=${{ steps.start-time.outputs.formattedTime }} |
| 84 | + END=${{ steps.end-time.outputs.formattedTime }} |
| 85 | + DURATION=$(( $END - $START )) |
| 86 | + echo "duration=$DURATION" | tee "$GITHUB_ENV" |
| 87 | +
|
| 88 | + - name: Compress cache |
| 89 | + # Always compress the cache so we can update the cache if needed. |
| 90 | + if: always() |
| 91 | + run: rm -rf $(bazel info repository_cache) |
| 92 | + |
| 93 | + - name: Save bazel cache |
| 94 | + uses: actions/cache/save@v4 |
| 95 | + # Only create a new cache entry if we're on the main branch or the build takes >3mins. |
| 96 | + # |
| 97 | + # NOTE: Even though `always()` evaluates to true, and `true && x == x`, |
| 98 | + # the `always() &&` prefix is not redundant! The call to `always()` has a |
| 99 | + # side effect, which is to override the default behavior of automagically |
| 100 | + # canceling this step if a previous step failed. |
| 101 | + # (Don't blame me, blame GitHub Actions!) |
| 102 | + if: always() && (github.ref_name == 'main' || env.duration > 180) |
| 103 | + with: |
| 104 | + path: "~/.cache/bazel" |
| 105 | + key: ${{ env.CACHE_KEY }}-${{ hashFiles('**/*.bazel*', '**/*.bzl') }}-${{ github.run_id }} |
0 commit comments