# =============================================================================
# Cloud Build — build va-worker image and push to Artifact Registry
#
# Trigger: manual or on push to main
# Usage:
#   Manual:  gcloud builds submit --config cloudbuild.yaml .
#   With SHA: gcloud builds submit --config cloudbuild.yaml --substitutions _TAG=$(git rev-parse --short HEAD) .
# =============================================================================

substitutions:
  _REGION: europe-west1
  _REPO: video-accessibility
  _IMAGE: va-worker
  _TAG: latest   # overridden with $COMMIT_SHA by Cloud Build trigger; use 'latest' for manual runs

steps:
  # ── Build Cloud Run worker image ──────────────────────────────────────────
  - name: gcr.io/cloud-builders/docker
    id: build-va-worker
    args:
      - build
      - -f
      - backend/Dockerfile.cloudrun
      - -t
      - ${_REGION}-docker.pkg.dev/$PROJECT_ID/${_REPO}/${_IMAGE}:${_TAG}
      - backend/

  # ── Push image ────────────────────────────────────────────────────────────
  - name: gcr.io/cloud-builders/docker
    id: push-va-worker
    args:
      - push
      - ${_REGION}-docker.pkg.dev/$PROJECT_ID/${_REPO}/${_IMAGE}:${_TAG}

  # ── Update Cloud Run Job to use new image ─────────────────────────────────
  - name: gcr.io/google.com/cloudsdktool/cloud-sdk
    id: update-cloud-run-job
    entrypoint: gcloud
    args:
      - run
      - jobs
      - update
      - va-worker
      - --image
      - ${_REGION}-docker.pkg.dev/$PROJECT_ID/${_REPO}/${_IMAGE}:${_TAG}
      - --region
      - ${_REGION}

images:
  - ${_REGION}-docker.pkg.dev/$PROJECT_ID/${_REPO}/${_IMAGE}:${_TAG}

options:
  logging: CLOUD_LOGGING_ONLY
  machineType: E2_HIGHCPU_8   # faster builds
