diff --git a/.kokoro/tests/build_cloud_run.sh b/.kokoro/tests/build_cloud_run.sh new file mode 100755 index 00000000000..17e4f91642e --- /dev/null +++ b/.kokoro/tests/build_cloud_run.sh @@ -0,0 +1,68 @@ +#!/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 + +# 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) +} +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 +# TODO: quiet doesn't work for "gcloud build submit" +gcloud --quiet builds submit --tag="${CONTAINER_IMAGE}" + +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}" + +echo +echo '---' +echo + + +# Do not use exec to preserve trap behavior. +"$@" diff --git a/.kokoro/tests/run_tests.sh b/.kokoro/tests/run_tests.sh index 1bb72c7667c..44c53f5ca1d 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" @@ -42,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 @@ -70,7 +73,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 @@ -135,6 +138,21 @@ for file in **/pom.xml; do echo -e "\n Testing completed.\n" fi + # Build and deploy Cloud Run samples + if [[ "$file" == "run/"* ]]; then + export SAMPLE_NAME=${file#"run/"} + # 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" 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