1414# See the License for the specific language governing permissions and
1515# limitations under the License.
1616#
17- # ## Run builds using Google Cloud Build ##
18- #
19- # This script runs the builds defined in `ci/cloudbuild/builds/` on the local
20- # machine (if `--local` is specified), in the local docker (if `--docker` is
21- # specified), or in the cloud using Google Cloud Build (the default). A single
22- # argument indicating the build name is required. The distro indicates the
23- # `<distro>.Dockerfile` to use for the build (ignored if using `--local`). The
24- # distro is looked up from the specified build's trigger file
25- # (`ci/cloudbuild/triggers/<build-name>.yaml`), but the distro can be
26- # overridden with the optional `--distro=<arg>` flag.
27- #
28- # Usage: build.sh [options] <build-name>
17+ # Usage: build.sh [options] [build-name]
2918#
3019# Options:
3120# --distro=<name> The distro name to use
21+ # -t|--trigger The trigger file to extract the build name and distro
3222# -l|--local Run the build in the local environment
3323# -d|--docker Run the build in a local docker
3424# -s|--docker-shell Run a shell in the build's docker container
3525# -p|--project=<name> The Cloud Project ID to use
3626# -h|--help Print this help message
3727#
38- # Build names:
28+ # This script runs named builds, where builds are defined as a build script
29+ # (which lives in `ci/cloudbuild/builds/`) and a distro (which is defined in
30+ # `ci/cloudbuild/<distro>.Dockerfile`). Trigger files (which live in
31+ # `ci/cloudbuild/triggers/`) associate build scripts with the distro they're
32+ # intended to run on. For example, the "asan-pr" build is defined in the
33+ # `ci/cloudbuild/triggers/asan-pr.yaml` file, which specifies that the
34+ # `ci/cloudbuild/builds/asan.sh` script should be run on the
35+ # `ci/cloudbuild/fedora.Dockerfile` image. There are a couple ways to specify
36+ # builds:
37+ #
38+ # 1. Explicitly name the distro and build script. For example:
39+ # $ build.sh --distro fedora asan
40+ #
41+ # 2. Name the trigger file, which contains the distro and build script:
42+ # $ build.sh --trigger asan-pr
43+ #
44+ # Note: command-line flags may be specified with or without an equals sign
45+ # (e.g. `-t=foo` is the same as `-t foo`), and in any order.
46+ #
47+ # Usage examples:
48+ #
49+ # Runs the asan build from the asan-pr.yaml file on GCB
50+ # $ build.sh -t asan-pr
51+ #
52+ # Runs the asan build from the asan-pr.yaml file in docker
53+ # $ build.sh -t asan-pr --docker
54+ #
55+ # Opens a shell in the docker container for the asan-pr.yaml build
56+ # NOTE: The `-s` flag is useful for debugging builds.
57+ # $ build.sh -t asan-pr --docker-shell
58+ # $ build.sh -t asan-pr --docker -s # equivalent
59+ # $ build.sh -t asan-pr -s # equivalent
60+ #
61+ # Runs the asan build from the asan-pr.yaml file in the local environment
62+ # $ build.sh -t asan-pr --local
63+ #
64+ # Runs builds/cmake-install.sh script in the demo-centos-7.Dockerfile
65+ # $ build.sh cmake-install --distro demo-centos-7 --docker
66+ #
67+ # Runs the integration tests in the cloud-cpp-testing-resources project
68+ # $ build.sh -t integration-pr --project cloud-cpp-testing-resources
69+ #
70+ # Runs the checkers in your local docker
71+ # NOTE: This is a good way to format your code and check for style issues.
72+ # $ build.sh -t checkers-pr --docker
3973
4074set -euo pipefail
4175
@@ -46,19 +80,19 @@ cd "${PROJECT_ROOT}"
4680function print_usage() {
4781 # Extracts the usage from the file comment starting at line 17.
4882 sed -n ' 17,/^$/s/^# \?//p' " ${PROGRAM_PATH} "
49- basename -s .sh ci/cloudbuild/builds/* .sh | sort | xargs printf " %s\n"
5083}
5184
5285# Use getopt to parse and normalize all the args.
5386PARSED=" $( getopt -a \
54- --options=" p:ldsh" \
55- --longoptions=" distro:,project:,local,docker,docker-shell,help" \
87+ --options=" p:t: ldsh" \
88+ --longoptions=" distro:,project:,trigger:, local,docker,docker-shell,help" \
5689 --name=" ${PROGRAM_NAME} " \
5790 -- " $@ " ) "
5891eval set -- " ${PARSED} "
5992
6093DISTRO_FLAG=" "
6194PROJECT_FLAG=" "
95+ TRIGGER_FLAG=" "
6296LOCAL_FLAG=" false"
6397DOCKER_FLAG=" false"
6498SHELL_FLAG=" false"
@@ -72,6 +106,10 @@ while true; do
72106 PROJECT_FLAG=" $2 "
73107 shift 2
74108 ;;
109+ -t | --trigger)
110+ TRIGGER_FLAG=" $2 "
111+ shift 2
112+ ;;
75113 -l | --local)
76114 LOCAL_FLAG=" true"
77115 shift
@@ -97,12 +135,28 @@ while true; do
97135done
98136readonly PROJECT_FLAG
99137
100- if (( $# != 1 )) ; then
101- echo " Must specify exactly one build name"
138+ # If `--trigger=name` was specified, use the _BUILD_NAME and _DISTRO in the
139+ # trigger file as defaults.
140+ BUILD_NAME=" ${1:- } "
141+ if [[ -n " ${TRIGGER_FLAG} " ]]; then
142+ trigger_file=" ${PROGRAM_DIR} /triggers/${TRIGGER_FLAG} .yaml"
143+ if [[ ! -r " ${trigger_file} " ]]; then
144+ io::log_red " Cannot open ${trigger_file} "
145+ exit 1
146+ fi
147+ build=" $( grep _BUILD_NAME " ${trigger_file} " | awk ' {print $2}' ) "
148+ distro=" $( grep _DISTRO " ${trigger_file} " | awk ' {print $2}' ) "
149+ test -z " ${BUILD_NAME} " && BUILD_NAME=" ${build} "
150+ test -z " ${DISTRO_FLAG} " && DISTRO_FLAG=" ${distro} "
151+ fi
152+ readonly BUILD_NAME
153+ readonly DISTRO_FLAG
154+
155+ if [[ -z " ${BUILD_NAME} " ]]; then
156+ io::log_red " No build name specified. Specify a build name or use --trigger"
102157 print_usage
103158 exit 1
104159fi
105- readonly BUILD_NAME=" $1 "
106160
107161# --local is the most fundamental build mode, in that all other builds
108162# eventually call this one. For example, a --docker build will build the
@@ -112,7 +166,7 @@ readonly BUILD_NAME="$1"
112166if [[ " ${LOCAL_FLAG} " = " true" ]]; then
113167 test -n " ${DISTRO_FLAG} " && io::log_red " Local build ignoring --distro=${DISTRO_FLAG} "
114168 if [[ " ${DOCKER_FLAG} " = " true" ]]; then
115- echo " Only one of --local or --docker may be specified"
169+ io::log_red " Only one of --local or --docker may be specified"
116170 print_usage
117171 exit 1
118172 fi
@@ -141,17 +195,11 @@ if [[ "${LOCAL_FLAG}" = "true" ]]; then
141195 exit
142196fi
143197
144- # If --distro wasn't specified, look it up from the build's trigger file.
145198if [[ -z " ${DISTRO_FLAG} " ]]; then
146- trigger_file=" ci/cloudbuild/triggers/${BUILD_NAME} -ci.yaml"
147- DISTRO_FLAG=" $( grep _DISTRO " ${trigger_file} " | awk ' {print $2}' || true) "
148- if [[ -z " ${DISTRO_FLAG} " ]]; then
149- echo " Missing --distro=<arg>, and none found in ${trigger_file} "
150- print_usage
151- exit 1
152- fi
199+ io::log_red " No distro specified. Use --distro or --trigger"
200+ print_usage
201+ exit 1
153202fi
154- readonly DISTRO_FLAG
155203
156204# Uses docker to locally build the specified image and run the build command.
157205# Docker builds store their outputs on the host system in `build-out/`.
194242
195243# Surface invalid arguments early rather than waiting for GCB to fail.
196244if [ ! -r " ${PROGRAM_DIR} /${DISTRO_FLAG} .Dockerfile" ]; then
197- echo " Unknown distro: ${DISTRO_FLAG} "
245+ io::log_red " Unknown distro: ${DISTRO_FLAG} "
198246 print_usage
199247 exit 1
200248elif [ ! -x " ${PROGRAM_DIR} /builds/${BUILD_NAME} .sh" ]; then
201- echo " Unknown build name: ${BUILD_NAME} "
249+ io::log_red " Unknown build name: ${BUILD_NAME} "
202250 print_usage
203251 exit 1
204252fi
0 commit comments