diff --git a/cloudbuild.yaml b/cloudbuild.yaml index 054b66d..efd491c 100644 --- a/cloudbuild.yaml +++ b/cloudbuild.yaml @@ -14,9 +14,10 @@ steps: # Steg 2: Bygg Docker-image - name: 'gcr.io/cloud-builders/docker' id: build - waitFor: ['cost-check'] 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' @@ -72,17 +73,16 @@ steps: # 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 18); do - TOKEN=$$(gcloud auth print-identity-token \ - --impersonate-service-account=${_AGENT_SA} \ + 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" \ + 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 @@ -90,19 +90,19 @@ steps: done if [ "$$STATUS" != "200" ]; then - echo "❌ /health svarte ikke 200 etter 180s (siste: $$STATUS)" + echo "❌ /health svarte ikke 200 etter 90s (siste: $$STATUS)" exit 1 fi # Kjør /run med fersk token - TOKEN=$$(gcloud auth print-identity-token \ - --impersonate-service-account=${_AGENT_SA} \ + TOKEN=$$(gcloud auth print-identity-token --audiences="$$BASE_URL") - RESPONSE=$$(curl -s -w "\nHTTP_STATUS:%{http_code}" \ - -H "Authorization: Bearer $$TOKEN" \ - -H "Content-Type: application/json" \ - -d '{"user_id": "ci", "session_id": "smoke-$$BUILD_ID", "message": "ping"}' \ + 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) @@ -125,14 +125,18 @@ steps: args: - '-c' - | - WH_URL=$$(gcloud secrets versions access latest \ - --secret=webhook-url \ + 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*\nCommit: \`$COMMIT_SHA\`\nService: \`${_CLOUD_RUN_SERVICE}\`\nRegion: \`${_REGION}\`\nSmoke test: ✅" - curl -s -X POST "$$WH_URL" \ - -H 'Content-Type: application/json' \ - -d "{\"text\": \"$$NOTIFY_MSG\"}" || true + NOTIFY_MSG="✅ *OSVauco Deploy fullført* +Commit: `$COMMIT_SHA` +Service: `${_CLOUD_RUN_SERVICE}` +Region: `${_REGION}` +Smoke test: ✅" + 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" diff --git a/scripts/watch-build.sh b/scripts/watch-build.sh new file mode 100755 index 0000000..13f3d19 --- /dev/null +++ b/scripts/watch-build.sh @@ -0,0 +1,65 @@ +#!/usr/bin/env bash +# watch-build.sh - Velg en Cloud Build trigger, kjør den og stream loggen live. + +set -euo pipefail + +PROJECT_ID="${PROJECT_ID:-propane-will-491900-m5}" +REGION="${REGION:-us-central1}" + +# Farger for output +GREEN="$(tput setaf 2 2>/dev/null || true)" +RED="$(tput setaf 1 2>/dev/null || true)" +YELLOW="$(tput setaf 3 2>/dev/null || true)" +CYAN="$(tput setaf 6 2>/dev/null || true)" +BOLD="$(tput bold 2>/dev/null || true)" +NC="$(tput sgr0 2>/dev/null || true)" + +# Hent tilgjengelige triggers +TRIGGERS=($(gcloud builds triggers list --project="$PROJECT_ID" --region="$REGION" --format="value(name)")) +if [ ${#TRIGGERS[@]} -eq 0 ]; then + echo -e "${RED}Fant ingen build triggers i $PROJECT_ID i region $REGION.${NC}" + exit 1 +fi + +TRIGGER_NAME="${1:-}" + +# Hvis ingen trigger er gitt som argument, vis en meny +if [ -z "$TRIGGER_NAME" ]; then + echo -e "${BOLD}Velg en Cloud Build trigger:${NC}" + select T_NAME in "${TRIGGERS[@]}"; do + if [ -n "$T_NAME" ]; then + TRIGGER_NAME="$T_NAME" + break + else + echo -e "${YELLOW}Ugyldig valg. Prøv igjen.${NC}" + fi + done +fi + +echo -e " +${CYAN}Kjører trigger: ${BOLD}$TRIGGER_NAME${NC} på branch 'main'..." + +# Kjør trigger og hent build-ID +BUILD_ID=$(gcloud builds triggers run "$TRIGGER_NAME" --project="$PROJECT_ID" --region="$REGION" --branch=main --format="value(id)") + +if [ -z "$BUILD_ID" ]; then + echo -e "${RED}Klarte ikke å starte build eller hente build-ID.${NC}" + exit 1 +fi + +echo -e "${GREEN}Build startet! ID: ${BOLD}$BUILD_ID${NC}" +echo -e "--- Streaming logs --- +" + +START_TIME=$(date +%s) +gcloud builds log --stream "$BUILD_ID" --project="$PROJECT_ID" --region="$REGION" + +FINAL_STATUS=$(gcloud builds describe "$BUILD_ID" --project="$PROJECT_ID" --region="$REGION" --format="value(status)") +END_TIME=$(date +%s) +ELAPSED=$((END_TIME - START_TIME)) + +echo -e " +--- Build Complete ---" +echo -e "${BOLD}Status: $FINAL_STATUS | Tid: $((ELAPSED / 60))m $((ELAPSED % 60))s${NC}" + +[ "$FINAL_STATUS" == "SUCCESS" ] \ No newline at end of file