#
# Copyright 2020 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/cloud-builders/docker'
  id: 'BUILD'
  args: ['build',
         '-t',
         'gcr.io/$PROJECT_ID/apache-atlas2datacatalog:$COMMIT_SHA',
         '/workspace/google-datacatalog-apache-atlas-connector/.']
- name: 'alpine'
  id: 'SETUP_TAG'
  args: ['sh',
         '-c',
         "echo `echo $BRANCH_NAME |
          sed 's,/,-,g' |
          awk '{print tolower($0)}'`_$(date -u +%Y%m%dT%H%M)_$SHORT_SHA > _TAG; echo $(cat _TAG)"]         
- name: 'gcr.io/cloudshell-images/cloudshell:latest'
  id: 'GET_PACKAGE'
  entrypoint: '/bin/bash'
  args: ['-c',
         'gsutil cp gs://datacatalog-connector-tools/apache-atlas-0.0.1.tar.gz .']
- name: 'gcr.io/cloudshell-images/cloudshell:latest'
  id: 'EXTRACT'
  entrypoint: '/bin/bash'
  args: ['-c',
         'tar -zxvf /workspace/apache-atlas-0.0.1.tar.gz']
- name: 'gcr.io/cloud-builders/gsutil'
  id: 'PREPARE_SERVICE_ACCOUNT'
  args: ['cp',
         'gs://connectors_build_env/apache-atlas2dc-credentials.json',
         '/workspace/apache-atlas/infrastructure/.']
- name: 'gcr.io/cloudshell-images/cloudshell:latest'
  id: 'PREPARE_ENV'
  entrypoint: '/bin/bash'
  args: ['-c',
         'cd /workspace/apache-atlas && ./cleanup-datacatalog.sh']                             
- name: 'gcr.io/cloudshell-images/cloudshell:latest'
  id: 'RUN'
  entrypoint: '/bin/bash'
  env:
  - 'PROJECT_ID=${PROJECT_ID}'
  - 'CONNECTOR_TAG_NAME=${COMMIT_SHA}'
  args: ['-c',
         'cd /workspace/apache-atlas && ./run-demo.sh']
  # Tag image with the custom tag
- name: 'docker.io/library/python:3.7'
  id: 'ASSERT_RESULTS'
  entrypoint: 'bash'
  env:
  - 'GOOGLE_APPLICATION_CREDENTIALS=/workspace/apache-atlas/infrastructure/apache-atlas2dc-credentials.json'
  - 'APACHE_ATLAS2DC_DATACATALOG_PROJECT_ID=${PROJECT_ID}'
  args:
  - -c
  - 'pip install google-cloud-datacatalog &&
     /workspace/google-datacatalog-apache-atlas-connector/system_tests/assert.sh'  
- name: 'gcr.io/cloud-builders/docker'
  id: 'TAG_IMAGE'
  entrypoint: '/bin/bash'
  args: ['-c',
         "docker tag gcr.io/$PROJECT_ID/apache-atlas2datacatalog:$COMMIT_SHA gcr.io/$PROJECT_ID/apache-atlas2datacatalog:$(cat _TAG)"]         
- name: 'gcr.io/cloudshell-images/cloudshell:latest'
  id: 'DELETE_ENV'
  entrypoint: '/bin/bash'
  args: ['-c',
         'cd /workspace/apache-atlas && ./delete-demo.sh']   
images: ['gcr.io/$PROJECT_ID/apache-atlas2datacatalog']
timeout: 30m
