# Copyright 2025 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
#
#     https://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.

# An example of Cloud Build configuration file with Terraform for CI/CD pipeline.

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/aa-integration-backend/ui-connector-$BRANCH_NAME:$SHORT_SHA', './aa-integration-backend/ui-connector']

- name: 'gcr.io/cloud-builders/docker'
  args: ['push',  'gcr.io/$PROJECT_ID/aa-integration-backend/ui-connector-$BRANCH_NAME:$SHORT_SHA']

- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/aa-integration-backend/cloud-pubsub-interceptor-$BRANCH_NAME:$SHORT_SHA', './aa-integration-backend/cloud-pubsub-interceptor']

- name: 'gcr.io/cloud-builders/docker'
  args: ['push',  'gcr.io/$PROJECT_ID/aa-integration-backend/cloud-pubsub-interceptor-$BRANCH_NAME:$SHORT_SHA']

- id: 'branch name'
  name: 'alpine'
  entrypoint: 'sh'
  args:
  - '-c'
  - |
      echo "***********************"
      echo "$BRANCH_NAME"
      echo "***********************"

- id: 'tf init'
  name: 'hashicorp/terraform:1.0.0'
  entrypoint: 'sh'
  args:
  - '-c'
  - |
      cd ./aa-integration-backend/terraform
      echo ""
      echo "*************** TERRAFORM INIT ******************"
      tf_state_bucket="$PROJECT_ID-tfstate"
      terraform init \
        -backend-config="bucket=$tf_state_bucket" \
        -backend-config="prefix=$BRANCH_NAME" || exit 1
      cd ../../

- id: 'tf plan'
  name: 'hashicorp/terraform:1.0.0'
  entrypoint: 'sh'
  args:
  - '-c'
  - |
      cd ./aa-integration-backend/terraform
      echo ""
      echo "*************** TERRAFOM PLAN ******************"
      terraform plan -var="gcp_project_id=$PROJECT_ID" \
        -var="ui_connector_docker_image=gcr.io/$PROJECT_ID/aa-integration-backend/ui-connector-$BRANCH_NAME:$SHORT_SHA" \
        -var="cloud_pubsub_interceptor_docker_image=gcr.io/$PROJECT_ID/aa-integration-backend/cloud-pubsub-interceptor-$BRANCH_NAME:$SHORT_SHA" \
        -var="connector_service_name=ui-connector-$BRANCH_NAME" \
        -var="interceptor_service_name=cloud-pubsub-interceptor-$BRANCH_NAME" \
        || exit 1
      cd ../../

- id: 'tf apply'
  name: 'hashicorp/terraform:1.0.0'
  entrypoint: 'sh'
  args:
  - '-c'
  - |
      cd ./aa-integration-backend/terraform
      echo ""
      echo "*************** TERRAFOM APPLYING ******************"
      auth_option=""
      if [ $BRANCH_NAME != 'main' ]; then
        auth_option="Skip"
      fi
      terraform apply -auto-approve \
        -var="gcp_project_id=$PROJECT_ID" \
        -var="ui_connector_docker_image=gcr.io/$PROJECT_ID/aa-integration-backend/ui-connector-$BRANCH_NAME:$SHORT_SHA" \
        -var="cloud_pubsub_interceptor_docker_image=gcr.io/$PROJECT_ID/aa-integration-backend/cloud-pubsub-interceptor-$BRANCH_NAME:$SHORT_SHA" \
        -var="connector_service_name=ui-connector-$BRANCH_NAME" \
        -var="interceptor_service_name=cloud-pubsub-interceptor-$BRANCH_NAME" \
        -var="redis_instance_id=aa-integration-redis-$BRANCH_NAME" \
        -var="agent_assist_notifications_subscription_id=aa-new-suggestion-sub-$BRANCH_NAME" \
        -var="new_message_notifications_subscription_id=aa-new-message-sub-$BRANCH_NAME" \
        -var="conversation_lifecycle_notifications_subscription_id=aa-conversation-event-sub-$BRANCH_NAME" \
        -var="new_recognition_result_notification_subscription_id=aa-intermediate-transcript-event-sub-$BRANCH_NAME" \
        -var="auth_option=$auth_option"
      cd ../../