# cloudbuild.yaml — Auto-build, cost-check og deploy osvauco-agent på push til main steps: # Steg 1: Kost-sjekk før deploy - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk' id: cost-check entrypoint: bash args: - '-c' - | echo "=== Cost Check ===" bash infrastructure/09-cost-check.sh || true echo "=== Cost Check ferdig ===" # Steg 2: Bygg Docker-image - name: 'gcr.io/cloud-builders/docker' id: build args: - 'build' - '--cache-from' - '${_REGION}-docker.pkg.dev/${PROJECT_ID}/${_ARTIFACT_REPO}/osvauco-agent:latest' - '-t' - '${_REGION}-docker.pkg.dev/${PROJECT_ID}/${_ARTIFACT_REPO}/osvauco-agent:$BUILD_ID' - '-t' - '${_REGION}-docker.pkg.dev/${PROJECT_ID}/${_ARTIFACT_REPO}/osvauco-agent:latest' - '.' - '-f' - 'agents/core-logic/Dockerfile' # Steg 3: Push image - name: 'gcr.io/cloud-builders/docker' id: push waitFor: ['build'] args: - 'push' - '--all-tags' - '${_REGION}-docker.pkg.dev/${PROJECT_ID}/${_ARTIFACT_REPO}/osvauco-agent' # Steg 4: Deploy til Cloud Run - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk' id: deploy waitFor: ['push'] entrypoint: gcloud args: - 'run' - 'deploy' - '${_CLOUD_RUN_SERVICE}' - '--image=${_REGION}-docker.pkg.dev/${PROJECT_ID}/${_ARTIFACT_REPO}/osvauco-agent:$BUILD_ID' - '--region=${_REGION}' - '--project=${PROJECT_ID}' - '--service-account=${_AGENT_SA}' - '--set-env-vars=GOOGLE_CLOUD_PROJECT=${PROJECT_ID},GOOGLE_CLOUD_LOCATION=${_GEMINI_LOCATION},GOOGLE_GENAI_USE_VERTEXAI=True,BILLING_TABLE=billing_export.gcp_billing_export_v1_0171F6_057E6B_A260BA,RAG_CORPUS=projects/propane-will-491900-m5/locations/europe-west4/ragCorpora/6917529027641081856,RAG_LOCATION=europe-west4' - '--set-secrets=GOOGLE_CLIENT_ID=GOOGLE_CLIENT_ID:1,SESSION_SECRET=SESSION_SECRET:1,GOOGLE_CLIENT_SECRET=GOOGLE_CLIENT_SECRET:1,MCP_SECRET=mcp-server-key:latest' - '--no-allow-unauthenticated' - '--port=8080' - '--memory=1Gi' - '--cpu=1' - '--min-instances=0' - '--max-instances=3' - '--quiet' # Steg 5: Smoke test — verifiser at agenten svarer etter deploy - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk' id: smoke-test waitFor: ['deploy'] entrypoint: bash args: - '-c' - | echo "=== Smoke Test ===" BASE_URL="https://${_CLOUD_RUN_URL}" # Vent på at Cloud Run-instansen er klar (cold start kan ta opptil 90s) # Henter fersk token per forsøk siden token kan utløpe under lang cold start echo "Venter på /health ..." STATUS="000" for i in $$(seq 1 9); do TOKEN=$$(gcloud auth print-identity-token --audiences="$$BASE_URL" 2>/dev/null) if [ -z "$$TOKEN" ]; then echo " /health forsøk $$i: kunne ikke hente token, venter..." sleep 10 continue fi STATUS=$$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bearer $$TOKEN" "$$BASE_URL/health") echo " /health forsøk $$i: HTTP $$STATUS" [ "$$STATUS" = "200" ] && break sleep 10 done if [ "$$STATUS" != "200" ]; then echo "❌ /health svarte ikke 200 etter 90s (siste: $$STATUS)" exit 1 fi # Kjør /run med fersk token TOKEN=$$(gcloud auth print-identity-token --audiences="$$BASE_URL") RESPONSE=$$(curl -s -w " HTTP_STATUS:%{http_code}" -H "Authorization: Bearer $$TOKEN" -H "Content-Type: application/json" -d '{"user_id": "ci", "session_id": "smoke-$$BUILD_ID", "message": "ping"}' "$$BASE_URL/run") HTTP_STATUS=$$(echo "$$RESPONSE" | grep "HTTP_STATUS:" | cut -d: -f2) BODY=$$(echo "$$RESPONSE" | grep -v "HTTP_STATUS:") echo " /run HTTP $$HTTP_STATUS" if [ "$$HTTP_STATUS" != "200" ]; then echo "⚠️ /run svarte HTTP $$HTTP_STATUS i smoke-test (ikke fatal for deploy)" echo "Body: $$BODY" else echo "✅ Smoke test OK: $$BODY" fi echo "=== Smoke Test ferdig ===" # Steg 6: Deploy-notifikasjon til webhook/chat-app - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk' id: notify waitFor: ['smoke-test'] entrypoint: bash args: - '-c' - | WH_URL=$$(gcloud secrets versions access latest --secret=webhook-url --project=${PROJECT_ID} 2>/dev/null || echo "") if [[ -n "$$WH_URL" ]]; then NOTIFY_MSG="✅ OSVauco Deploy fullført. Commit: $COMMIT_SHA, Service: ${_CLOUD_RUN_SERVICE}, Region: ${_REGION}" curl -s -X POST "$$WH_URL" \ -H 'Content-Type: application/json' \ -d "{\"text\": \"$$NOTIFY_MSG\"}" || true echo "✓ Deploy-notifikasjon sendt" else echo "⚠️ Ingen webhook-url i Secret Manager — hopper over varsling" fi substitutions: _REGION: us-central1 _ARTIFACT_REPO: osvauco-repo _CLOUD_RUN_SERVICE: osvauco-agent _CLOUD_RUN_URL: osvauco-agent-357036551735.us-central1.run.app _AGENT_SA: osvauco-agent-sa@propane-will-491900-m5.iam.gserviceaccount.com _GEMINI_LOCATION: global options: substitutionOption: ALLOW_LOOSE logging: CLOUD_LOGGING_ONLY