You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
2.9 KiB
Bash
112 lines
2.9 KiB
Bash
#!/bin/bash
|
|
|
|
# Deploy script for Google Cloud Run + Artifact Registry
|
|
# Usage: ./deploy.sh
|
|
|
|
set -euo pipefail
|
|
|
|
ENV_FILE="${ENV_FILE:-.env.prod}"
|
|
if [ ! -f "${ENV_FILE}" ] && [ -f ".env" ]; then
|
|
ENV_FILE=".env"
|
|
fi
|
|
if [ ! -f "${ENV_FILE}" ]; then
|
|
echo "ERROR: env file not found. Expected ${ENV_FILE} (or .env)."
|
|
exit 1
|
|
fi
|
|
|
|
get_env_value() {
|
|
local key="$1"
|
|
grep -E "^${key}=" "${ENV_FILE}" | tail -n 1 | cut -d'=' -f2- | tr -d '\r'
|
|
}
|
|
|
|
PROJECT_ID=$(gcloud config get-value project)
|
|
REGION="us-central1"
|
|
SERVICE_NAME="orquestrador"
|
|
REPO_NAME="orquestrador"
|
|
IMAGE_NAME="${REGION}-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/${SERVICE_NAME}"
|
|
|
|
if [ -z "${PROJECT_ID}" ]; then
|
|
echo "ERROR: gcloud project is not configured."
|
|
echo "Run: gcloud config set project <your-project-id>"
|
|
exit 1
|
|
fi
|
|
|
|
echo "Starting deploy to Cloud Run"
|
|
echo "Project: ${PROJECT_ID}"
|
|
echo "Region: ${REGION}"
|
|
echo "Env file: ${ENV_FILE}"
|
|
|
|
echo "1) Checking auth..."
|
|
ACTIVE_ACCOUNT=$(gcloud auth list --filter=status:ACTIVE --format="value(account)")
|
|
if [ -z "${ACTIVE_ACCOUNT}" ]; then
|
|
echo "ERROR: no active gcloud account. Run: gcloud auth login"
|
|
exit 1
|
|
fi
|
|
echo "Authenticated as: ${ACTIVE_ACCOUNT}"
|
|
|
|
echo "2) Enabling required APIs..."
|
|
gcloud services enable \
|
|
run.googleapis.com \
|
|
cloudbuild.googleapis.com \
|
|
artifactregistry.googleapis.com \
|
|
aiplatform.googleapis.com \
|
|
sqladmin.googleapis.com \
|
|
--quiet
|
|
|
|
echo "3) Building image with Cloud Build..."
|
|
gcloud builds submit \
|
|
--config=cloudbuild.yaml \
|
|
--substitutions=_REGION="${REGION}",_REPO_NAME="${REPO_NAME}",_IMAGE_NAME="${SERVICE_NAME}"
|
|
|
|
echo "4) Deploying to Cloud Run..."
|
|
|
|
ENV_VARS=""
|
|
while IFS= read -r line || [[ -n "$line" ]]; do
|
|
[[ "$line" =~ ^[[:space:]]*# ]] && continue
|
|
[[ -z "${line// }" ]] && continue
|
|
if [[ "$line" == *"="* ]]; then
|
|
clean_line=$(echo "$line" | tr -d '\r')
|
|
ENV_VARS="${ENV_VARS}${clean_line},"
|
|
fi
|
|
done < "${ENV_FILE}"
|
|
ENV_VARS="${ENV_VARS%,}"
|
|
|
|
CLOUD_SQL_CONN=$(get_env_value "CLOUD_SQL_CONNECTION_NAME")
|
|
RUN_VPC_CONN=$(get_env_value "RUN_VPC_CONNECTOR")
|
|
RUN_VPC_EGRESS=$(get_env_value "RUN_VPC_EGRESS")
|
|
|
|
DEPLOY_ARGS=(
|
|
--image="${IMAGE_NAME}:latest"
|
|
--region="${REGION}"
|
|
--platform=managed
|
|
--memory=512Mi
|
|
--cpu=1
|
|
--timeout=3600
|
|
--max-instances=10
|
|
--allow-unauthenticated
|
|
)
|
|
|
|
if [ -n "${ENV_VARS}" ]; then
|
|
DEPLOY_ARGS+=(--set-env-vars="${ENV_VARS}")
|
|
else
|
|
echo "WARN: no env vars found in ${ENV_FILE}"
|
|
fi
|
|
|
|
if [ -n "${CLOUD_SQL_CONN}" ]; then
|
|
DEPLOY_ARGS+=(--add-cloudsql-instances="${CLOUD_SQL_CONN}")
|
|
echo "Cloud SQL socket enabled: ${CLOUD_SQL_CONN}"
|
|
fi
|
|
|
|
if [ -n "${RUN_VPC_CONN}" ]; then
|
|
DEPLOY_ARGS+=(--vpc-connector="${RUN_VPC_CONN}")
|
|
DEPLOY_ARGS+=(--vpc-egress="${RUN_VPC_EGRESS:-private-ranges-only}")
|
|
echo "VPC connector enabled: ${RUN_VPC_CONN}"
|
|
fi
|
|
|
|
gcloud run deploy "${SERVICE_NAME}" "${DEPLOY_ARGS[@]}"
|
|
|
|
echo "Deploy finished."
|
|
echo "Service URL:"
|
|
gcloud run services describe "${SERVICE_NAME}" --region="${REGION}" --format='value(status.url)'
|
|
|