# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

steps:
  - id: "setup"
    name: "gcr.io/google.com/cloudsdktool/cloud-sdk:slim"
    entrypoint: /bin/bash
    args:
      - "-c"
      - |
        source ./provisioning/automation/project-setup.sh \
          -f ${_PARENT_FOLDER} \
          -p ${_CI_PROJECT} \
          -b ${_BILLING_ACCOUNT_ID} \
          -r ${_REGION} \
          -s ${_SA_NAME}

  - id: "provision"
    name: "gcr.io/google.com/cloudsdktool/cloud-sdk:slim"
    entrypoint: gcloud
    args:
      [
        "builds",
        "submit",
        "--config=$_CLOUDBUILD_CONFIG",
        "--timeout=1500",
        "--project=$_CI_PROJECT"
      ]


  # Use Debian 11 Bullseye until Playwright supports Debian 12 Bookworm
  #   - https://playwright.dev/python/docs/intro#system-requirements
  - id: "test"
    name: python:3.13-bullseye
    dir: provisioning
    env: 
      - 'CI_PROJECT=$_CI_PROJECT'
    script: |
        #!/bin/bash

        python -m pip install -r test/requirements.txt
        playwright install --with-deps
        python -m pytest


  - id: "cleanup"
    name: "gcr.io/google.com/cloudsdktool/cloud-sdk:slim"
    env: ["KEEP_PROJECT=${_KEEP_PROJECT}"]
    script:
      #!/bin/bash

      if [ $KEEP_PROJECT  = 'true' ]; then echo "Configuration says to keep project around. Not deleting."; exit 0; fi

      echo "Deleting project..."
      gcloud projects delete ${_CI_PROJECT} --quiet;

options:
  dynamic_substitutions: true
  machineType: "E2_HIGHCPU_8"
  logging: CLOUD_LOGGING_ONLY

serviceAccount: projects/${PROJECT_ID}/serviceAccounts/ci-serviceaccount@${PROJECT_ID}.iam.gserviceaccount.com

substitutions:
  _KEEP_PROJECT: "false"
  _REGION: us-central1
  _SA_NAME: ci-serviceaccount
  _CI_PROJECT: "avocano-ephemeral-${BUILD_ID:0:8}"
  _CLOUDBUILD_CONFIG: provisioning/automation/deploy.cloudbuild.yaml
  _BILLING_ACCOUNT_ID: ""

timeout: "1500s"
