|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Copyright 2020 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 | +# https://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 | +# Make our include guard clean against set -o nounset. |
| 18 | +test -n "${CI_KOKORO_LIB_CACHE_SH__:-}" || declare -i CI_KOKORO_LIB_CACHE_SH__=0 |
| 19 | +if ((CI_KOKORO_LIB_CACHE_SH__++ != 0)); then |
| 20 | + return 0 |
| 21 | +fi # include guard |
| 22 | + |
| 23 | +source module /ci/kokoro/lib/gcloud.sh |
| 24 | +source module /ci/lib/io.sh |
| 25 | + |
| 26 | +readonly CACHE_KEYFILE="${KOKORO_GFILE_DIR:-/dev/shm}/build-results-service-account.json" |
| 27 | + |
| 28 | +cache_download_enabled() { |
| 29 | + if [[ ! -f "${CACHE_KEYFILE}" ]]; then |
| 30 | + io::log "Service account for cache access is not configured." |
| 31 | + io::log "No attempt will be made to download the cache, exit with success." |
| 32 | + return 1 |
| 33 | + fi |
| 34 | + |
| 35 | + if [[ "${RUNNING_CI:-}" != "yes" ]]; then |
| 36 | + io::log "Cache not downloaded as this is not a CI build." |
| 37 | + return 1 |
| 38 | + fi |
| 39 | + |
| 40 | + return 0 |
| 41 | +} |
| 42 | + |
| 43 | +cache_gcloud_cleanup() { |
| 44 | + revoke_service_account_keyfile "${CACHE_KEYFILE}" || true |
| 45 | + delete_gcloud_config || true |
| 46 | +} |
| 47 | + |
| 48 | +cache_download_tarball() { |
| 49 | + local -r GCS_FOLDER="$1" |
| 50 | + local -r DESTINATION="$2" |
| 51 | + local -r FILENAME="$3" |
| 52 | + |
| 53 | + trap cache_gcloud_cleanup RETURN |
| 54 | + create_gcloud_config |
| 55 | + activate_service_account_keyfile "${CACHE_KEYFILE}" |
| 56 | + |
| 57 | + io::log_h2 "Downloading build cache ${FILENAME} from ${GCS_FOLDER}" |
| 58 | + io::log "gcloud configuration" |
| 59 | + "${GCLOUD_BIN}" version |
| 60 | + env "CLOUDSDK_ACTIVE_CONFIG_NAME=${GCLOUD_CONFIG}" \ |
| 61 | + "${GCLOUD_BIN}" --quiet storage "${CACHE_GSUTIL_DEBUG:--q}" \ |
| 62 | + cp "gs://${GCS_FOLDER}/${FILENAME}" "${DESTINATION}" |
| 63 | +} |
| 64 | + |
| 65 | +cache_upload_enabled() { |
| 66 | + if [[ ! -f "${CACHE_KEYFILE}" ]]; then |
| 67 | + io::log "Service account for cache access is not configured." |
| 68 | + io::log "No attempt will be made to upload the cache, exit with success." |
| 69 | + return 1 |
| 70 | + fi |
| 71 | + |
| 72 | + if [[ "${RUNNING_CI:-}" != "yes" || "${KOKORO_JOB_TYPE:-}" != "CONTINUOUS_INTEGRATION" ]]; then |
| 73 | + io::log "Cache not updated as this is not a CI build or it is a PR build." |
| 74 | + return 1 |
| 75 | + fi |
| 76 | + return 0 |
| 77 | +} |
| 78 | + |
| 79 | +cache_upload_tarball() { |
| 80 | + local -r SOURCE_DIRECTORY="$1" |
| 81 | + local -r FILENAME="$2" |
| 82 | + local -r GCS_FOLDER="$3" |
| 83 | + |
| 84 | + io::log_h2 "Uploading build cache ${FILENAME} to ${GCS_FOLDER}" |
| 85 | + io::log "gsutil configuration" |
| 86 | + gsutil version -l |
| 87 | + |
| 88 | + trap cache_gcloud_cleanup RETURN |
| 89 | + create_gcloud_config |
| 90 | + activate_service_account_keyfile "${CACHE_KEYFILE}" |
| 91 | + env "CLOUDSDK_ACTIVE_CONFIG_NAME=${GCLOUD_CONFIG}" \ |
| 92 | + "${GCLOUD_BIN}" --quiet storage "${CACHE_GSUTIL_DEBUG:--q}" \ |
| 93 | + cp "${SOURCE_DIRECTORY}/${FILENAME}" "gs://${CACHE_FOLDER}/" |
| 94 | + |
| 95 | + io::log "Upload completed" |
| 96 | +} |
0 commit comments