|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright 2021 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 | +# http://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 -eu |
| 18 | + |
| 19 | +source "$(dirname "$0")/../../lib/init.sh" |
| 20 | +source module ci/cloudbuild/builds/lib/bazel.sh |
| 21 | +source module ci/cloudbuild/builds/lib/integration.sh |
| 22 | + |
| 23 | +export CC=gcc |
| 24 | +export CXX=g++ |
| 25 | + |
| 26 | +mapfile -t args < <(bazel::common_args) |
| 27 | +bazel coverage "${args[@]}" --test_tag_filters=-integration-test ... |
| 28 | +mapfile -t integration_args < <(integration::args) |
| 29 | +integration::bazel_with_emulators coverage "${args[@]}" "${integration_args[@]}" |
| 30 | + |
| 31 | +# Where does this token come from? For triggered ci/pr builds GCB will securely |
| 32 | +# inject this into the environment. See the "secretEnv" setting in the |
| 33 | +# cloudbuild.yaml file. The value is stored in Secret Manager. You can store |
| 34 | +# your own token in your personal project's Secret Manager so that your |
| 35 | +# personal builds have coverage data uploaded to your own account. See also |
| 36 | +# https://cloud.google.com/build/docs/securing-builds/use-secrets |
| 37 | +if [[ -z "${CODECOV_TOKEN:-}" ]]; then |
| 38 | + io::log_h2 "No codecov token. Skipping upload." |
| 39 | + exit 0 |
| 40 | +fi |
| 41 | + |
| 42 | +# Merges the coverage.dat files, which reduces the overall size by about 90%. |
| 43 | +readonly MERGED_COVERAGE="/var/tmp/merged-coverage.lcov" |
| 44 | +io::log_h2 "Merging coverage data into ${MERGED_COVERAGE}" |
| 45 | +TIMEFORMAT="==> 🕑 merging done in %R seconds" |
| 46 | +time { |
| 47 | + mapfile -t coverage_dat < <(find "$(bazel info output_path)" -name "coverage.dat") |
| 48 | + io::log "Found ${#coverage_dat[@]} coverage.dat files" |
| 49 | + mapfile -t lcov_flags < <(printf -- "--add-tracefile=%s\n" "${coverage_dat[@]}") |
| 50 | + lcov --quiet "${lcov_flags[@]}" --output-file "${MERGED_COVERAGE}" |
| 51 | + ls -lh "${MERGED_COVERAGE}" |
| 52 | +} |
| 53 | + |
| 54 | +codecov_args=( |
| 55 | + "-X" "gcov" |
| 56 | + "-f" "${MERGED_COVERAGE}" |
| 57 | + "-q" "${HOME}/coverage-report.txt" |
| 58 | + "-B" "${BRANCH_NAME}" |
| 59 | + "-C" "${COMMIT_SHA}" |
| 60 | + "-P" "${PR_NUMBER:-}" |
| 61 | + "-b" "${BUILD_ID:-}" |
| 62 | +) |
| 63 | +io::log_h2 "Uploading ${MERGED_COVERAGE} to codecov.io" |
| 64 | +io::log "Flags: ${codecov_args[*]}" |
| 65 | +TIMEFORMAT="==> 🕑 codecov.io upload done in %R seconds" |
| 66 | +time { |
| 67 | + env -i CODECOV_TOKEN="${CODECOV_TOKEN:-}" HOME="${HOME}" \ |
| 68 | + bash <(curl -s https://codecov.io/bash) "${codecov_args[@]}" |
| 69 | +} |
0 commit comments