#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" cd "${ROOT_DIR}" if [[ -n "$(git status --porcelain)" ]]; then echo "ERROR: Git working directory is not clean. Aborting." >&2 git status exit 1 fi echo "==> Git working directory is clean." PROJECT_ID="propane-will-491900-m5" REGION="us-central1" REPOSITORY="us-central1-docker.pkg.dev/${PROJECT_ID}/osvauco-repo" MCP_SERVICE="opax-mcp" MCP_IMAGE="${REPOSITORY}/opax-mcp" # This is now aligned with opax-mcp.yaml MCP_OLLAMA_BASE_URL="http://10.128.0.15:11434" COMMIT_SHA=$(git rev-parse --short HEAD) echo "==> Submitting MCP Cloud Build" BUILD_ID="$( gcloud builds submit \ --async \ --project="${PROJECT_ID}" \ --region="${REGION}" \ --config="cloudbuild.build-only.yaml" \ --format="value(id)" \ . )" if [[ -z "${BUILD_ID}" ]]; then echo "ERROR: Cloud Build did not return a build ID." >&2 exit 1 fi echo "==> Cloud Build ID: ${BUILD_ID}" echo "==> Streaming Cloud Build logs" set +e gcloud builds log "${BUILD_ID}" \ --project="${PROJECT_ID}" \ --region="${REGION}" \ --stream BUILD_LOG_EXIT_CODE=$? set -e BUILD_STATUS="$( gcloud builds describe "${BUILD_ID}" \ --project="${PROJECT_ID}" \ --region="${REGION}" \ --format="value(status)" )" if [[ "${BUILD_STATUS}" != "SUCCESS" ]]; then echo "ERROR: Cloud Build did not succeed. Status: ${BUILD_STATUS}. Aborting deploy." >&2 exit 1 fi echo "==> Cloud Build status: SUCCESS." IMAGE_REF="$( gcloud builds describe "${BUILD_ID}" \ --project="${PROJECT_ID}" \ --region="${REGION}" \ --format="value(results.images[0].name)" )" if [[ -z "${IMAGE_REF}" ]]; then IMAGE_REF="${MCP_IMAGE}:${BUILD_ID}" fi if [[ "${IMAGE_REF}" == *@sha256:* ]]; then DEPLOY_IMAGE="${IMAGE_REF}" else IMAGE_DIGEST="$( gcloud artifacts docker images describe "${IMAGE_REF}" \ --project="${PROJECT_ID}" \ --format="value(image_summary.digest)" )" if [[ -z "${IMAGE_DIGEST}" ]]; then echo "ERROR: No pushed MCP image digest was found." >&2 echo "Cloud Build status: ${BUILD_STATUS}" >&2 echo "Cloud Build log exit code: ${BUILD_LOG_EXIT_CODE}" >&2 exit 1 fi DEPLOY_IMAGE="${MCP_IMAGE}@${IMAGE_DIGEST}" fi echo "==> Deploying immutable MCP image:" echo "${DEPLOY_IMAGE}" gcloud run deploy "${MCP_SERVICE}" \ --project="${PROJECT_ID}" \ --region="${REGION}" \ --image="${DEPLOY_IMAGE}" \ --tag="candidate-${COMMIT_SHA}" \ --no-traffic \ --service-account="jason-vauger@propane-will-491900-m5.iam.gserviceaccount.com" \ --port="8080" \ --min-instances="0" \ --max-instances="5" \ --cpu="1" \ --memory="512Mi" \ --concurrency="80" \ --timeout="60" \ --network="default" \ --subnet="default" \ --vpc-egress="private-ranges-only" \ --update-env-vars="^|^GOOGLE_CLOUD_PROJECT=propane-will-491900-m5|OLLAMA_BASE_URL=http://10.128.0.15:11434|EMMA_MODEL=gemma3:4b|EMMA_FAST_MODEL=gemma3:4b|EMMA_LIGHT_MODEL=qwen2.5:3b|GITEA_URL=https://git.vauco.no|GITEA_REPO=chris/OSVauco|STATIC_BUCKET=opax-vauco-static|CLOUD_RUN_SERVICE=osvauco-agent|CLOUD_BUILD_TRIGGER_ID=38423976-91ff-4ff4-859e-1f262344c609|OPAX_IAP_CLIENT_ID=357036551735-kq8nt7ld38hfqlcfb3n52ef7tala4meo.apps.googleusercontent.com|GMAIL_DEFAULT_SENDER=jason.vauger@vauco.no|GMAIL_ALLOWED_SENDERS=jason.vauger@vauco.no,emma.vauger@vauco.no|OSVAUCO_AGENT_URL=https://osvauco-agent-357036551735.us-central1.run.app" \ --update-secrets="GITEA_TOKEN=gitea-token:latest,INTERNAL_API_KEY=INTERNAL_API_KEY:latest,MCP_SECRET=mcp-server-key:latest,TWILIO_ACCOUNT_SID=TWILIO_ACCOUNT_SID:latest,TWILIO_AUTH_TOKEN=TWILIO_AUTH_TOKEN:latest,TWILIO_FROM_NUMBER=TWILIO_FROM_NUMBER:latest" \ --quiet echo echo "==> Candidate revision created successfully." echo "Candidate tag: candidate-${COMMIT_SHA}" echo echo "No production traffic has been promoted." echo "Next steps: Perform health and smoke tests against the candidate URL, then promote traffic manually."