Skip to content

Commit d349a4e

Browse files
authored
ci(gcb): misc cleanups (#6281)
These are a couple small cleanups and documentation improvements that I had lying around.
1 parent 47a4eae commit d349a4e

File tree

3 files changed

+70
-30
lines changed

3 files changed

+70
-30
lines changed

ci/cloudbuild/README.md

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,69 @@
22

33
This directory contains the files needed for our GCB presubmit ("PR builds")
44
and postsubmit ("CI builds") builds. The `cloudbuild.yaml` file is the main
5-
config file for cloud build. Cloud builds may be managed from the command line
6-
with the `gcloud builds` command. To make this process easier, the `build.sh`
7-
script can be used to submit builds to GCB, run builds locally in a docker
8-
container, or even run builds directly in your local environment. See `build.sh
9-
--help` for more details.
5+
config file for Google Cloud Build (GCB). Cloud builds may be managed from the
6+
command line with the `gcloud builds` command. To make this process easier, the
7+
`build.sh` script can be used to submit builds to GCB, run builds locally in a
8+
docker container, or even run builds directly in your local environment. See
9+
`build.sh --help` for more details.
10+
11+
Build scripts live in the `builds/` directory. We want to keep these scripts as
12+
simple as possible. Ideally they will be just a simple listing of the commands
13+
that a human would type with minimal logic. Many of these build scripts should
14+
be only a few lines. A little duplication is even OK if the resulting build
15+
script is simpler. Sometimes there are non-trivial things we need to do
16+
repeatedly (e.g., run the quickstart builds), and these live in `builds/lib/`.
17+
18+
Each build runs in an environment defined by a Dockerfile in the `dockerfiles/`
19+
directory. Some Dockerfiles are used by multiple builds, some only one. Build
20+
scripts and Dockerfiles are associated in a trigger file.
1021

1122
GCB triggers can be configured in the http://console.cloud.google.com/ UI, but
12-
we prefer to configure them with SCM controlled YAML files that live in the
23+
we prefer to configure them with version controlled YAML files that live in the
1324
`triggers/` directory. Managing triggers can be done with the `gcloud` command
1425
also, but we have the local `trigger.sh` script to make this process a bit
1526
easier. See `trigger.sh --help` for more details.
1627

28+
Adding a new build is pretty simple, and can be done in a single PR. For
29+
example, see https://github.com/googleapis/google-cloud-cpp/pull/6252, which
30+
adds the `cxx17-pr` and `cxx17-ci` builds. The steps to add a new build are:
31+
32+
1. (Optional) If your build needs to run in a unique environment, you should
33+
start by creating a new Dockerfile in `dockerfiles/`. If you don't need a
34+
specific environment, we typically use the `dockerfiles/fedora.Dockerfile`
35+
image, which is simply called the "fedora" distro. Here's an [example
36+
PR](https://github.com/googleapis/google-cloud-cpp/pull/6259) that adds a
37+
build with its own Dockerfile.
38+
2. Create the new build script in `builds/` that runs the commands you want.
39+
* You can test this script right away with `build.sh` by explicitly
40+
specifying the `--distro` you want your build to run in. For example:
41+
```
42+
$ build.sh --distro fedora my-new-build # or ...
43+
$ build.sh --distro fedora my-new-build --docker
44+
```
45+
3. Create your trigger file(s) in the `triggers/` directory. If you want both
46+
PR (presubmit) and CI (postsubmit) builds you can generate the trigger files
47+
with the command `trigger.sh --generate my-new-build`, which will write the
48+
new files in the `triggers/` directory. You may need to tweak the files at
49+
this point, for example to change the distro (fedora is the default).
50+
4. At this point, you're pretty much done. You can now test your build using
51+
the trigger name as shown here:
52+
```
53+
$ build.sh -t my-new-build-pr # or ...
54+
$ build.sh -t my-new-build-pr --docker # or ...
55+
$ build.sh -t my-new-build-pr --project cloud-cpp-testing-resources
56+
```
57+
5. Send a PR! Google Cloud Build will not know about your new trigger files yet
58+
so they will not be run for your PR. This is working as intended. Get the PR
59+
reviewed and merge it.
60+
6. FINAL STEP: Now that the code for your new build is checked in, tell GCB
61+
about the triggers so the can be run automatically for all future PRs and
62+
merges.
63+
```
64+
$ trigger.sh --import triggers/my-new-build-pr.yaml
65+
$ trigger.sh --import triggers/my-new-build-ci.yaml
66+
```
67+
1768
## Testing principles
1869

1970
We want our code to work for our customers. We don't know their exact
@@ -45,9 +96,3 @@ while achieving high likelihood of the code working for our customers.
4596
* Sanitizer builds need to run integration tests
4697
* We want to test our user-facing instructions (i.e., how to install and use)
4798
as much as possible (this is difficult on macOS and Windows without docker)
48-
49-
In addition to the guiding principles above, we'd like to keep the build
50-
scripts in `builds/` as simple as possible; ideally just a simple listing of
51-
the commands that a human would type with minimal logic. Many of these build
52-
scripts should be only a few lines. A little duplication is even OK if the
53-
resulting build script is simpler.

ci/cloudbuild/build.sh

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ if [[ "${DOCKER_FLAG}" = "true" ]]; then
220220
io::log_h2 "Building docker image: ${image}"
221221
docker build -t "${image}" "--build-arg=NCPU=$(nproc)" \
222222
-f "ci/cloudbuild/dockerfiles/${DISTRO_FLAG}.Dockerfile" ci
223-
io::log_h2 "Starting container for ${image} running ${BUILD_NAME}"
223+
io::log_h2 "Starting docker container: ${image}"
224224
run_flags=(
225225
"--interactive"
226226
"--tty"
@@ -242,8 +242,9 @@ if [[ "${DOCKER_FLAG}" = "true" ]]; then
242242
)
243243
cmd=(ci/cloudbuild/build.sh --local "${BUILD_NAME}")
244244
if [[ "${SHELL_FLAG}" = "true" ]]; then
245-
io::log "Starting shell, to manually run the requested build use:"
246-
echo "==> ${cmd[*]}"
245+
printf "To run the build manually:\n "
246+
printf " %q" "${cmd[@]}"
247+
printf "\n\n"
247248
cmd=("bash")
248249
fi
249250
docker run "${run_flags[@]}" "${image}" "${cmd[@]}"
@@ -264,16 +265,17 @@ fi
264265
# Uses Google Cloud build to run the specified build.
265266
io::log_h1 "Starting cloud build: ${BUILD_NAME}"
266267
account="$(gcloud config list account --format "value(core.account)")"
267-
subs="_DISTRO=${DISTRO_FLAG}"
268-
subs+=",_BUILD_NAME=${BUILD_NAME}"
269-
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}"
273-
io::log "Substitutions ${subs}"
268+
subs=("_DISTRO=${DISTRO_FLAG}")
269+
subs+=("_BUILD_NAME=${BUILD_NAME}")
270+
subs+=("_CACHE_TYPE=manual-${account}")
271+
subs+=("_PR_NUMBER=") # Must be empty or a number, and this is not a PR
272+
subs+=("BRANCH_NAME=${BRANCH_NAME}")
273+
subs+=("COMMIT_SHA=${COMMIT_SHA}")
274+
printf "Substitutions:\n"
275+
printf " %s\n" "${subs[@]}"
274276
args=(
275277
"--config=ci/cloudbuild/cloudbuild.yaml"
276-
"--substitutions=${subs}"
278+
"--substitutions=$(printf "%s," "${subs[@]}")"
277279
)
278280
if [[ -n "${PROJECT_FLAG}" ]]; then
279281
args+=("--project=${PROJECT_FLAG}")

ci/cloudbuild/builds/README.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)