parameters:
  poolSettings: {}
  expectUseVMs: true
  modules: []
  name: ''
  postgres: false
  dynamodb: false
  keycloak: false
  mysql: false
  neo4j: false
  timeoutInMinutes: 80


jobs:
  - job: ${{ parameters.name }}
    condition: and(eq(variables.LINUX_USE_VMS, ${{parameters.expectUseVMs}}),succeeded()) 
    displayName: ${{ join(', ', parameters.modules) }}
    timeoutInMinutes: ${{parameters.timeoutInMinutes}}
    pool: ${{parameters.poolSettings}}

    workspace:
      clean: all

    variables:
      imageName: 'quarkus:$(build.buildId)'
    steps:
    - script: docker rm -f $(docker ps -qa) || true
      displayName: 'Docker Cleanup'
    - task: DownloadPipelineArtifact@1
      inputs:
        artifact: $(Build.SourceVersion)-BuiltMavenRepo
        targetPath: $(Pipeline.Workspace)/.m2/repository/
    - bash: (cd $(Pipeline.Workspace)/.m2/repository/$(Build.SourceVersion)-BuiltMavenRepo/; mv * ..)
    - ${{ if eq(parameters.postgres, 'true') }}:
      - script: docker run --rm --publish 5432:5432 --name build-postgres -e POSTGRES_USER=hibernate_orm_test -e POSTGRES_PASSWORD=hibernate_orm_test -e POSTGRES_DB=hibernate_orm_test -d postgres:10.5
        displayName: 'start postgres'
    - ${{ if eq(parameters.dynamodb, 'true') }}:
      - script: docker run --rm --publish 8000:8000 --name build-dynamodb -d amazon/dynamodb-local:1.11.477
        displayName: 'start dynamodb'
    - ${{ if eq(parameters.keycloak, 'true') }}:
        - script: docker run --rm --publish 8180:8080 --name build-keycloak  -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin -e JAVA_OPTS="-server -Xms64m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djava.awt.headless=true -Dkeycloak.profile.feature.upload_scripts=enabled" -d quay.io/keycloak/keycloak:8.0.1
          displayName: 'start keycloak'
    - ${{ if eq(parameters.neo4j, 'true') }}:
        - script: docker run --rm --publish 7687:7687 --name build-neo4j -e NEO4J_AUTH=neo4j/secret -e NEO4J_dbms_memory_pagecache_size=10M -e NEO4J_dbms_memory_heap_initial__size=10M -d neo4j/neo4j-experimental:4.0.0-rc01
          displayName: 'start neo4j'
    - ${{ if eq(parameters.mysql, 'true') }}:
      - bash: |
          sudo service mysql stop || true
          docker run --rm --publish 3306:3306 --name build-mysql -e MYSQL_USER=hibernate_orm_test -e MYSQL_PASSWORD=hibernate_orm_test -e MYSQL_DATABASE=hibernate_orm_test -e MYSQL_RANDOM_ROOT_PASSWORD=true -e MYSQL_DATABASE=hibernate_orm_test -d mysql:5 --skip-ssl
        displayName: 'start mysql'
    - ${{ if eq(parameters.mariadb, 'true') }}:
      - bash: |
          sudo service mysql stop || true
          docker run --rm --publish 3308:3306 --name build-mariadb -e MYSQL_USER=hibernate_orm_test -e MYSQL_PASSWORD=hibernate_orm_test -e MYSQL_DATABASE=hibernate_orm_test -e MYSQL_RANDOM_ROOT_PASSWORD=true -d mariadb:10.4
        displayName: 'start mariadb'
    - ${{ if eq(parameters.mssql, 'true') }}:
        - script: docker run --rm --publish 1433:1433 --name build-mssql -e ACCEPT_EULA=Y -e SA_PASSWORD=ActuallyRequired11Complexity -d microsoft/mssql-server-linux:2017-CU12
          displayName: 'start microsoft sql server'

    - task: Maven@3
      displayName: 'Maven Build'
      inputs:
        goals: 'install'
        mavenOptions: $(MAVEN_OPTS)
        options: '-pl integration-tests/${{ join('',integration-tests/'', parameters.modules) }} -B --settings azure-mvn-settings.xml -Dquarkus.native.container-build=true -Dtest-postgresql -Dtest-elasticsearch -Dtest-keycloak -Dtest-dynamodb -Dtest-mysql -Dtest-vault -Dtest-neo4j -Dtest-mssql -Dtest-mariadb -Dmariadb.url="jdbc:mariadb://localhost:3308/hibernate_orm_test" -Dnative-image.xmx=6g -Dnative -Dno-format'
