Skip to content

Commit 3a265fb

Browse files
authored
ci: deflake macOS builds with CMake (#9306)
I searched for places where there is an explicit or implicit download, and wrap them with a retry loop. This motivated me to refactor the code to install vcpkg. Incidentally, my previous changes to use GCS for the vcpkg cache may have increased flakiness: instead of one download for the vcpkg binary caches it performs N (currently 14). This is fixed in this change too.
1 parent 655e7f4 commit 3a265fb

File tree

3 files changed

+74
-47
lines changed

3 files changed

+74
-47
lines changed

ci/kokoro/lib/vcpkg.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright 2022 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Make our include guard clean against set -o nounset.
18+
test -n "${CI_KOKORO_LIB_VCPKG_SH__:-}" || declare -i CI_KOKORO_LIB_VCPKG_SH__=0
19+
if ((CI_KOKORO_LIB_VCPKG_SH__++ != 0)); then
20+
return 0
21+
fi # include guard
22+
23+
source module /ci/lib/io.sh
24+
source module /ci/kokoro/lib/cache.sh
25+
source module /ci/kokoro/lib/gcloud.sh
26+
27+
install_vcpkg() {
28+
local -r vcpkg_dir="$1"
29+
30+
mkdir -p "${vcpkg_dir}"
31+
io::log "Downloading vcpkg into ${vcpkg_dir}..."
32+
VCPKG_COMMIT="$(<ci/etc/vcpkg-commit.txt)"
33+
ci/retry-command.sh 3 120 curl -sSL "https://github.com/microsoft/vcpkg/archive/${VCPKG_COMMIT}.tar.gz" |
34+
tar -C "${vcpkg_dir}" --strip-components=1 -zxf -
35+
36+
io::log_h2 "Configure VCPKG to use GCS as a cache"
37+
readonly CACHE_BUCKET="${GOOGLE_CLOUD_CPP_KOKORO_RESULTS:-cloud-cpp-kokoro-results}"
38+
readonly CACHE_FOLDER="${CACHE_BUCKET}/build-cache/google-cloud-cpp/vcpkg/macos"
39+
export VCPKG_BINARY_SOURCES="x-gcs,gs://${CACHE_BUCKET}/${CACHE_FOLDER},readwrite"
40+
41+
create_gcloud_config
42+
activate_service_account_keyfile "${CACHE_KEYFILE}"
43+
export CLOUDSDK_ACTIVE_CONFIG_NAME="${GCLOUD_CONFIG}"
44+
io::run gsutil ls "gs://${CACHE_BUCKET}/"
45+
# Eventually we can remove this, but the current caches contain both `ccache`
46+
# files (which we want), and `vcpkg` files (which we don't want).
47+
io::run rm -fr "${HOME}/.cache/vcpkg"
48+
49+
ci/retry-command.sh 3 120 "${vcpkg_dir}/bootstrap-vcpkg.sh"
50+
"${vcpkg_dir}/vcpkg" --feature-flags=-manifests remove --outdated --recurse
51+
}

ci/kokoro/macos/builds/cmake-vcpkg.sh

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,26 @@ set -euo pipefail
1919
source "$(dirname "$0")/../../../lib/init.sh"
2020
source module ci/etc/integration-tests-config.sh
2121
source module ci/lib/io.sh
22+
source module ci/kokoro/lib/vcpkg.sh
2223

2324
readonly SOURCE_DIR="."
2425
readonly BINARY_DIR="cmake-out/macos-vcpkg"
2526

2627
NCPU="$(sysctl -n hw.logicalcpu)"
2728
readonly NCPU
2829

29-
io::log_h2 "Using CMake version"
30-
cmake --version
31-
3230
io::log_h2 "Update or install dependencies"
3331
brew list --versions openssl || ci/retry-command.sh 3 120 brew install openssl
3432
brew list --versions ccache || ci/retry-command.sh 3 120 brew install ccache
3533
brew list --versions cmake || ci/retry-command.sh 3 120 brew install cmake
3634
brew list --versions ninja || ci/retry-command.sh 3 120 brew install ninja
3735

36+
io::log_h2 "Using CMake version"
37+
cmake --version
38+
3839
# Fetch vcpkg at the specified hash.
3940
vcpkg_dir="${HOME}/vcpkg-quickstart"
40-
mkdir -p "${vcpkg_dir}"
41-
io::log "Downloading vcpkg into ${vcpkg_dir}..."
42-
VCPKG_COMMIT="$(<ci/etc/vcpkg-commit.txt)"
43-
ci/retry-command.sh 3 120 curl -sSL "https://github.com/microsoft/vcpkg/archive/${VCPKG_COMMIT}.tar.gz" |
44-
tar -C "${vcpkg_dir}" --strip-components=1 -zxf -
45-
(
46-
cd "${vcpkg_dir}"
47-
env VCPKG_ROOT="${vcpkg_dir}" CC="ccache cc" CXX="ccache c++" \
48-
"${PROJECT_ROOT}/ci/retry-command.sh" 3 120 ./bootstrap-vcpkg.sh
49-
./vcpkg remove --outdated --recurse
50-
)
41+
install_vcpkg "${vcpkg_dir}"
5142

5243
io::log_h2 "ccache stats"
5344
ccache --show-stats
@@ -60,23 +51,28 @@ export OPENSSL_ROOT_DIR="${homebrew_prefix}/opt/openssl"
6051

6152
cmake_flags=(
6253
"-DCMAKE_TOOLCHAIN_FILE=${vcpkg_dir}/scripts/buildsystems/vcpkg.cmake"
63-
"-DCMAKE_INSTALL_PREFIX=$HOME/staging"
54+
"-DCMAKE_INSTALL_PREFIX=${HOME}/staging"
6455
"-DGOOGLE_CLOUD_CPP_ENABLE_CCACHE=ON"
6556
)
6657

58+
# The downloads can fail, therefore require a retry loop.
59+
io::log_h2 "Download and compile dependencies using vcpkg"
60+
ci/retry-command.sh 3 120 \
61+
"${vcpkg_dir}/vcpkg" install --x-install-root="${BINARY_DIR}/vcpkg_installed"
62+
6763
io::log_h2 "Configure CMake"
6864
export NINJA_STATUS="T+%es [%f/%t] "
69-
cmake -GNinja "-H${SOURCE_DIR}" "-B${BINARY_DIR}" "${cmake_flags[@]}"
65+
cmake -GNinja -S "${SOURCE_DIR}" -B "${BINARY_DIR}" "${cmake_flags[@]}"
7066

71-
io::log_h2 "Compiling with ${NCPU} cpus"
72-
cmake --build "${BINARY_DIR}" -- -j "${NCPU}"
67+
io::log_h2 "Compiling using all CPUs"
68+
cmake --build "${BINARY_DIR}"
7369

7470
io::log_h2 "Running unit tests"
7571
ctest_args=(
7672
# Print the full output on failures
7773
--output-on-failure
7874
# Run many tests in parallel, use -j for compatibility with old versions
79-
-j "$(nproc)"
75+
-j "${NCPU}"
8076
# Make the output shorter on interactive tests
8177
--progress
8278
)

ci/kokoro/macos/builds/quickstart-cmake.sh

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ source "$(dirname "$0")/../../../lib/init.sh"
2020
source module ci/lib/io.sh
2121
source module ci/etc/integration-tests-config.sh
2222
source module ci/etc/quickstart-config.sh
23-
source module ci/kokoro/lib/gcloud.sh
24-
source module ci/kokoro/lib/cache.sh
23+
source module ci/kokoro/lib/vcpkg.sh
2524

2625
io::log_h2 "Using CMake version"
2726
cmake --version
@@ -32,32 +31,13 @@ brew list --versions pkg-config || brew install pkg-config
3231

3332
# Fetch vcpkg at the specified hash.
3433
vcpkg_dir="${HOME}/vcpkg-quickstart"
35-
mkdir -p "${vcpkg_dir}"
36-
io::log "Downloading vcpkg into ${vcpkg_dir}..."
37-
VCPKG_COMMIT="$(<ci/etc/vcpkg-commit.txt)"
38-
39-
ci/retry-command.sh 3 120 curl -sSL "https://github.com/microsoft/vcpkg/archive/${VCPKG_COMMIT}.tar.gz" |
40-
tar -C "${vcpkg_dir}" --strip-components=1 -zxf -
41-
42-
io::log_h2 "Configure VCPKG to use GCS as a cache"
43-
readonly CACHE_BUCKET="${GOOGLE_CLOUD_CPP_KOKORO_RESULTS:-cloud-cpp-kokoro-results}"
44-
readonly CACHE_FOLDER="${CACHE_BUCKET}/build-cache/google-cloud-cpp/vcpkg/macos"
45-
export VCPKG_BINARY_SOURCES="x-gcs,gs://${CACHE_BUCKET}/${CACHE_FOLDER},readwrite"
46-
47-
create_gcloud_config
48-
activate_service_account_keyfile "${CACHE_KEYFILE}"
49-
export CLOUDSDK_ACTIVE_CONFIG_NAME="${GCLOUD_CONFIG}"
50-
io::run gsutil ls "gs://${CACHE_BUCKET}/"
51-
# Eventually we can remove this, but the current caches contain both `ccache`
52-
# files (which we want), and `vcpkg` files (which we don't want).
53-
io::run rm -fr "${HOME}/.cache/vcpkg"
34+
install_vcpkg "${vcpkg_dir}"
5435

36+
# The downloads can fail, therefore require a retry loop.
5537
(
5638
cd "${vcpkg_dir}"
57-
env VCPKG_ROOT="${vcpkg_dir}" CC="ccache cc" CXX="ccache c++" \
58-
"${PROJECT_ROOT}/ci/retry-command.sh" 3 120 ./bootstrap-vcpkg.sh
59-
./vcpkg remove --outdated --recurse
60-
./vcpkg install google-cloud-cpp
39+
"${PROJECT_ROOT}/ci/retry-command.sh" 3 120 \
40+
"${vcpkg_dir}/vcpkg" "--feature-flags=-manifests" install google-cloud-cpp
6141
)
6242

6343
export NINJA_STATUS="T+%es [%f/%t] "
@@ -76,17 +56,17 @@ build_quickstart() {
7656

7757
io::log_h2 "Configure CMake for ${library}'s quickstart"
7858
"${cmake}" "-GNinja" "-DCMAKE_MAKE_PROGRAM=${ninja}" \
79-
"-H${source_dir}" "-B${binary_dir}" "${cmake_flags[@]}"
59+
-S "${source_dir}" -B "${binary_dir}" "${cmake_flags[@]}"
8060

8161
echo
8262
io::log_h2 "Compiling ${library}'s quickstart"
8363
"${cmake}" --build "${binary_dir}"
8464
}
8565

8666
io::log_h2 "Fetch vcpkg tools"
87-
env VCPKG_ROOT="${vcpkg_dir}" "${PROJECT_ROOT}/ci/retry-command.sh" 3 120 \
67+
ci/retry-command.sh 3 120 \
8868
"${vcpkg_dir}/vcpkg" "--feature-flags=-manifests" fetch cmake
89-
env VCPKG_ROOT="${vcpkg_dir}" "${PROJECT_ROOT}/ci/retry-command.sh" 3 120 \
69+
ci/retry-command.sh 3 120 \
9070
"${vcpkg_dir}/vcpkg" "--feature-flags=-manifests" fetch ninja
9171

9272
errors=""

0 commit comments

Comments
 (0)