|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright 2025 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 | +set -euo pipefail |
| 18 | + |
| 19 | +source "$(dirname "$0")/../../lib/init.sh" |
| 20 | +source module ci/cloudbuild/builds/lib/bazel.sh |
| 21 | +source module ci/cloudbuild/builds/lib/cloudcxxrc.sh |
| 22 | +source module ci/cloudbuild/builds/lib/integration.sh |
| 23 | + |
| 24 | +export CC=gcc |
| 25 | +export CXX=g++ |
| 26 | + |
| 27 | +# Explicitly list the patterns that match hand-crafted code. Excluding the |
| 28 | +# generated code results in a longer list and more maintenance. |
| 29 | +instrumented_patterns=( |
| 30 | + "/google/cloud/bigquery_unified[/:]" |
| 31 | +) |
| 32 | +instrumentation_filter="$(printf ",%s" "${instrumented_patterns[@]}")" |
| 33 | +instrumentation_filter="${instrumentation_filter:1}" |
| 34 | + |
| 35 | +mapfile -t args < <(bazel::common_args) |
| 36 | +args+=("--instrumentation_filter=${instrumentation_filter}") |
| 37 | +args+=("--instrument_test_targets") |
| 38 | +# This is a workaround for: |
| 39 | +# https://github.com/googleapis/google-cloud-cpp/issues/6952 |
| 40 | +# Based on the recommendations from: |
| 41 | +# https://github.com/bazelbuild/bazel/issues/3236 |
| 42 | +args+=("--sandbox_tmpfs_path=/tmp") |
| 43 | +io::log_h2 "Running coverage on non-integration tests." |
| 44 | +bazel coverage "${args[@]}" --test_tag_filters=-integration-test "${BAZEL_TARGETS[@]}" |
| 45 | + |
| 46 | +mapfile -t integration_args < <(integration::bazel_args) |
| 47 | +# With code coverage the `--flaky_test_attempts` flag works in unexpected ways. |
| 48 | +# A flake produces an empty `coverage.dat` file, which breaks the build when |
| 49 | +# trying to consolidate all the `coverage.dat` files. |
| 50 | +# |
| 51 | +# This combination of a "successful" test (because the flake is retried) and a |
| 52 | +# failure in the consolidation of coverage results, which happens much later |
| 53 | +# in the build, easily leads the developer astray. |
| 54 | +i=0 |
| 55 | +for arg in "${integration_args[@]}"; do |
| 56 | + case "${arg}" in |
| 57 | + --flaky_test_attempts=*) |
| 58 | + unset "integration_args[$i]" |
| 59 | + ;; |
| 60 | + esac |
| 61 | + i=$((++i)) |
| 62 | +done |
| 63 | +integration::bazel_with_emulators coverage "${args[@]}" "${integration_args[@]}" |
| 64 | + |
| 65 | +# Where does this token come from? For triggered ci/pr builds GCB will securely |
| 66 | +# inject this into the environment. See the "secretEnv" setting in the |
| 67 | +# cloudbuild.yaml file. The value is stored in Secret Manager. You can store |
| 68 | +# your own token in your personal project's Secret Manager so that your |
| 69 | +# personal builds have coverage data uploaded to your own account. See also |
| 70 | +# https://cloud.google.com/build/docs/securing-builds/use-secrets |
| 71 | +if [[ -z "${CODECOV_TOKEN:-}" ]]; then |
| 72 | + io::log_h2 "No codecov token. Skipping upload." |
| 73 | + exit 0 |
| 74 | +fi |
| 75 | + |
| 76 | +# Merges the coverage.dat files, which reduces the overall size by about 90%. |
| 77 | +readonly MERGED_COVERAGE="/var/tmp/merged-coverage.lcov" |
| 78 | +io::log_h2 "Merging coverage data into ${MERGED_COVERAGE}" |
| 79 | +TIMEFORMAT="==> 🕑 merging done in %R seconds" |
| 80 | +time { |
| 81 | + mapfile -t coverage_dat < <(find "$(bazel info output_path)" -name "coverage.dat") |
| 82 | + io::log "Found ${#coverage_dat[@]} coverage.dat files" |
| 83 | + mapfile -t lcov_flags < <(printf -- "--add-tracefile=%s\n" "${coverage_dat[@]}") |
| 84 | + lcov --quiet --ignore-errors negative "${lcov_flags[@]}" --output-file "${MERGED_COVERAGE}" |
| 85 | + ls -lh "${MERGED_COVERAGE}" |
| 86 | +} |
| 87 | + |
| 88 | +codecov_args=( |
| 89 | + "--filter=gcov" |
| 90 | + "--file=${MERGED_COVERAGE}" |
| 91 | + "--branch=${BRANCH_NAME}" |
| 92 | + "--sha=${COMMIT_SHA}" |
| 93 | + "--pr=${PR_NUMBER:-}" |
| 94 | + "--build=${BUILD_ID:-}" |
| 95 | + "--verbose" |
| 96 | +) |
| 97 | +io::log_h2 "Uploading ${MERGED_COVERAGE} to codecov.io" |
| 98 | +io::log "Flags: ${codecov_args[*]}" |
| 99 | +TIMEFORMAT="==> 🕑 codecov.io upload done in %R seconds" |
| 100 | +time { |
| 101 | + # Downloads and verifies the codecov uploader before executing it. |
| 102 | + codecov="$(mktemp -u -t codecov.XXXXXXXXXX)" |
| 103 | + curl -fsSL -o "${codecov}" https://github.com/codecov/uploader/releases/download/v0.6.3/codecov-linux |
| 104 | + sha256sum="e6aa8429d6ff91eddc7eced927e6ec936364a88fe755eed28b1f627a6499980d" |
| 105 | + if ! sha256sum -c <(echo "${sha256sum} *${codecov}"); then |
| 106 | + io::log_h2 "ERROR: Invalid sha256sum for codecov program" |
| 107 | + exit 1 |
| 108 | + fi |
| 109 | + chmod +x "${codecov}" |
| 110 | + env -i HOME="${HOME}" "${codecov}" --token="${CODECOV_TOKEN}" "${codecov_args[@]}" |
| 111 | + rm "${codecov}" |
| 112 | +} |
0 commit comments