Skip to content

Commit 73c7c8e

Browse files
authored
CI: Test on several Bazel and Ubuntu versions. (#521)
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 <[email protected]>
1 parent 9a91e38 commit 73c7c8e

File tree

1 file changed

+70
-8
lines changed

1 file changed

+70
-8
lines changed

.github/workflows/ci-build-proto.yml

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,92 @@ on:
1414

1515
jobs:
1616
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+
1830
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+
2036
steps:
21-
- uses: actions/checkout@v2
37+
- uses: actions/checkout@v4
2238
with:
2339
submodules: recursive
2440

2541
- name: Mount bazel cache
26-
uses: actions/cache@v4
42+
uses: actions/cache/restore@v4
2743
with:
2844
# See https://docs.bazel.build/versions/master/output_directories.html
2945
path: "~/.cache/bazel"
3046
# Create a new cache entry whenever Bazel files change.
3147
# 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') }}
3349
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
3558

3659
- name: Build proto/
3760
run: cd proto && bazel build //... && bazel test //...
3861

62+
- name: Build bazel/example/using-bzlmod/
63+
run: cd bazel/example/using-bzlmod && bazel build //...
64+
3965
- name: Build bazel/example/using-workspace/
4066
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' }}
4169

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

Comments
 (0)