11#! /usr/bin/env 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+ # 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.
216set -eo pipefail
317
4- git clone --recursive --single-branch --branch $( cat VERSION.txt) https://github.com/apache/datasketches-bigquery.git
5- cd datasketches-bigquery
18+ # Remove testing assets
19+ # Globals:
20+ # _BQ_DATASET
21+ # _JS_BUCKET
22+ # Arguments:
23+ # None
24+ # Returns:
25+ # None
26+ # ######################################
27+ function remove_testing_assets(){
28+ printf " Deleting BigQuery dataset: %s\n" " ${_BQ_DATASET} "
29+ bq --headless --synchronous_mode rm -r -f " ${_BQ_DATASET} "
30+ printf " Deleting Cloud Storage directory: %s\n" " ${_JS_BUCKET} "
31+ gcloud storage rm -r " ${_JS_BUCKET} /**"
32+ }
33+
34+ # #############################################
35+ # Deploys datasketch UDFs and runs unit tests.
36+ # Globals:
37+ # PROJECT_ID
38+ # _BQ_DATASET
39+ # _JS_BUCKET
40+ # _BQ_LOCATION
41+ # Arguments:
42+ # None
43+ # Returns:
44+ # None
45+ # #############################################
46+ function deploy_udfs_and_run_unit_tests() {
47+ git clone --recursive --single-branch --branch $( cat VERSION.txt) https://github.com/apache/datasketches-bigquery.git
48+ cd datasketches-bigquery
49+
50+ if [[ " ${_BQ_LOCATION^^} " != " US" ]]; then
51+ printf " Deploying to regional BigQuery location: %s\n" " ${_BQ_LOCATION} "
52+ export _BQ_DATASET=" ${_BQ_DATASET} _$( echo $_BQ_LOCATION | tr ' [:upper:]' ' [:lower:]' | tr ' -' ' _' ) "
53+ printf " BigQuery regional dataset name: %s\n" " ${_BQ_DATASET} "
54+ fi
55+
56+ gcloud builds submit . \
57+ --project=$PROJECT_ID \
58+ --region=" us-central1d" \
59+ --worker-pool=" projects/${PROJECT_ID} /locations/us-central1/workerPools/udf-unit-testing" \
60+ --polling-interval=" 10" \
61+ --substitutions=_BQ_LOCATION=$_BQ_LOCATION ,_BQ_DATASET=$_BQ_DATASET ,_JS_BUCKET=$_JS_BUCKET
62+ }
663
7- _BQ_DATASET=" datasketches"
8- if [[ " ${_BQ_LOCATION^^} " != " US" ]]; then
9- printf " Deploying to regional BigQuery location: %s\n" " ${_BQ_LOCATION} "
10- _BQ_DATASET=" ${_BQ_DATASET} _$( echo $_BQ_LOCATION | tr ' [:upper:]' ' [:lower:]' | tr ' -' ' _' ) "
11- printf " BigQuery regional dataset name: %s\n" " ${_BQ_DATASET} "
12- fi
64+ # ######################################
65+ # Main entry-point for execution
66+ # Globals:
67+ # BRANCH_NAME
68+ # SHORT_SHA
69+ # _PR_NUMBER
70+ # _BQ_DATASET
71+ # _JS_BUCKET
72+ # Arguments:
73+ # None
74+ # Returns:
75+ # None
76+ # ######################################
77+ function main() {
78+ # Only deploy UDFs when building master branch and there is
79+ # no associated pull request, meaning the PR was approved
80+ # and this is now building a commit on master branch.
81+ if [[ " ${BRANCH_NAME} " == " master" && -z " ${_PR_NUMBER} " ]]; then
82+ deploy_udfs_and_run_unit_tests
83+ else
84+ # Add SHORTSHA to _JS_BUCKET and _BQ_DATASET to prevent
85+ # collisions between concurrent builds.
86+ export _JS_BUCKET=" ${_JS_BUCKET} /${SHORT_SHA} "
87+ export _BQ_DATASET=" ${_BQ_DATASET} _${SHORT_SHA} "
88+ if ! deploy_udfs_and_run_unit_tests ; then
89+ printf " FAILURE: Build process for datasketch UDFs failed, running cleanup steps:\n"
90+ remove_testing_assets
91+ exit 1
92+ fi
93+ fi
94+ }
1395
14- gcloud builds submit . \
15- --project=$PROJECT_ID \
16- --region=" us-central1" \
17- --worker-pool=" projects/${PROJECT_ID} /locations/us-central1/workerPools/udf-unit-testing" \
18- --polling-interval=" 10" \
19- --substitutions=_BQ_LOCATION=$_BQ_LOCATION ,_BQ_DATASET=$_BQ_DATASET ,_JS_BUCKET=$_JS_BUCKET
96+ main
0 commit comments