Skip to content

Commit 6d24381

Browse files
authored
Update Kokoro tests for Cloud Run (GoogleCloudPlatform#1629)
* 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
1 parent 9dc5ddc commit 6d24381

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

.kokoro/tests/build_cloud_run.sh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/bash
2+
3+
# Copyright 2019 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.
16+
17+
set -eo pipefail
18+
19+
# Register post-test cleanup.
20+
# Only needed if deploy completed.
21+
function cleanup {
22+
set -x
23+
gcloud --quiet container images delete "${CONTAINER_IMAGE}" || true
24+
gcloud beta run services delete ${SERVICE_NAME} \
25+
--platform=managed \
26+
--region="${REGION:-us-central1}" \
27+
--quiet
28+
}
29+
trap cleanup EXIT
30+
31+
requireEnv() {
32+
test "${!1}" || (echo "Environment Variable '$1' not found" && exit 1)
33+
}
34+
requireEnv SAMPLE_NAME
35+
36+
# Version is in the format <PR#>-<GIT COMMIT SHA>.
37+
# Ensures PR-based triggers of the same branch don't collide if Kokoro attempts
38+
# to run them concurrently.
39+
export SAMPLE_VERSION="${KOKORO_GIT_COMMIT:-latest}"
40+
# Builds not triggered by a PR will fall back to the commit hash then "latest".
41+
SUFFIX=${KOKORO_GITHUB_PULL_REQUEST_NUMBER:-${SAMPLE_VERSION:0:12}}
42+
export SERVICE_NAME="${SAMPLE_NAME}-${SUFFIX}"
43+
export CONTAINER_IMAGE="gcr.io/${GOOGLE_CLOUD_PROJECT}/run-${SAMPLE_NAME}:${SAMPLE_VERSION}"
44+
45+
# Build the service
46+
set -x
47+
# TODO: quiet doesn't work for "gcloud build submit"
48+
gcloud --quiet builds submit --tag="${CONTAINER_IMAGE}"
49+
50+
gcloud beta run deploy "${SERVICE_NAME}" \
51+
--image="${CONTAINER_IMAGE}" \
52+
--region="${REGION:-us-central1}" \
53+
--platform=managed \
54+
--quiet
55+
56+
set +x
57+
58+
echo 'Cloud Run Links:'
59+
echo "- Logs: https://console.cloud.google.com/logs/viewer?project=${GOOGLE_CLOUD_PROJECT}&resource=cloud_run_revision%2Fservice_name%2F${SERVICE_NAME}"
60+
echo "- Console: https://console.cloud.google.com/run/detail/${REGION:-us-central1}/${SERVICE_NAME}/metrics?project=${GOOGLE_CLOUD_PROJECT}"
61+
62+
echo
63+
echo '---'
64+
echo
65+
66+
67+
# Do not use exec to preserve trap behavior.
68+
"$@"

.kokoro/tests/run_tests.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ set -eo pipefail
1919
# Enables `**` to include files nested inside sub-folders
2020
shopt -s globstar
2121

22+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
23+
2224
# `--debug` can be added make local testing of this script easier
2325
if [[ $* == *--script-debug* ]]; then
2426
SCRIPT_DEBUG="true"
@@ -42,6 +44,7 @@ fi
4244

4345
if [[ "$SCRIPT_DEBUG" != "true" ]]; then
4446
# Update `gcloud` and log versioning for debugging.
47+
gcloud components install beta --quiet
4548
gcloud components update --quiet
4649
echo "********** GCLOUD INFO ***********"
4750
gcloud -v
@@ -70,7 +73,7 @@ fi
7073
# Package local jetty dependency for Java11 samples
7174
if [[ "$JAVA_VERSION" == "11" ]]; then
7275
cd appengine-java11/appengine-simple-jetty-main/
73-
mvn install
76+
mvn install --quiet
7477
cd ../../
7578
fi
7679

@@ -135,6 +138,21 @@ for file in **/pom.xml; do
135138
echo -e "\n Testing completed.\n"
136139
fi
137140

141+
# Build and deploy Cloud Run samples
142+
if [[ "$file" == "run/"* ]]; then
143+
export SAMPLE_NAME=${file#"run/"}
144+
# chmod 755 "$SCRIPT_DIR"/build_cloud_run.sh
145+
"$SCRIPT_DIR"/build_cloud_run.sh
146+
EXIT=$?
147+
148+
if [[ $EXIT -ne 0 ]]; then
149+
RTN=1
150+
echo -e "\n Cloud Run build/deploy failed: gcloud returned a non-zero exit code. \n"
151+
else
152+
echo -e "\n Cloud Run build/deploy completed.\n"
153+
fi
154+
fi
155+
138156
done
139157

140158
exit "$RTN"

run/hello-broken/pom.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ limitations under the License.
1616
<groupId>com.example.cloudrun</groupId>
1717
<artifactId>hello-broken</artifactId>
1818
<version>0.0.1-SNAPSHOT</version>
19-
<!-- 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. -->
19+
<!--
20+
The parent pom defines common style checks and testing strategies for our samples.
21+
Removing or replacing it should not affect the execution of the samples in anyway.
22+
-->
2023
<parent>
2124
<groupId>com.google.cloud.samples</groupId>
2225
<artifactId>shared-configuration</artifactId>

0 commit comments

Comments
 (0)