fix(CI3): bruk regional parent i ListBuildsRequest for us-central1

This commit is contained in:
chrischristiansen-glitch 2026-06-10 04:14:48 +02:00
parent e4c79c999d
commit 3c69490a68

View File

@ -6,18 +6,17 @@ import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException, Depends, Header
from fastapi.responses import JSONResponse
from pydantic import BaseModel
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "propane-will-491900-m5")
REGION = os.environ.get("REGION", "us-central1")
STATIC_BUCKET = os.environ.get("STATIC_BUCKET", "opax-vauco-static")
CLOUD_RUN_SERVICE = os.environ.get("CLOUD_RUN_SERVICE", "osvauco-agent")
MCP_SECRET = os.environ.get("MCP_SECRET", "")
BUILD_TRIGGER_ID = os.environ.get("CLOUD_BUILD_TRIGGER_ID", "38423976-91ff-4ff4-859e-1f262344c609")
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "propane-will-491900-m5")
REGION = os.environ.get("REGION", "us-central1")
STATIC_BUCKET = os.environ.get("STATIC_BUCKET", "opax-vauco-static")
CLOUD_RUN_SERVICE = os.environ.get("CLOUD_RUN_SERVICE", "osvauco-agent")
MCP_SECRET = os.environ.get("MCP_SECRET", "")
BUILD_TRIGGER_ID = os.environ.get("CLOUD_BUILD_TRIGGER_ID", "38423976-91ff-4ff4-859e-1f262344c609")
def verify_token(x_mcp_key: str = Header(default="")):
@ -44,7 +43,7 @@ class ToolCallRequest(BaseModel):
@asynccontextmanager
async def lifespan(app: FastAPI):
logger.info(f"OPAX-MCP starting | project={PROJECT_ID} | trigger={BUILD_TRIGGER_ID}")
logger.info(f"OPAX-MCP starting | project={PROJECT_ID} | region={REGION} | trigger={BUILD_TRIGGER_ID}")
yield
app = FastAPI(title="OPAX-MCP", version="1.0.0", lifespan=lifespan)
@ -52,19 +51,17 @@ app = FastAPI(title="OPAX-MCP", version="1.0.0", lifespan=lifespan)
@app.get("/health")
async def health():
return {"status": "ok", "service": "opax-mcp", "project": PROJECT_ID}
return {"status": "ok", "service": "opax-mcp", "project": PROJECT_ID, "region": REGION}
@app.get("/mcp/tools")
async def list_tools(_: bool = Depends(verify_token)):
return {
"tools": [
{"name": "push_static", "description": "Last opp statisk fil til GCS (opax.vauco.no)", "required": ["file_path", "content"]},
{"name": "get_build_status", "description": "Hent status på siste Cloud Build for osvauco-agent-main-trigger", "required": []},
{"name": "deploy_service", "description": "Trigger ny Cloud Build deploy (HITL)", "required": []},
{"name": "get_logs", "description": "Hent Cloud Run-logger for osvauco-agent", "required": []},
]
}
return {"tools": [
{"name": "push_static", "description": "Last opp statisk fil til GCS (opax.vauco.no)"},
{"name": "get_build_status", "description": "Hent status på siste Cloud Build for osvauco-agent-main-trigger"},
{"name": "deploy_service", "description": "Trigger ny Cloud Build deploy (HITL)"},
{"name": "get_logs", "description": "Hent Cloud Run-logger for osvauco-agent"},
]}
@app.post("/tools/push_static")
@ -72,8 +69,7 @@ async def push_static(req: PushStaticRequest, _: bool = Depends(verify_token)):
try:
from google.cloud import storage
client = storage.Client(project=PROJECT_ID)
bucket = client.bucket(STATIC_BUCKET)
blob = bucket.blob(req.file_path)
blob = client.bucket(STATIC_BUCKET).blob(req.file_path)
blob.cache_control = req.cache_control
blob.upload_from_string(req.content.encode("utf-8"), content_type=req.content_type)
logger.info(f"[push_static] {req.file_path} ({len(req.content)} bytes)")
@ -89,19 +85,21 @@ async def push_static(req: PushStaticRequest, _: bool = Depends(verify_token)):
async def get_build_status(_: bool = Depends(verify_token)):
"""
Henter siste build fra osvauco-agent-main-trigger (us-central1).
Filtrerer trigger_id for å unngå global/andre builds.
Bruker regional parent-format: projects/{project}/locations/{region}
"""
try:
from google.cloud.devtools import cloudbuild_v1
client = cloudbuild_v1.CloudBuildClient()
# Regional builds krever parent i stedet for project_id
parent = f"projects/{PROJECT_ID}/locations/{REGION}"
request = cloudbuild_v1.ListBuildsRequest(
project_id=PROJECT_ID,
parent=parent,
filter=f'trigger_id="{BUILD_TRIGGER_ID}"',
page_size=1,
)
builds = list(client.list_builds(request=request))
if not builds:
return {"status": "unknown", "message": f"Ingen builds for trigger {BUILD_TRIGGER_ID}"}
return {"status": "unknown", "message": f"Ingen builds for trigger {BUILD_TRIGGER_ID} i {REGION}"}
b = builds[0]
status_map = {1:"queued",2:"working",3:"success",4:"failure",
5:"internal_error",6:"timeout",7:"cancelled"}
@ -119,6 +117,7 @@ async def get_build_status(_: bool = Depends(verify_token)):
"commit": (b.substitutions or {}).get("SHORT_SHA", ""),
"duration_s": duration_s,
"log_url": b.log_url or "",
"region": REGION,
"trigger_id": BUILD_TRIGGER_ID,
}
except Exception as e: