Skip to content

Commit da81445

Browse files
author
ddeleo
committed
Adding logic to prevent parallel commits from clobbering assets and to also remove testing assets when done testing
1 parent dc5de65 commit da81445

File tree

4 files changed

+96
-15
lines changed

4 files changed

+96
-15
lines changed

cloudbuild.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ steps:
4242
- PROJECT_ID=${PROJECT_ID}
4343
- BRANCH_NAME=${BRANCH_NAME}
4444
- SHORT_SHA=${SHORT_SHA}
45+
- _BQ_DATASET=datasketches
4546
- _JS_BUCKET=${_JS_BUCKET}
4647
- _PR_NUMBER=${_PR_NUMBER}
4748
- _BQ_LOCATION=${_BQ_LOCATION}

release/terraform/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ resource "google_cloudbuild_trigger" "datasketches_regional_trigger" {
9090

9191
substitutions = {
9292
_BQ_LOCATION = "${each.value}"
93-
_JS_BUCKET = "gs://${var.project}-lib-${each.value}"
93+
_JS_BUCKET = "gs://${var.project}-datasketches-${each.value}"
9494
}
9595
}
9696

udfs/datasketches/cloudbuild.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ steps:
1010
- deploy.sh
1111
env:
1212
- PROJECT_ID=${PROJECT_ID}
13+
- BRANCH_NAME=${BRANCH_NAME}
14+
- SHORT_SHA=${SHORT_SHA}
1315
- _JS_BUCKET=${_JS_BUCKET}
16+
- _PR_NUMBER=${_PR_NUMBER}
1417
- _BQ_LOCATION=${_BQ_LOCATION}
1518

1619
timeout: 1800s # 30 minutes

udfs/datasketches/deploy.sh

Lines changed: 91 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,96 @@
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.
216
set -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

Comments
 (0)