# 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:
  buildMavenModules: legal-core,provider/legal-gcp
  dockerDir: provider/legal-gcp/docker
  imageName: os-legal-app
  deploymentDir: provider/legal-gcp/kubernetes/deployments
  deploymentFile: deployment-os-legal-service.yml
  mavenSettings: ./maven/settings.xml

  integrationTestCorePom: testing/legal-test-core/pom.xml
  integrationTestGcpPom: testing/legal-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: DownloadSecureFile@1
    name: gac
    inputs:
      secureFile: opendes-datafier-126f1340d475.json

  - task: Maven@3
    inputs:
      mavenPomFile: 'pom.xml'
      options: '--settings $(mavenSettings) -DVSTS_FEED_TOKEN=$(VSTS_FEED_TOKEN) -P legal-core 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 legal-gcp -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
      max_attempts=120
      until [[ $(curl --head --write-out %{http_code} $(LEGAL_READINESS_URL) --silent -o /dev/null --fail) -eq 401 ]]; 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) -DargLine="-DGOOGLE_APPLICATION_CREDENTIALS=$(gac.secureFilePath)" -DSKIP_HTTP_TESTS=SKIP_HTTP_TESTS -Dsurefire.useFile=false -DVSTS_FEED_TOKEN=$(VSTS_FEED_TOKEN) -DINTEGRATION_TESTER=$(INTEGRATION_TESTER) -DNO_DATA_ACCESS_TESTER=$(NO_DATA_ACCESS_TESTER) -DHOST_URL=$(HOST_URL) -DINTEGRATION_TEST_AUDIENCE=$(INTEGRATION_TEST_AUDIENCE) -DMY_TENANT=$(MY_TENANT) -DMY_TENANT_PROJECT=$(MY_TENANT_PROJECT) compile'
      publishJUnitResults: false
      javaHomeOption: 'JDKVersion'
      mavenVersionOption: 'Default'
      mavenAuthenticateFeed: false
      effectivePomSkip: false
      sonarQubeRunAnalysis: false