|
| 1 | +name: Bazel Build |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + pull_request: |
| 6 | + |
| 7 | +jobs: |
| 8 | + build: |
| 9 | + |
| 10 | + strategy: |
| 11 | + matrix: |
| 12 | + # We only test on the oldest version we want to support and latest. |
| 13 | + # We trust that things also work for versions in the middle. |
| 14 | + os: [ubuntu-22.04, ubuntu-latest] |
| 15 | + # See Bazelisk README for legal values. |
| 16 | + bazel_version: [7.x, latest] |
| 17 | + # Don't abort other runs when one of them fails, to ease debugging. |
| 18 | + fail-fast: false |
| 19 | + |
| 20 | + runs-on: ${{ matrix.os }} |
| 21 | + |
| 22 | + env: |
| 23 | + # This tells Bazelisk (installed as `bazel`) to use specified version. |
| 24 | + # https://github.com/bazelbuild/bazelisk?tab=readme-ov-file#how-does-bazelisk-know-which-bazel-version-to-run |
| 25 | + USE_BAZEL_VERSION: ${{ matrix.bazel_version }} |
| 26 | + CACHE_KEY: bazel-${{ matrix.bazel_version }} |
| 27 | + |
| 28 | + steps: |
| 29 | + - uses: actions/checkout@v4 |
| 30 | + |
| 31 | + - name: Mount bazel cache |
| 32 | + uses: actions/cache/restore@v4 |
| 33 | + with: |
| 34 | + path: "~/.cache/bazel" |
| 35 | + key: ${{ env.CACHE_KEY }}-${{ hashFiles('**/*.bazel*', '**/*.bzl') }} |
| 36 | + restore-keys: | |
| 37 | + ${{ env.CACHE_KEY }} |
| 38 | +
|
| 39 | + - name: Save start time |
| 40 | + uses: josStorer/get-current-time@v2 |
| 41 | + id: start-time |
| 42 | + with: |
| 43 | + format: X # https://momentjs.com/docs/#/displaying/format/ |
| 44 | + |
| 45 | + - run: bazel build //... |
| 46 | + |
| 47 | + - name: Save end time |
| 48 | + # Needed to save cache regardless of build failures. |
| 49 | + if: always() |
| 50 | + uses: josStorer/get-current-time@v2 |
| 51 | + id: end-time |
| 52 | + with: |
| 53 | + format: X # https://momentjs.com/docs/#/displaying/format/ |
| 54 | + |
| 55 | + - name: Calculate build duration |
| 56 | + # Needed to save cache regardless of build failures. |
| 57 | + if: always() |
| 58 | + run: | |
| 59 | + START=${{ steps.start-time.outputs.formattedTime }} |
| 60 | + END=${{ steps.end-time.outputs.formattedTime }} |
| 61 | + DURATION=$(( $END - $START )) |
| 62 | + echo "duration=$DURATION" | tee "$GITHUB_ENV" |
| 63 | +
|
| 64 | + - name: Compress cache |
| 65 | + # Needed to save cache regardless of build failures. |
| 66 | + if: always() |
| 67 | + run: rm -rf $(bazel info repository_cache) |
| 68 | + |
| 69 | + - name: Save bazel cache |
| 70 | + uses: actions/cache/save@v4 |
| 71 | + # Create new cache entry if on master branch or build takes >3mins. |
| 72 | + if: always() && (github.ref_name == 'master' || env.duration > 180) |
| 73 | + with: |
| 74 | + path: "~/.cache/bazel" |
| 75 | + key: ${{ env.CACHE_KEY }}-${{ hashFiles('**/*.bazel*', '**/*.bzl') }}-${{ github.run_id }} |
0 commit comments