# Docker image
# Build a Docker image to deploy, run, or push to a container registry.
# Add steps that use Docker Compose, tag images, push to a registry, run an image, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
  batch: true
  branches:
    include:
      - master

pr:
  branches:
    include:
      - master
  paths:
    exclude:
      - docs/src/main/asciidoc/*
      - docs/src/main/asciidoc/images/*
      - README.md
      - CONTRIBUTING.md
      - LICENSE.txt
      - dco.txt
      - .github/ISSUE_TEMPLATE/*.md

variables:
  MAVEN_CACHE_FOLDER: $(Pipeline.Workspace)/.m2/repository/
  MAVEN_OPTS: '-Dmaven.repo.local=$(MAVEN_CACHE_FOLDER)'
  QUARKUS_LOCAL_REPO: $(MAVEN_CACHE_FOLDER)

stages:
  #This stage uses the azure caching feature to reduce the downloads that are performed from central, which greatly speeds up CI
  #This is it's own stage to prevent Quarkus artifacts produced by the build from being cached
  - stage: cache_maven_repo_stage
    displayName: 'Maven Cache'
    jobs:
      - job: Cache_Maven_Repo
        displayName: 'Linux Maven Repo'
        timeoutInMinutes: 30
        pool:
          vmImage: 'Ubuntu 16.04'

        variables:
          imageName: 'quarkus:$(build.buildId)'

        steps:

          - task: CacheBeta@0
            inputs:
              key: maven | bom/runtime/pom.xml   #if we attempt to use all poms then when they get copied to target everything breaks. This should be good enough, it does not need to be perfect
              path: $(MAVEN_CACHE_FOLDER)
              securityNamespace: cache
              cacheHitVar: CACHE_RESTORED
            displayName: Cache Maven local repo

          - task: Maven@3
            displayName: 'Maven Build'
            condition: ne(variables.CACHE_RESTORED, 'true')
            inputs:
              goals: 'package'
              mavenOptions: $(MAVEN_OPTS)
              options: '-B --settings azure-mvn-settings.xml -DskipTests=true -Dno-format'
      - job: Cache_Windows_Maven_Repo #windows has different line endings so the cache key is different
        displayName: 'Windows Maven Repo'
        timeoutInMinutes: 30
        pool:
          vmImage: 'vs2017-win2016'

        variables:
          imageName: 'quarkus-windows:$(build.buildId)'

        steps:

          - task: CacheBeta@0
            inputs:
              key: mavenWindows | bom/runtime/pom.xml   #if we attempt to use all poms then when they get copied to target everything breaks. This should be good enough, it does not need to be perfect
              path: $(MAVEN_CACHE_FOLDER)
              securityNamespace: cache
              cacheHitVar: CACHE_RESTORED
            displayName: Cache Maven local repo

          - task: Maven@3
            displayName: 'Maven Build'
            condition: ne(variables.CACHE_RESTORED, 'true')
            inputs:
              goals: 'package'
              mavenOptions: $(MAVEN_OPTS)
              options: '-B --settings azure-mvn-settings.xml -DskipTests=true -Dno-format'

  #This stage builds the Quarkus artifacts needed for native image testing
  - stage: build_artifact_for_native_stage
    displayName: 'Build for Native'
    dependsOn: cache_maven_repo_stage
    jobs:
      - job: Build_JDK8_Linux
        displayName: 'Build JDK8 Linux'
        timeoutInMinutes: 60
        pool:
          vmImage: 'Ubuntu 16.04'
        steps:
          - task: CacheBeta@0 #we know the very first job will have restored or created this, so this will never write built artifacts to the cache
            inputs:
              key: maven | bom/runtime/pom.xml
              path: $(Pipeline.Workspace)/.m2/repository/
              securityNamespace: cache
            displayName: Restore Maven Cache

          - task: Maven@3
            displayName: 'Maven Build'
            inputs:
              goals: 'install'
              mavenOptions: $(MAVEN_OPTS)
              options: '-DskipTests -Dno-format'
          - publish: $(MAVEN_CACHE_FOLDER)
            artifact: BuiltMavenRepo

  - stage: run_jvm_tests_stage
    displayName: 'Run JVM Tests'
    dependsOn: cache_maven_repo_stage
    jobs:

      - job: Build_JDK8_Linux
        displayName: 'Build JDK8 Linux'
        timeoutInMinutes: 60
        pool:
          vmImage: 'Ubuntu 16.04'
        steps:
          - template: ci-templates/jvm-build-steps.yaml

      - job: Windows_Build
        displayName: 'Windows JVM Build'
        timeoutInMinutes: 60
        pool:
          vmImage: 'vs2017-win2016'
          MAVEN_CACHE_FOLDER: $(Pipeline.Workspace)/.m2/repository
          MAVEN_OPTS: '-Dmaven.repo.local=$(MAVEN_CACHE_FOLDER)'
        steps:
          - task: CacheBeta@0 #we know the very first job will have restored or created this, so this will never write built artifacts to the cache
            inputs:
              key: mavenWindows | bom/runtime/pom.xml
              path: $(MAVEN_CACHE_FOLDER)
              securityNamespace: cache
            displayName: Restore Maven Cache

          - task: Maven@3
            displayName: 'Maven Build'
            inputs:
              goals: 'install'
              mavenOptions: $(MAVEN_OPTS)
              options: '-B --settings azure-mvn-settings.xml -Dno-native -Dno-format'

      - job: Build_JDK11_Linux
        timeoutInMinutes: 60
        displayName: 'Linux JDK11 Build'
        pool:
          vmImage: 'Ubuntu 16.04'
        steps:
          - template: ci-templates/jvm-build-steps.yaml
            parameters:
              jdk: '1.11'

      - job: Build_JDK12_Linux
        timeoutInMinutes: 60
        displayName: 'Linux JDK12 Build'
        pool:
          vmImage: 'Ubuntu 16.04'
        steps:
          - template: ci-templates/jvm-build-steps.yaml
            parameters:
              jdk: '1.12'

      - job: Run_TCKs
        timeoutInMinutes: 45
        pool:
          vmImage: 'Ubuntu 16.04'
        steps:
          - task: Maven@3
            displayName: 'Maven Install'
            inputs:
              goals: 'install'
              mavenOptions: $(MAVEN_OPTS)
              options: '-B --settings azure-mvn-settings.xml -Dno-native -Dno-format -DskipTests -Dtcks'
          - task: Maven@3
            displayName: 'Maven Verify'
            inputs:
              goals: 'verify'
              mavenOptions: $(MAVEN_OPTS)
              mavenPomFile: 'tcks/pom.xml'


  - stage: run_native_tests_stage
    displayName: 'Native Tests'
    dependsOn: build_artifact_for_native_stage
    jobs:
      - template: ci-templates/native-build-steps.yaml
        parameters:
          modules: main
          name: main
          postgres: true

      - template: ci-templates/native-build-steps.yaml
        parameters:
          timeoutInMinutes: 25
          modules:
            - jpa-h2
            - jpa-mariadb
            - jpa-mssql
          name: jpa_db

      - template: ci-templates/native-build-steps.yaml
        parameters:
          timeoutInMinutes: 25
          modules:
            - jpa
            - jpa-postgresql
            - jpa-mysql
          name: jpa
          postgres: true
          mysql: true

      - template: ci-templates/native-build-steps.yaml
        parameters:
          timeoutInMinutes: 15
          modules:
            - amazon-dynamodb
            - amazon-lambda
          name: amazon
          dynamodb: true

      - template: ci-templates/native-build-steps.yaml
        parameters:
          timeoutInMinutes: 20
          modules:
            - artemis-core
            - artemis-jms
            - kafka
          name: messaging

      - template: ci-templates/native-build-steps.yaml
        parameters:
          timeoutInMinutes: 20
          modules:
            - elytron-security-oauth2
            - elytron-security
            - oidc
          name: security
          keycloak: true

      - template: ci-templates/native-build-steps.yaml
        parameters:
          timeoutInMinutes: 30
          modules:
            - flyway
            - hibernate-orm-panache
            - reactive-pg-client
          name: database
          postgres: true

      - template: ci-templates/native-build-steps.yaml
        parameters:
          modules:
            - infinispan-cache-jpa
            - infinispan-client
            - mongodb-client
            - mongodb-panache
            - neo4j
            - narayana-stm
          name: data

      - template: ci-templates/native-build-steps.yaml
        parameters:
          timeoutInMinutes: 25
          modules:
            - resteasy-jackson
            - vertx
            - vertx-http
            - virtual-http
          name: http

      - template: ci-templates/native-build-steps.yaml
        parameters:
          timeoutInMinutes: 25
          modules:
            - jackson
            - jgit
            - kogito
            - kubernetes-client
          name: misc1

      - template: ci-templates/native-build-steps.yaml
        parameters:
          timeoutInMinutes: 25
          modules:
            - tika
            - hibernate-validator
            - test-extension
          name: misc2

      - template: ci-templates/native-build-steps.yaml
        parameters:
          timeoutInMinutes: 25
          modules:
            - spring-di
            - spring-web
            - spring-data-jpa
          name: spring


      - template: ci-templates/native-build-steps.yaml
        parameters:
          timeoutInMinutes: 15
          modules:
            - hibernate-search-elasticsearch
          name: hibernate_search_elasticsearch
