trigger: [ 'master' ]

pool:
  name: Hosted Ubuntu 1604
  demands: maven

variables:
  osProjectName: notification
  dockerDir: provider/$(osProjectName)-gcp/docker
  imageName: os-$(osProjectName)-app
  sourceImageName: gcr.io/opendes/$(imageName)
  tag: $(Build.BuildNumber)

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


  - task: Maven@3
    displayName: 'Build and test'
    inputs:
      jdkVersionOption: 1.8
      jdkArchitectureOption: 'x64'
      mavenPomFile: 'pom.xml'
      goals: "clean install"
      options: '--settings configuration/settings.xml -DVSTS_FEED_TOKEN=$(VSTS_FEED_TOKEN)'
      testResultsFiles: '**/TEST-*.xml'

  - 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 tag $(sourceImageName) $(sourceImageName):$(tag)
      docker push $(sourceImageName):$(tag)
      popd
    displayName: 'Docker image build and push'


  - bash: |
      sed -i -e "s|{{definitionName}}|$(build.DefinitionName)|g" deploy.sh
      sed -i -e "s|{{sourceBranchName}}|$(build.SourceBranchName)|g" deploy.sh
      sed -i -e "s|{{buildNumber}}|$(build.BuildNumber)|g" deploy.sh
    workingDirectory: provider/notification-gcp/scripts
    displayName: 'Gomplate GCP deploy script'

  - task: ArchiveFiles@2
    displayName: 'Archive integration-tests'
    inputs:
      rootFolderOrFile: 'testing'
      archiveFile: 'provider/notification-gcp/target/integration-tests.zip'
      includeRootFolder: false

  - task: CopyFiles@2
    displayName: 'Copy deployable jar and integration tests zip'
    inputs:
      sourceFolder: 'provider/notification-gcp/target'
      contents: |
        *spring-boot.jar
        integration-tests.zip
      targetFolder: '$(build.artifactstagingdirectory)/deploy'

  - task: CopyFiles@2
    displayName: 'Copy maven settings.xml'
    inputs:
      sourceFolder: 'configuration'
      contents: 'settings.xml'
      targetFolder: '$(build.artifactstagingdirectory)/deploy'

  - task: CopyFiles@2
    displayName: 'Copy deploy scripts'
    inputs:
      sourceFolder: 'provider/notification-gcp/scripts'
      contents: '**'
      targetFolder: '$(build.artifactstagingdirectory)/deploy'

  - task: CopyFiles@2
    displayName: 'Copy deployment yaml'
    inputs:
      sourceFolder: 'provider/notification-gcp/src/main/appengine'
      contents: '**'
      targetFolder: '$(build.artifactstagingdirectory)/deploy'

  - task: PublishBuildArtifacts@1
    displayName: 'Publish Artifact: drop'
    inputs:
      PathtoPublish: '$(build.artifactstagingdirectory)'
