Skip to content

Commit

Permalink
Update Kokoro tests for Cloud Run (#1629)
Browse files Browse the repository at this point in the history
* Kokoro tests

* change for kokoro to run

* update bash script

* fix clean up and wildcard

* sample name fixed

* Add todo

* test remove pipe to file
  • Loading branch information
averikitsch authored Oct 22, 2019
1 parent 9dc5ddc commit 6d24381
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
68 changes: 68 additions & 0 deletions .kokoro/tests/build_cloud_run.sh
Original file line number Diff line number Diff line change
@@ -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 <PR#>-<GIT COMMIT SHA>.
# 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.
"$@"
20 changes: 19 additions & 1 deletion .kokoro/tests/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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"
5 changes: 4 additions & 1 deletion run/hello-broken/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ limitations under the License.
<groupId>com.example.cloudrun</groupId>
<artifactId>hello-broken</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- The parent pom defines common style checks and testing strategies for our samples. Removing or replacing it should not affect the execution of the samples in anyway. -->
<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
Expand Down

0 comments on commit 6d24381

Please sign in to comment.