|
14 | 14 |
|
15 | 15 | jobs: |
16 | 16 | build: |
17 | | - runs-on: ubuntu-22.04 |
18 | | - env: |
19 | | - BAZEL: bazelisk-linux-amd64 |
| 17 | + strategy: |
| 18 | + matrix: |
| 19 | + os: |
| 20 | + - ubuntu-22.04 |
| 21 | + - ubuntu-24.04 |
| 22 | + - ubuntu-latest |
| 23 | + runs-on: ${{ matrix.os }} |
20 | 24 | steps: |
21 | | - - uses: actions/checkout@v2 |
| 25 | + - uses: actions/checkout@v4 |
22 | 26 | with: |
23 | 27 | submodules: recursive |
24 | | - |
| 28 | + |
25 | 29 | - name: Mount bazel cache |
26 | | - uses: actions/cache@v2 |
| 30 | + uses: actions/cache/restore@v4 |
27 | 31 | with: |
28 | 32 | # See https://docs.bazel.build/versions/master/output_directories.html |
29 | 33 | path: "~/.cache/bazel" |
30 | 34 | # Create a new cache entry whenever Bazel files change. |
31 | 35 | # See https://docs.github.com/en/actions/guides/caching-dependencies-to-speed-up-workflows |
32 | | - key: bazel-${{ runner.os }}-build-${{ hashFiles('**/*.bzl', '**/*.bazel') }} |
| 36 | + key: bazel-${{ matrix.os }}-${{ hashFiles('**/*.bazel*') }} |
33 | 37 | restore-keys: | |
34 | | - bazel-${{ runner.os }}-build- |
| 38 | + bazel-${{ matrix.os }}- |
35 | 39 |
|
36 | | - - name: Install bazelisk |
37 | | - run: | |
38 | | - curl -LO "https://github.com/bazelbuild/bazelisk/releases/download/v1.15.0/$BAZEL" |
39 | | - chmod +x $BAZEL |
40 | | - sudo mv $BAZEL /usr/local/bin/bazel |
| 40 | + - name: Save start time |
| 41 | + uses: josStorer/get-current-time@v2 |
| 42 | + id: start-time |
| 43 | + with: |
| 44 | + # Unix timestamp -- seconds since 1970. |
| 45 | + format: X |
41 | 46 |
|
42 | 47 | - name: Build proto/ |
43 | 48 | run: cd proto && bazel build //... && bazel test //... |
44 | 49 |
|
45 | 50 | - name: Build bazel/example/ |
46 | 51 | run: cd bazel/example/ && bazel build //... |
| 52 | + |
| 53 | + - name: Save end time |
| 54 | + # Always save the end time so we can calculate the build duration. |
| 55 | + if: always() |
| 56 | + uses: josStorer/get-current-time@v2 |
| 57 | + id: end-time |
| 58 | + with: |
| 59 | + # Unix timestamp -- seconds since 1970. |
| 60 | + format: X |
| 61 | + |
| 62 | + - name: Calculate build duration |
| 63 | + # Always calculate the build duration so we can update the cache if needed. |
| 64 | + if: always() |
| 65 | + run: | |
| 66 | + START=${{ steps.start-time.outputs.formattedTime }} |
| 67 | + END=${{ steps.end-time.outputs.formattedTime }} |
| 68 | + DURATION=$(( $END - $START )) |
| 69 | + echo "duration=$DURATION" | tee "$GITHUB_ENV" |
| 70 | +
|
| 71 | + - name: Compress cache |
| 72 | + # Always compress the cache so we can update the cache if needed. |
| 73 | + if: always() |
| 74 | + run: rm -rf $(bazel info repository_cache) |
| 75 | + |
| 76 | + - name: Save bazel cache |
| 77 | + uses: actions/cache/save@v4 |
| 78 | + # Only create a new cache entry if we're on the main branch or the build takes >5mins. |
| 79 | + # |
| 80 | + # NOTE: Even though `always()` evaluates to true, and `true && x == x`, |
| 81 | + # the `always() &&` prefix is not redundant! The call to `always()` has a |
| 82 | + # side effect, which is to override the default behavior of automagically |
| 83 | + # canceling this step if a previous step failed. |
| 84 | + # (Don't blame me, blame GitHub Actions!) |
| 85 | + if: always() && (github.ref_name == 'main' || env.duration > 300) |
| 86 | + with: |
| 87 | + path: "~/.cache/bazel" |
| 88 | + key: bazel-${{ matrix.os }}-${{ hashFiles('**/*.bazel*') }}-${{ github.run_id }} |
0 commit comments