# 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:
  - name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
    id: "Remove Xwiki app image"
    script: |
      if [ -z $(gcloud compute images describe hsa-xwiki-vm-img-latest --verbosity=none --format=text) ]; then
        echo "The image does not exist. Proceeding with next build step."
      else
        echo "Deleting the image."
        gcloud compute images delete hsa-xwiki-vm-img-latest -q --verbosity=none
      fi

  - name: 'gcr.io/migrate-legacy-java-app-gce/packer'
    dir: 'backend/config'
    args:
      - init
      - .

  - name: 'gcr.io/migrate-legacy-java-app-gce/packer'
    dir: 'backend/config'
    args:
      - build
      - -var
      - project_id=$PROJECT_ID
      - -var
      - region=$_LOCATION
      - -var
      - zone=${_ZONE}
      - -var
      - xwiki_img_name=hsa-xwiki-vm-img-latest
      - -var
      - img_desc=XWiki image from Packer which is triggered by Cloud Build
      - -var
      - file_sources_tcp=./tcp_gcp.xml
      - -var
      - file_sources_hibernate=../../third_party/xwiki/hibernate_gcp.cfg.xml
      - -var
      - file_sources_startup_sh=../tools/xwiki_startup.sh
      - -var
      - file_deploy_flavor_sh=../tools/xwiki_deploy_flavor.sh
      - -var
      - deploy_sh=../tools/xwiki_manual_deploy_gcp.sh
      - -var
      - xwiki_migrate_file_bucket=$_XWIKI_MIGRATE_FILE_BUCKET
      - .

  - name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
    id: "Public Xwiki app image"
    env:
      - _LOCATION=$_LOCATION
      - BUILD_ID=$BUILD_ID
    script: |
      gcloud compute images add-iam-policy-binding hsa-xwiki-vm-img-latest --member='allAuthenticatedUsers' --role='roles/compute.imageUser'

substitutions:
  _XWIKI_MIGRATE_FILE_BUCKET: ""

options:
  logging: CLOUD_LOGGING_ONLY
