# Maven
# Build your Java project and run tests with Apache Maven.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/java

trigger:
  branches:
    include:
    - kuber-migration
  paths:
    exclude:
      - README.md
      - .gitignore

pool:
  name: Hosted Ubuntu 1604
  demands: maven

variables:
  dockerDir: provider/indexer-gcp/docker
  imageName: os-indexer-app
  deploymentDir: provider/indexer-gcp/kubernetes/deployments
  deploymentFile: deployment-os-indexer-service.yml
  mavenSettings: ./indexer-core/maven/settings.xml
  coreModule: indexer-core
  gcpModule: indexer-gcp

  integrationTestCorePom: testing/indexer-test-core/pom.xml
  integrationTestGcpPom: testing/indexer-test-gcp/pom.xml
steps:

  - task: DownloadSecureFile@1
    name: gcrKey
    inputs:
      secureFile: cicd-push-image-to-cr-keyfile.json

  - task: DownloadSecureFile@1
    name: kuberConfig
    inputs:
      secureFile: kubeconfig

  - task: Maven@3
    inputs:
      mavenPomFile: 'pom.xml'
      options: '--settings $(mavenSettings) -DVSTS_FEED_TOKEN=$(VSTS_FEED_TOKEN) -P $(coreModule) install'
      publishJUnitResults: false
      javaHomeOption: 'JDKVersion'
      mavenVersionOption: 'Default'
      mavenAuthenticateFeed: false
      effectivePomSkip: false
      sonarQubeRunAnalysis: false

  - task: Maven@3
    inputs:
      mavenPomFile: 'pom.xml'
      options: '--settings $(mavenSettings) -DVSTS_FEED_TOKEN=$(VSTS_FEED_TOKEN) -P $(gcpModule) -DskipTests package'
      publishJUnitResults: false
      javaHomeOption: 'JDKVersion'
      mavenVersionOption: 'Default'
      mavenAuthenticateFeed: false
      effectivePomSkip: false

  - bash: |
      #!/bin/bash
      set -e
      pushd $(dockerDir)
      docker-compose build $(imageName)
      echo 'Image done.'
      cat $(gcrKey.secureFilePath) | docker login -u _json_key --password-stdin https://gcr.io
      echo 'Login done.'
      docker push gcr.io/opendes/$(imageName)
      echo 'Push done.'
      popd
      pushd $(deploymentDir)
      kubectl --kubeconfig $(kuberConfig.secureFilePath) delete -f $(deploymentFile)
      kubectl --kubeconfig $(kuberConfig.secureFilePath) apply -f $(deploymentFile)
      popd

      attempt_counter=0
      max_attempts=60
      until [[ $(curl --head --write-out %{http_code} $(INDEXER_READINESS_URL) --silent -o /dev/null --fail) -eq 200 ]]; do
      if [ ${attempt_counter} -eq ${max_attempts} ];then
          echo "Service is not available, integraton tests are skipped"
          exit 1
      fi

      printf '.'
      attempt_counter=$(($attempt_counter+1))
      sleep 2
      done

  - task: Maven@3
    inputs:
      mavenPomFile: '$(integrationTestCorePom)'
      options: '--settings $(mavenSettings) -DVSTS_FEED_TOKEN=$(VSTS_FEED_TOKEN) install'
      publishJUnitResults: false
      javaHomeOption: 'JDKVersion'
      mavenVersionOption: 'Default'
      mavenAuthenticateFeed: false
      effectivePomSkip: false
      sonarQubeRunAnalysis: false

  - task: Maven@3
    inputs:
      mavenPomFile: '$(integrationTestGcpPom)'
      options: '--settings $(mavenSettings) -Dsurefire.useFile=false -DVSTS_FEED_TOKEN=$(VSTS_FEED_TOKEN) -DDEFAULT_DATA_PARTITION_ID_TENANT1=$(DEFAULT_DATA_PARTITION_ID_TENANT1) -DDEFAULT_DATA_PARTITION_ID_TENANT2=$(DEFAULT_DATA_PARTITION_ID_TENANT2) -DELASTIC_HOST=$(ELASTIC_HOST) -DELASTIC_PASSWORD=$(ELASTIC_PASSWORD) -DELASTIC_USER_NAME=$(ELASTIC_USER_NAME) -DENTITLEMENTS_DOMAIN=$(ENTITLEMENTS_DOMAIN) -DINDEXER_HOST=$(INDEXER_HOST) -DLEGAL_TAG=$(LEGAL_TAG) -DOTHER_RELEVANT_DATA_COUNTRIES=$(OTHER_RELEVANT_DATA_COUNTRIES) -DSEARCH_HOST=$(SEARCH_HOST) -DSEARCH_INTEGRATION_TESTER=$(SEARCH_INTEGRATION_TESTER) -DSTORAGE_HOST=$(STORAGE_HOST) compile'
      publishJUnitResults: false
      javaHomeOption: 'JDKVersion'
      mavenVersionOption: 'Default'
      mavenAuthenticateFeed: false
      effectivePomSkip: false
      sonarQubeRunAnalysis: false
