|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +cloud_run_set_defaults() { |
| 4 | + CLOUD_RUN_PROJECT="${CLOUD_RUN_PROJECT:-policyengine-api}" |
| 5 | + CLOUD_RUN_REGION="${CLOUD_RUN_REGION:-us-central1}" |
| 6 | + CLOUD_RUN_SERVICE="${CLOUD_RUN_SERVICE:-policyengine-api}" |
| 7 | + CLOUD_RUN_ARTIFACT_REPOSITORY="${CLOUD_RUN_ARTIFACT_REPOSITORY:-policyengine-api}" |
| 8 | + CLOUD_RUN_RUNTIME_SERVICE_ACCOUNT="${CLOUD_RUN_RUNTIME_SERVICE_ACCOUNT:-policyengine-api-cr-runtime@policyengine-api.iam.gserviceaccount.com}" |
| 9 | + CLOUD_RUN_CLOUD_SQL_INSTANCE="${CLOUD_RUN_CLOUD_SQL_INSTANCE:-policyengine-api:us-central1:policyengine-api-data}" |
| 10 | + CLOUD_RUN_CPU="${CLOUD_RUN_CPU:-4}" |
| 11 | + CLOUD_RUN_MEMORY="${CLOUD_RUN_MEMORY:-16Gi}" |
| 12 | + CLOUD_RUN_TIMEOUT="${CLOUD_RUN_TIMEOUT:-300}" |
| 13 | + CLOUD_RUN_MIN_INSTANCES="${CLOUD_RUN_MIN_INSTANCES:-0}" |
| 14 | + CLOUD_RUN_MAX_INSTANCES="${CLOUD_RUN_MAX_INSTANCES:-1}" |
| 15 | + CLOUD_RUN_PORT="${CLOUD_RUN_PORT:-8080}" |
| 16 | + CLOUD_RUN_POLICYENGINE_DB_PASSWORD_SECRET="${CLOUD_RUN_POLICYENGINE_DB_PASSWORD_SECRET:-policyengine-api-prod-db-password:latest}" |
| 17 | + CLOUD_RUN_GITHUB_MICRODATA_TOKEN_SECRET="${CLOUD_RUN_GITHUB_MICRODATA_TOKEN_SECRET:-policyengine-api-prod-github-microdata-token:latest}" |
| 18 | + CLOUD_RUN_ANTHROPIC_API_KEY_SECRET="${CLOUD_RUN_ANTHROPIC_API_KEY_SECRET:-policyengine-api-prod-anthropic-api-key:latest}" |
| 19 | + CLOUD_RUN_OPENAI_API_KEY_SECRET="${CLOUD_RUN_OPENAI_API_KEY_SECRET:-policyengine-api-prod-openai-api-key:latest}" |
| 20 | + CLOUD_RUN_HUGGING_FACE_TOKEN_SECRET="${CLOUD_RUN_HUGGING_FACE_TOKEN_SECRET:-policyengine-api-prod-hugging-face-token:latest}" |
| 21 | + |
| 22 | + local sha |
| 23 | + sha="${GITHUB_SHA:-local}" |
| 24 | + CLOUD_RUN_IMAGE_TAG="${CLOUD_RUN_IMAGE_TAG:-${sha}}" |
| 25 | + CLOUD_RUN_IMAGE_URI="${CLOUD_RUN_IMAGE_URI:-${CLOUD_RUN_REGION}-docker.pkg.dev/${CLOUD_RUN_PROJECT}/${CLOUD_RUN_ARTIFACT_REPOSITORY}/${CLOUD_RUN_SERVICE}:${CLOUD_RUN_IMAGE_TAG}}" |
| 26 | + |
| 27 | + local short_sha |
| 28 | + short_sha="${sha:0:7}" |
| 29 | + CLOUD_RUN_TAG="${CLOUD_RUN_TAG:-stage3-${GITHUB_RUN_NUMBER:-local}-${short_sha}}" |
| 30 | + |
| 31 | + export CLOUD_RUN_PROJECT |
| 32 | + export CLOUD_RUN_REGION |
| 33 | + export CLOUD_RUN_SERVICE |
| 34 | + export CLOUD_RUN_ARTIFACT_REPOSITORY |
| 35 | + export CLOUD_RUN_RUNTIME_SERVICE_ACCOUNT |
| 36 | + export CLOUD_RUN_CLOUD_SQL_INSTANCE |
| 37 | + export CLOUD_RUN_CPU |
| 38 | + export CLOUD_RUN_MEMORY |
| 39 | + export CLOUD_RUN_TIMEOUT |
| 40 | + export CLOUD_RUN_MIN_INSTANCES |
| 41 | + export CLOUD_RUN_MAX_INSTANCES |
| 42 | + export CLOUD_RUN_PORT |
| 43 | + export CLOUD_RUN_POLICYENGINE_DB_PASSWORD_SECRET |
| 44 | + export CLOUD_RUN_GITHUB_MICRODATA_TOKEN_SECRET |
| 45 | + export CLOUD_RUN_ANTHROPIC_API_KEY_SECRET |
| 46 | + export CLOUD_RUN_OPENAI_API_KEY_SECRET |
| 47 | + export CLOUD_RUN_HUGGING_FACE_TOKEN_SECRET |
| 48 | + export CLOUD_RUN_IMAGE_TAG |
| 49 | + export CLOUD_RUN_IMAGE_URI |
| 50 | + export CLOUD_RUN_TAG |
| 51 | +} |
| 52 | + |
| 53 | +cloud_run_require_env() { |
| 54 | + local missing=() |
| 55 | + local name |
| 56 | + |
| 57 | + for name in "$@"; do |
| 58 | + if [[ -z "${!name:-}" ]]; then |
| 59 | + missing+=("${name}") |
| 60 | + fi |
| 61 | + done |
| 62 | + |
| 63 | + if (( ${#missing[@]} > 0 )); then |
| 64 | + echo "Missing required Cloud Run deployment configuration: ${missing[*]}" >&2 |
| 65 | + return 1 |
| 66 | + fi |
| 67 | +} |
| 68 | + |
| 69 | +cloud_run_run() { |
| 70 | + if [[ "${CLOUD_RUN_DRY_RUN:-0}" == "1" ]]; then |
| 71 | + printf '+' |
| 72 | + printf ' %q' "$@" |
| 73 | + printf '\n' |
| 74 | + return 0 |
| 75 | + fi |
| 76 | + |
| 77 | + "$@" |
| 78 | +} |
0 commit comments