name: gradle-ci

on: [push, pull_request, workflow_dispatch]

jobs:
  build-gradle-project:
    runs-on: ubuntu-latest

    services:
      oracle:
        image: gvenzl/oracle-xe:18-slim
        env:
          ORACLE_RANDOM_PASSWORD: true
          APP_USER: unittests
          APP_USER_PASSWORD: password
        ports:
          - 1521:1521
        options: >-
          --health-cmd healthcheck.sh
          --health-interval 10s
          --health-timeout 5s
          --health-retries 10

      mysql:
        image: mysql:8
        env:
          MYSQL_DATABASE: unittests
          MYSQL_USER: unittests
          MYSQL_PASSWORD: password
          MYSQL_ROOT_PASSWORD: root
        ports:
          - 3306:3306
        options: >-
          --health-cmd="mysqladmin ping"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=3

      postgres:
        image: postgres:15
        env:
          POSTGRES_DB: unittests
          POSTGRES_PASSWORD: password
          POSTGRES_PORT: 5432
          POSTGRES_USER: unittests
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432

    env:
      GRADLE_OPTS: "-Dorg.gradle.jvmargs=-XX:MaxMetaspaceSize=512m"
      MYSQL_DATABASE: unittests
      MYSQL_USER: root
      MYSQL_PASSWORD: root

    strategy:
      matrix:
        java-version: [ 17, 19 ]

    steps:
      - name: Checkout source repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Set up JDK ${{ matrix.java-version }}
        uses: actions/setup-java@v3
        with:
          distribution: 'temurin'
          java-version: ${{ matrix.java-version }}

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Test with Gradle
        uses: gradle/gradle-build-action@v2
        with:
          arguments: -Dtest.postgres=true -Dtest.mysql=true -Dtest.oracle=true build check --stacktrace -PtestsBadgeApiKey=${{ secrets.TESTS_BADGE_API_KEY }}