-
Notifications
You must be signed in to change notification settings - Fork 0
232 lines (221 loc) · 11.2 KB
/
Copy pathtest.yml
File metadata and controls
232 lines (221 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# SPDX-FileCopyrightText: Copyright (c) The helly25/mbo authors (helly25.com)
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: Reusable Test Runner
permissions: read-all
on:
workflow_call:
inputs:
continue-on-error:
description: "Whether to continue on error (e.g. prevent parallel matrix operations to stop)."
type: boolean
default: false
os:
description: "The operating system the tests will be run on."
type: string
default: ubuntu-latest
compiler:
description: "The compiler to use (clang, gcc, native)."
type: string
default: clang
llvm_version:
description: "The LLVM version to use. This is only for compiler=clang and will be ignored otherwise."
type: string
default: ""
gcc_version:
description: "The GCC version to use. This is only for compiler=gcc and will be ignored otherwise (e.g. 13, 14)."
type: string
default: ""
bazel_config:
description: "The way how Bazel should operate compilation and tests, one of (asan, cpp23, fastbuild, opt)."
type: string
default: opt
bazel_version:
description: "The Bazel version to pin via `.bazelversion` (e.g. 8.7.0, 9.1.1). If empty the checked-in `.bazelversion` is used as-is."
type: string
default: ""
proto_version:
description: "The protobuf version to pin in MODULE.bazel (e.g. 34.1, 35.0). If empty the checked-in pin is used as-is."
type: string
default: ""
env:
CACHE_KEY_PREFIX: ${{inputs.os}}-bzlmod_${{inputs.compiler}}_${{inputs.compiler == 'clang' && inputs.llvm_version || ''}}_${{inputs.compiler == 'gcc' && inputs.gcc_version || ''}}_${{inputs.bazel_config}}_bazel${{inputs.bazel_version}}_proto${{inputs.proto_version}}
jobs:
test:
runs-on: ${{inputs.os}}
continue-on-error: ${{inputs.continue-on-error}}
steps:
- uses: actions/checkout@v6
- uses: bazelbuild/setup-bazelisk@v3
- name: Install GCC
if: ${{inputs.compiler == 'gcc' && inputs.gcc_version != ''}}
run: |
sudo apt update
sudo apt install gcc-${{inputs.gcc_version}} g++-${{inputs.gcc_version}}
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-${{inputs.gcc_version}} 100 --slave /usr/bin/g++ g++ /usr/bin/g++-${{inputs.gcc_version}} --slave /usr/bin/gcov gcov /usr/bin/gcov-${{inputs.gcc_version}}
sudo update-alternatives --set gcc /usr/bin/gcc-${{inputs.gcc_version}}
- uses: actions/cache/restore@v5
id: cache_restore
with:
path: "~/.cache/bazel"
key: ${{env.CACHE_KEY_PREFIX}}-${{github.ref}}-${{github.sha}}
restore-keys: |
${{env.CACHE_KEY_PREFIX}}-${{github.ref}}
${{env.CACHE_KEY_PREFIX}}-refs/heads/main
${{env.CACHE_KEY_PREFIX}}
- name: Setup
# Resolve the C++ toolchain selection and the Bazel invocation flags
# exactly ONCE, then export them via `$GITHUB_ENV` so that both
# "Show compiler" and "Build and Test" consume a single source of truth
# (no copy-pasted selection logic that can drift). The only side effects
# beyond the environment are mutating `bazelmod/llvm.MODULE.bazel` (to pin
# the requested LLVM version) and `MODULE.bazel` (to pin the requested
# protobuf version) in the checkout. `BAZEL_INIT`/`BAZEL_ARGS` are
# exported as space-separated strings; none of their elements contain
# spaces, so later steps re-split them with unquoted expansion.
run: |
set -euo pipefail
if [ "${{inputs.compiler}}:${{inputs.gcc_version}}" == "gcc:" ]; then
echo "::error::When compiler=='gcc', a valid 'gcc_version' must be given."
exit 1
fi
declare -a BAZEL_INIT=()
declare -a BAZEL_ARGS=()
CACHE_ROOT=~/.cache
BAZEL_INIT+=("--output_user_root=${CACHE_ROOT}/bazel")
BAZEL_ARGS+=("--repository_cache=${CACHE_ROOT}/bazel_repo-cache")
EXPECT=""
if [ "${{inputs.compiler}}" == "clang" ]; then
# `--config=clang` selects the hermetic toolchains_llvm toolchain via
# Bazel toolchain resolution (`--extra_toolchains`), independent of
# CC/CXX. The `EXPECT="Clang:"` assertion in "Show compiler" verifies
# the actually-selected compiler.
BAZEL_ARGS+=("--config=clang")
EXPECT="Clang:"
if [ -r "bazelmod/llvm.${{inputs.llvm_version}}.MODULE.bazel" ]; then
cp "bazelmod/llvm.${{inputs.llvm_version}}.MODULE.bazel" bazelmod/llvm.MODULE.bazel
elif [ -n "${{inputs.llvm_version}}" ]; then
CONTROL_FILE=bazelmod/llvm.MODULE.bazel
sed -e 's,llvm_version = ".*",llvm_version = "${{inputs.llvm_version}}",g' "${CONTROL_FILE}" > "${CONTROL_FILE}.new"
mv "${CONTROL_FILE}.new" "${CONTROL_FILE}"
fi
elif [ "${{inputs.compiler}}" == "gcc" ]; then
echo "CC=/usr/bin/gcc" >> "${GITHUB_ENV}"
echo "CXX=/usr/bin/g++" >> "${GITHUB_ENV}"
EXPECT="GCC:"
elif [ "${{inputs.compiler}}" == "native" ]; then
# Use whatever is found!
if [ -n "${CC:-}" ] && [ -x "$(which gcc)" ]; then
echo "CC=$(which gcc)" >> "${GITHUB_ENV}"
fi
if [ -n "${CXX:-}" ] && [ -x "$(which g++)" ]; then
echo "CXX=$(which g++)" >> "${GITHUB_ENV}"
fi
else
echo "::error::Matrix/Input var 'compiler' must be one of [clang, gcc, native]."
exit 1
fi
if [ "${{inputs.bazel_config}}" == "asan" ]; then
BAZEL_ARGS+=("-c" "dbg" "--config=asan")
elif [ "${{inputs.bazel_config}}" == "cpp23" ]; then
BAZEL_ARGS+=("-c" "opt" "--config=cpp23")
elif [ "${{inputs.bazel_config}}" == "fastbuild" ]; then
BAZEL_ARGS+=("-c" "fastbuild")
elif [ "${{inputs.bazel_config}}" == "opt" ]; then
BAZEL_ARGS+=("-c" "opt")
else
echo "::error::Matrix/Input var 'bazel_config' must be one of [asan, cpp23, fastbuild, opt]."
exit 1
fi
# Pin the protobuf dependency via a single-line rewrite of MODULE.bazel
# (no whole-file/include swapping). Mirrors the llvm_version rewrite above.
if [ -n "${{inputs.proto_version}}" ]; then
# `-E` + `|` delimiter: the protobuf line contains a comma, so a
# comma-delimited `s,,,` would misparse (`unknown option to s`).
sed -i.bak -E 's|(bazel_dep\(name = "protobuf", version = ")[^"]*|\1${{inputs.proto_version}}|' MODULE.bazel
rm -f MODULE.bazel.bak
echo "Pinned protobuf to '${{inputs.proto_version}}':"
grep 'name = "protobuf"' MODULE.bazel
fi
# Pin the Bazel release that bazelisk launches. bazelisk resolves the
# version with this precedence: `USE_BAZEL_VERSION` (env) > `.bazeliskrc`
# > `.bazelversion`. We neutralize any inherited `USE_BAZEL_VERSION` by
# exporting it empty (bazelisk treats empty as "unset") so `.bazelversion`
# is the single source of truth. When the caller passes `bazel_version`
# we overwrite `.bazelversion`; otherwise the checked-in pin stands.
echo "USE_BAZEL_VERSION=" >> "${GITHUB_ENV}"
if [ -n "${{inputs.bazel_version}}" ]; then
echo "${{inputs.bazel_version}}" > .bazelversion
fi
EXPECT_BAZEL="$(tr -d '[:space:]' < .bazelversion 2>/dev/null || true)"
echo "BAZEL_INIT=${BAZEL_INIT[*]}" >> "${GITHUB_ENV}"
echo "BAZEL_ARGS=${BAZEL_ARGS[*]}" >> "${GITHUB_ENV}"
echo "EXPECT=${EXPECT}" >> "${GITHUB_ENV}"
echo "EXPECT_BAZEL=${EXPECT_BAZEL}" >> "${GITHUB_ENV}"
echo "Resolved BAZEL_INIT: ${BAZEL_INIT[*]}"
echo "Resolved BAZEL_ARGS: ${BAZEL_ARGS[*]}"
echo "Expected compiler prefix: ${EXPECT:-<none>}"
echo "Expected Bazel version: ${EXPECT_BAZEL:-<none>}"
- name: Show compiler
# Probe the actually-selected toolchain (using the flags resolved in
# "Setup") and ASSERT it matches what the matrix asked for, so a silent
# toolchain fallback fails CI here -- with a clear message -- rather than
# producing a green build that used the wrong compiler. Also records the
# Bazel version in the log for diagnostics.
run: |
BAZEL_VERSION="$(bazel --version | awk '{print $2}')"
echo "Bazel: ${BAZEL_VERSION}"
if [ -n "${EXPECT_BAZEL}" ] && [[ "${EXPECT_BAZEL}" =~ ^[0-9] ]]; then
if [ "${BAZEL_VERSION}" != "${EXPECT_BAZEL}" ]; then
echo "::error::Expected Bazel version '${EXPECT_BAZEL}' but bazelisk launched '${BAZEL_VERSION}'."
exit 1
fi
echo "OK: Bazel version matches expected '${EXPECT_BAZEL}'."
fi
COMPILER="$(bazel ${BAZEL_INIT} run ${BAZEL_ARGS} //tools:show_compiler)"
echo "Selected compiler: ${COMPILER}"
if [ -n "${EXPECT}" ]; then
case "${COMPILER}" in
"${EXPECT}"*) echo "OK: compiler matches expected prefix '${EXPECT}'." ;;
*) echo "::error::Expected compiler prefix '${EXPECT}' but selected '${COMPILER}'."; exit 1 ;;
esac
fi
- name: Build and Test
run: |
[ -n "${CC:-}" ] && [ -x "${CC}" ] && echo "CC: $(${CC} --version)"
[ -n "${CXX:-}" ] && [ -x "${CXX}" ] && echo "CXX: $(${CXX} --version)"
echo "BAZEL_INIT: ${BAZEL_INIT}"
echo "BAZEL_ARGS: ${BAZEL_ARGS}"
bazel ${BAZEL_INIT} test ${BAZEL_ARGS} //...
- uses: actions/cache/save@v5
if: ${{!startsWith(github.ref, 'refs/tags/')}}
id: cache_save
with:
path: "~/.cache/bazel"
key: ${{env.CACHE_KEY_PREFIX}}-${{github.ref}}-${{github.sha}}
- name: Cleanup old caches
if: ${{!startsWith(github.ref, 'refs/tags/') && steps.cache_restore.outputs.cache-hit}}
run: |
set -euo pipefail
PREFIX="${{env.CACHE_KEY_PREFIX}}-${{github.ref}}"
echo "Fetching list of old caches for key prefix '${PREFIX}'..."
cacheKeyList=$(gh cache list --limit 250 --json id,key,ref,createdAt --jq "map(select(.key|startswith(\"${PREFIX}\")))|sort_by(.createdAt)|.[:-1]|.[].id")
echo "Deleting caches..."
for cacheKey in ${cacheKeyList}; do
echo "Deleting cache: '${cacheKey}'"
gh cache delete "${cacheKey}" || echo "Error deleting: '${cacheKey}'."
done
env:
GH_TOKEN: ${{secrets.CACHE_ACCESS}}
GH_REPO: ${{github.repository}}