From c69df480634dc728cbc6c79023254ab92a9615df Mon Sep 17 00:00:00 2001 From: averikitsch Date: Mon, 21 Oct 2019 14:37:36 -0700 Subject: [PATCH 1/7] Kokoro tests --- .kokoro/tests/build_cloud_run.sh | 66 ++++++++++++++++++++++++++++++++ .kokoro/tests/run_tests.sh | 12 +++++- 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100755 .kokoro/tests/build_cloud_run.sh diff --git a/.kokoro/tests/build_cloud_run.sh b/.kokoro/tests/build_cloud_run.sh new file mode 100755 index 00000000000..de3c6c6906f --- /dev/null +++ b/.kokoro/tests/build_cloud_run.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +# Copyright 2019 Google LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -eo pipefail +requireEnv() { + test "${!1}" || (echo "Environment Variable '$1' not found" && exit 1) +} +requireEnv SAMPLE_NAME + +# Version is in the format -. +# Ensures PR-based triggers of the same branch don't collide if Kokoro attempts +# to run them concurrently. +export SAMPLE_VERSION="${KOKORO_GIT_COMMIT:-latest}" +# Builds not triggered by a PR will fall back to the commit hash then "latest". +SUFFIX=${KOKORO_GITHUB_PULL_REQUEST_NUMBER:-${SAMPLE_VERSION:0:12}} +export SERVICE_NAME="${SAMPLE_NAME}-${SUFFIX}" +export CONTAINER_IMAGE="gcr.io/${GOOGLE_CLOUD_PROJECT}/run-${SAMPLE_NAME}:${SAMPLE_VERSION}" + +# Build the service +set -x +gcloud builds submit --tag="${CONTAINER_IMAGE}" +set +x + +echo '---' +set -x +gcloud beta run deploy "${SERVICE_NAME}" \ + --image="${CONTAINER_IMAGE}" \ + --region="${REGION:-us-central1}" \ + --platform=managed \ + --quiet + +echo 'Cloud Run Links:' +echo "- Logs: https://console.cloud.google.com/logs/viewer?project=${GOOGLE_CLOUD_PROJECT}&resource=cloud_run_revision%2Fservice_name%2F${SERVICE_NAME}" +echo "- Console: https://console.cloud.google.com/run/detail/${REGION:-us-central1}/${SERVICE_NAME}/metrics?project=${GOOGLE_CLOUD_PROJECT}" + +echo +echo '---' +echo + +# Register post-test cleanup. +# Only needed if deploy completed. +function cleanup { + set -x + gcloud --quiet container images delete "${CONTAINER_IMAGE}" || true + gcloud beta run services delete ${SERVICE_NAME} \ + --platform=managed \ + --region="${REGION:-us-central1}" \ + --quiet +} +trap cleanup EXIT + +# Do not use exec to preserve trap behavior. +"$@" diff --git a/.kokoro/tests/run_tests.sh b/.kokoro/tests/run_tests.sh index 1bb72c7667c..6369b6aa7ac 100755 --- a/.kokoro/tests/run_tests.sh +++ b/.kokoro/tests/run_tests.sh @@ -19,6 +19,8 @@ set -eo pipefail # Enables `**` to include files nested inside sub-folders shopt -s globstar +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + # `--debug` can be added make local testing of this script easier if [[ $* == *--script-debug* ]]; then SCRIPT_DEBUG="true" @@ -70,7 +72,7 @@ fi # Package local jetty dependency for Java11 samples if [[ "$JAVA_VERSION" == "11" ]]; then cd appengine-java11/appengine-simple-jetty-main/ - mvn install + mvn install --quiet cd ../../ fi @@ -101,6 +103,14 @@ for file in **/pom.xml; do echo "- testing $file" echo "------------------------------------------------------------" + if [[ "$file" =~ "run" ]]; then + sample=$(dirname "$file") + export SAMPLE_NAME=$sample + chmod 755 "$SCRIPT_DIR"/build_cloud_run.sh + "$SCRIPT_DIR"/build_cloud_run.sh + continue + fi + # Fail the tests if no Java version was found. POM_JAVA=$(grep -oP '(?<=).*?(?=)' pom.xml) if [[ "$POM_JAVA" = "" ]]; then From ee5cbe63312df13f9459749c981d6cecf7f43dbc Mon Sep 17 00:00:00 2001 From: averikitsch Date: Mon, 21 Oct 2019 14:43:30 -0700 Subject: [PATCH 2/7] change for kokoro to run --- run/hello-broken/pom.xml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/run/hello-broken/pom.xml b/run/hello-broken/pom.xml index a32045dc79b..0f1e2f73096 100644 --- a/run/hello-broken/pom.xml +++ b/run/hello-broken/pom.xml @@ -16,7 +16,10 @@ limitations under the License. com.example.cloudrun hello-broken 0.0.1-SNAPSHOT - + com.google.cloud.samples shared-configuration From 866b82a1eee09c4728a17f844942d407d30fc579 Mon Sep 17 00:00:00 2001 From: averikitsch Date: Mon, 21 Oct 2019 15:50:56 -0700 Subject: [PATCH 3/7] update bash script --- .kokoro/tests/build_cloud_run.sh | 5 ++--- .kokoro/tests/run_tests.sh | 25 +++++++++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/.kokoro/tests/build_cloud_run.sh b/.kokoro/tests/build_cloud_run.sh index de3c6c6906f..9afe63c4b0a 100755 --- a/.kokoro/tests/build_cloud_run.sh +++ b/.kokoro/tests/build_cloud_run.sh @@ -32,16 +32,15 @@ export CONTAINER_IMAGE="gcr.io/${GOOGLE_CLOUD_PROJECT}/run-${SAMPLE_NAME}:${SAMP # Build the service set -x gcloud builds submit --tag="${CONTAINER_IMAGE}" -set +x -echo '---' -set -x gcloud beta run deploy "${SERVICE_NAME}" \ --image="${CONTAINER_IMAGE}" \ --region="${REGION:-us-central1}" \ --platform=managed \ --quiet +set +x + echo 'Cloud Run Links:' echo "- Logs: https://console.cloud.google.com/logs/viewer?project=${GOOGLE_CLOUD_PROJECT}&resource=cloud_run_revision%2Fservice_name%2F${SERVICE_NAME}" echo "- Console: https://console.cloud.google.com/run/detail/${REGION:-us-central1}/${SERVICE_NAME}/metrics?project=${GOOGLE_CLOUD_PROJECT}" diff --git a/.kokoro/tests/run_tests.sh b/.kokoro/tests/run_tests.sh index 6369b6aa7ac..1042a02ac7f 100755 --- a/.kokoro/tests/run_tests.sh +++ b/.kokoro/tests/run_tests.sh @@ -44,6 +44,7 @@ fi if [[ "$SCRIPT_DEBUG" != "true" ]]; then # Update `gcloud` and log versioning for debugging. + gcloud components install beta --quiet gcloud components update --quiet echo "********** GCLOUD INFO ***********" gcloud -v @@ -103,14 +104,6 @@ for file in **/pom.xml; do echo "- testing $file" echo "------------------------------------------------------------" - if [[ "$file" =~ "run" ]]; then - sample=$(dirname "$file") - export SAMPLE_NAME=$sample - chmod 755 "$SCRIPT_DIR"/build_cloud_run.sh - "$SCRIPT_DIR"/build_cloud_run.sh - continue - fi - # Fail the tests if no Java version was found. POM_JAVA=$(grep -oP '(?<=).*?(?=)' pom.xml) if [[ "$POM_JAVA" = "" ]]; then @@ -145,6 +138,22 @@ for file in **/pom.xml; do echo -e "\n Testing completed.\n" fi + # Build and deploy Cloud Run samples + if [[ "$file" == "run/*" ]]; then + sample=$(dirname "$file") + export SAMPLE_NAME=$sample + chmod 755 "$SCRIPT_DIR"/build_cloud_run.sh + "$SCRIPT_DIR"/build_cloud_run.sh + EXIT=$? + + if [[ $EXIT -ne 0 ]]; then + RTN=1 + echo -e "\n Cloud Run build/deploy failed: gcloud returned a non-zero exit code. \n" + else + echo -e "\n Cloud Run build/deploy completed.\n" + fi + fi + done exit "$RTN" From f60317e8a84fa1fcee21b4e2a42686bddc2cee64 Mon Sep 17 00:00:00 2001 From: averikitsch Date: Mon, 21 Oct 2019 16:03:53 -0700 Subject: [PATCH 4/7] fix clean up and wildcard --- .kokoro/tests/build_cloud_run.sh | 26 +++++++++++++++----------- .kokoro/tests/run_tests.sh | 2 +- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/.kokoro/tests/build_cloud_run.sh b/.kokoro/tests/build_cloud_run.sh index 9afe63c4b0a..c5425f841fd 100755 --- a/.kokoro/tests/build_cloud_run.sh +++ b/.kokoro/tests/build_cloud_run.sh @@ -15,6 +15,19 @@ # limitations under the License. set -eo pipefail + +# Register post-test cleanup. +# Only needed if deploy completed. +function cleanup { + set -x + gcloud --quiet container images delete "${CONTAINER_IMAGE}" || true + gcloud beta run services delete ${SERVICE_NAME} \ + --platform=managed \ + --region="${REGION:-us-central1}" \ + --quiet +} +trap cleanup EXIT + requireEnv() { test "${!1}" || (echo "Environment Variable '$1' not found" && exit 1) } @@ -29,6 +42,8 @@ SUFFIX=${KOKORO_GITHUB_PULL_REQUEST_NUMBER:-${SAMPLE_VERSION:0:12}} export SERVICE_NAME="${SAMPLE_NAME}-${SUFFIX}" export CONTAINER_IMAGE="gcr.io/${GOOGLE_CLOUD_PROJECT}/run-${SAMPLE_NAME}:${SAMPLE_VERSION}" + + # Build the service set -x gcloud builds submit --tag="${CONTAINER_IMAGE}" @@ -49,17 +64,6 @@ echo echo '---' echo -# Register post-test cleanup. -# Only needed if deploy completed. -function cleanup { - set -x - gcloud --quiet container images delete "${CONTAINER_IMAGE}" || true - gcloud beta run services delete ${SERVICE_NAME} \ - --platform=managed \ - --region="${REGION:-us-central1}" \ - --quiet -} -trap cleanup EXIT # Do not use exec to preserve trap behavior. "$@" diff --git a/.kokoro/tests/run_tests.sh b/.kokoro/tests/run_tests.sh index 1042a02ac7f..42d1dbd4729 100755 --- a/.kokoro/tests/run_tests.sh +++ b/.kokoro/tests/run_tests.sh @@ -139,7 +139,7 @@ for file in **/pom.xml; do fi # Build and deploy Cloud Run samples - if [[ "$file" == "run/*" ]]; then + if [[ "$file" == "run/"* ]]; then sample=$(dirname "$file") export SAMPLE_NAME=$sample chmod 755 "$SCRIPT_DIR"/build_cloud_run.sh From bd5405765d68dbf5133fbcd7dd69014e5c6b0c43 Mon Sep 17 00:00:00 2001 From: averikitsch Date: Mon, 21 Oct 2019 16:18:53 -0700 Subject: [PATCH 5/7] sample name fixed --- .kokoro/tests/build_cloud_run.sh | 2 +- .kokoro/tests/run_tests.sh | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.kokoro/tests/build_cloud_run.sh b/.kokoro/tests/build_cloud_run.sh index c5425f841fd..bd4d7cadfc0 100755 --- a/.kokoro/tests/build_cloud_run.sh +++ b/.kokoro/tests/build_cloud_run.sh @@ -46,7 +46,7 @@ export CONTAINER_IMAGE="gcr.io/${GOOGLE_CLOUD_PROJECT}/run-${SAMPLE_NAME}:${SAMP # Build the service set -x -gcloud builds submit --tag="${CONTAINER_IMAGE}" +gcloud builds submit --tag="${CONTAINER_IMAGE}" --quiet gcloud beta run deploy "${SERVICE_NAME}" \ --image="${CONTAINER_IMAGE}" \ diff --git a/.kokoro/tests/run_tests.sh b/.kokoro/tests/run_tests.sh index 42d1dbd4729..44c53f5ca1d 100755 --- a/.kokoro/tests/run_tests.sh +++ b/.kokoro/tests/run_tests.sh @@ -140,9 +140,8 @@ for file in **/pom.xml; do # Build and deploy Cloud Run samples if [[ "$file" == "run/"* ]]; then - sample=$(dirname "$file") - export SAMPLE_NAME=$sample - chmod 755 "$SCRIPT_DIR"/build_cloud_run.sh + export SAMPLE_NAME=${file#"run/"} + # chmod 755 "$SCRIPT_DIR"/build_cloud_run.sh "$SCRIPT_DIR"/build_cloud_run.sh EXIT=$? From f27a6e746f8d0f681971a8ca472c1391217ba9bf Mon Sep 17 00:00:00 2001 From: averikitsch Date: Mon, 21 Oct 2019 16:40:58 -0700 Subject: [PATCH 6/7] Add todo --- .kokoro/tests/build_cloud_run.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.kokoro/tests/build_cloud_run.sh b/.kokoro/tests/build_cloud_run.sh index bd4d7cadfc0..2dee4eeb633 100755 --- a/.kokoro/tests/build_cloud_run.sh +++ b/.kokoro/tests/build_cloud_run.sh @@ -42,11 +42,10 @@ SUFFIX=${KOKORO_GITHUB_PULL_REQUEST_NUMBER:-${SAMPLE_VERSION:0:12}} export SERVICE_NAME="${SAMPLE_NAME}-${SUFFIX}" export CONTAINER_IMAGE="gcr.io/${GOOGLE_CLOUD_PROJECT}/run-${SAMPLE_NAME}:${SAMPLE_VERSION}" - - # Build the service set -x -gcloud builds submit --tag="${CONTAINER_IMAGE}" --quiet +# TODO: quiet doesn't work for "gcloud build submit" +gcloud builds submit --quiet --tag="${CONTAINER_IMAGE}" > /dev/null 2>&1 & gcloud beta run deploy "${SERVICE_NAME}" \ --image="${CONTAINER_IMAGE}" \ From 9ce91a4bb432b839a4acb7520e7364938f71d322 Mon Sep 17 00:00:00 2001 From: averikitsch Date: Mon, 21 Oct 2019 17:13:32 -0700 Subject: [PATCH 7/7] test remove pipe to file --- .kokoro/tests/build_cloud_run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.kokoro/tests/build_cloud_run.sh b/.kokoro/tests/build_cloud_run.sh index 2dee4eeb633..17e4f91642e 100755 --- a/.kokoro/tests/build_cloud_run.sh +++ b/.kokoro/tests/build_cloud_run.sh @@ -45,7 +45,7 @@ export CONTAINER_IMAGE="gcr.io/${GOOGLE_CLOUD_PROJECT}/run-${SAMPLE_NAME}:${SAMP # Build the service set -x # TODO: quiet doesn't work for "gcloud build submit" -gcloud builds submit --quiet --tag="${CONTAINER_IMAGE}" > /dev/null 2>&1 & +gcloud --quiet builds submit --tag="${CONTAINER_IMAGE}" gcloud beta run deploy "${SERVICE_NAME}" \ --image="${CONTAINER_IMAGE}" \