availableSecrets:
  secretManager:
    - versionName: projects/master-ai-484822/secrets/gitea-token/versions/latest
      env: GITEA_TOKEN

steps:
  # Step 1: Clone the repo from Gitea (master branch has our startup script + vibn.css)
  - name: 'alpine/git'
    entrypoint: sh
    args:
      - -c
      - |
        git clone --depth=1 -b master \
          https://oauth2:$$GITEA_TOKEN@git.vibnai.com/mark/theia-code-os.git \
          /workspace/src && \
        echo "Cloned commit: $(git -C /workspace/src rev-parse --short HEAD)"
    secretEnv: ['GITEA_TOKEN']

  # Step 2: Build image FROM upstream Theia Blueprint — no compilation needed
  # Just adds startup-custom.sh and vibn.css on top of the pre-built Blueprint image
  - name: 'gcr.io/cloud-builders/docker'
    args:
      - build
      - -f
      - /workspace/src/Dockerfile.custom
      - -t
      - northamerica-northeast1-docker.pkg.dev/master-ai-484822/vibn-ide/theia:latest
      - /workspace/src

  # Step 3: Push to Artifact Registry
  - name: 'gcr.io/cloud-builders/docker'
    args:
      - push
      - northamerica-northeast1-docker.pkg.dev/master-ai-484822/vibn-ide/theia:latest

  # Step 4: Roll out new image to all active Cloud Run workspace services
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:slim'
    entrypoint: bash
    args:
      - -c
      - |
        IMAGE="northamerica-northeast1-docker.pkg.dev/master-ai-484822/vibn-ide/theia:latest"

        SERVICES=$$(gcloud run services list \
          --project=master-ai-484822 \
          --region=northamerica-northeast1 \
          --format="value(name)" \
          --filter="metadata.name:theia-")

        for svc in $$SERVICES; do
          echo "Rolling out new image to $$svc..."
          gcloud run services update $$svc \
            --project=master-ai-484822 \
            --region=northamerica-northeast1 \
            --image=$$IMAGE \
            --quiet
        done
        echo "Rollout complete."

images:
  - northamerica-northeast1-docker.pkg.dev/master-ai-484822/vibn-ide/theia:latest

options:
  machineType: E2_HIGHCPU_32
  logging: CLOUD_LOGGING_ONLY

# Full source compile + webpack — needs ~20 minutes on 32-vCPU machine
timeout: 2400s
