Skip to content

Commit be2c7d3

Browse files
authored
ci(gcb): enable coverage build (#6270)
1 parent 9f256b7 commit be2c7d3

File tree

5 files changed

+117
-1
lines changed

5 files changed

+117
-1
lines changed

ci/cloudbuild/build.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ if [[ -z "${BUILD_NAME}" ]]; then
158158
exit 1
159159
fi
160160

161+
# Info about the git repo that is used by some builds, e.g., coverage. These
162+
# will be automatically set by GCB for triggered builds, but we need to compute
163+
# them ourselves for manually started builds and docker builds. See
164+
# https://cloud.google.com/build/docs/configuring-builds/substitute-variable-values
165+
BRANCH_NAME="${BRANCH_NAME:-$(git branch --show-current)}"
166+
COMMIT_SHA="${COMMIT_SHA:-$(git rev-parse HEAD)}"
167+
161168
# --local is the most fundamental build mode, in that all other builds
162169
# eventually call this one. For example, a --docker build will build the
163170
# specified docker image, then in a container from that image it will run the
@@ -221,6 +228,9 @@ if [[ "${DOCKER_FLAG}" = "true" ]]; then
221228
"--user=$(id -u):$(id -g)"
222229
"--env=USER=$(id -un)"
223230
"--env=TZ=UTC0"
231+
"--env=CODECOV_TOKEN=${CODECOV_TOKEN:-}"
232+
"--env=BRANCH_NAME=${BRANCH_NAME}"
233+
"--env=COMMIT_SHA=${COMMIT_SHA}"
224234
# Mounts an empty volume over "build-out" to isolate builds from each
225235
# other. Doesn't affect GCB builds, but it helps our local docker builds.
226236
"--volume=/workspace/build-out"
@@ -257,6 +267,9 @@ account="$(gcloud config list account --format "value(core.account)")"
257267
subs="_DISTRO=${DISTRO_FLAG}"
258268
subs+=",_BUILD_NAME=${BUILD_NAME}"
259269
subs+=",_CACHE_TYPE=manual-${account}"
270+
subs+=",_PR_NUMBER=" # Must be empty or a number, and this is not a PR
271+
subs+=",BRANCH_NAME=${BRANCH_NAME}"
272+
subs+=",COMMIT_SHA=${COMMIT_SHA}"
260273
io::log "Substitutions ${subs}"
261274
args=(
262275
"--config=ci/cloudbuild/cloudbuild.yaml"

ci/cloudbuild/builds/coverage.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
}

ci/cloudbuild/cloudbuild.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ options:
2121
'TZ=UTC0',
2222
'GOOGLE_CLOUD_BUILD=yes',
2323
'PROJECT_ID=${PROJECT_ID}',
24-
'BUILD_ID=${BUILD_ID}'
24+
'BUILD_ID=${BUILD_ID}',
25+
'BRANCH_NAME=${BRANCH_NAME}',
26+
'COMMIT_SHA=${COMMIT_SHA}',
27+
'PR_NUMBER=${_PR_NUMBER}'
2528
]
2629
volumes:
2730
- name: 'home'
@@ -40,6 +43,11 @@ substitutions:
4043
tags: [ '${_TRIGGER_TYPE}', '${_BUILD_NAME}', '${_DISTRO}' ]
4144
timeout: 3600s
4245

46+
availableSecrets:
47+
secretManager:
48+
- versionName: projects/${PROJECT_ID}/secrets/CODECOV_TOKEN/versions/latest
49+
env: 'CODECOV_TOKEN'
50+
4351
steps:
4452
# Builds the docker image that will be used by the main build step.
4553
- name: 'gcr.io/kaniko-project/executor:edge'
@@ -66,6 +74,7 @@ steps:
6674
- name: 'gcr.io/${PROJECT_ID}/${_IMAGE}:${BUILD_ID}'
6775
entrypoint: 'ci/cloudbuild/build.sh'
6876
args: [ '--local', '${_BUILD_NAME}' ]
77+
secretEnv: ['CODECOV_TOKEN']
6978
env: [
7079
'BAZEL_REMOTE_CACHE=https://storage.googleapis.com/${_CACHE_BUCKET}/bazel-cache/${_DISTRO}-${_BUILD_NAME}',
7180
'VCPKG_BINARY_SOURCES=x-gcs,gs://${_CACHE_BUCKET}/vcpkg-cache/${_DISTRO}-${_BUILD_NAME},readwrite'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
filename: ci/cloudbuild/cloudbuild.yaml
2+
github:
3+
name: google-cloud-cpp
4+
owner: googleapis
5+
push:
6+
branch: ^(master|main|v\d+\..*)$
7+
name: coverage-ci
8+
substitutions:
9+
_BUILD_NAME: coverage
10+
_DISTRO: fedora
11+
tags:
12+
- ci
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
filename: ci/cloudbuild/cloudbuild.yaml
2+
github:
3+
name: google-cloud-cpp
4+
owner: googleapis
5+
pullRequest:
6+
branch: ^(master|main|v\d+\..*)$
7+
commentControl: COMMENTS_ENABLED_FOR_EXTERNAL_CONTRIBUTORS_ONLY
8+
name: coverage-pr
9+
substitutions:
10+
_BUILD_NAME: coverage
11+
_DISTRO: fedora
12+
tags:
13+
- pr

0 commit comments

Comments
 (0)