|
| 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 | +"$@" |
0 commit comments