name: build

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "main" branch
  push:
    branches: [ "main", "branch-*" ]
  pull_request:
    branches: [ "main", "branch-*" ]

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  changes:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: dorny/paths-filter@v2
        id: filter
        with:
          filters: |
            source_changes:
              - '.github/**'
              - 'catalogs/**'
              - 'clients/**'
              - 'conf/**'
              - 'dev/**'
              - 'docs/open-api/**'
              - 'gradle/**'
              - 'gradle.properties'
              - 'meta/**'
              - 'scripts/**'
              - 'web/**'
              - '**/*.java'
              - '**/*.kts'
              - '**/*.py'
              - '**/*.rs'
              - '**/resources/**'
              - '**/src/**'
              - '!**/*.md'
              - '!**/*.png'
              - '!**/*.svg'
            spark_connector_changes:
              - spark-connector/**
    outputs:
      source_changes: ${{ steps.filter.outputs.source_changes }}
      spark_connector_changes: ${{ steps.filter.outputs.spark_connector_changes }}

  compile-check:
    runs-on: ubuntu-latest
    needs: changes
    if: needs.changes.outputs.source_changes != 'true'
    steps:
      - uses: actions/checkout@v3

      - uses: actions/setup-java@v4
        with:
          java-version: 8
          distribution: 'temurin'
          cache: 'gradle'

      - name: Build with Gradle
        run: ./gradlew build -x test -PjdkVersion=8

  # To check the spark-connector is compatible with scala2.13
  spark-connector-build:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    needs: changes
    if: needs.changes.outputs.spark_connector_changes == 'true'
    steps:
      - uses: actions/checkout@v3

      - uses: actions/setup-java@v4
        with:
          java-version: 8
          distribution: 'temurin'
          cache: 'gradle'

      - name: Free up disk space
        run: |
          dev/ci/util_free_space.sh

      - name: Build with Scala2.13
        run: |
          ./gradlew :spark-connector:spark-3.4:build -PscalaVersion=2.13 -PskipITs -PskipDockerTests=false
          ./gradlew :spark-connector:spark-3.5:build -PscalaVersion=2.13 -PskipITs -PskipDockerTests=false

      - name: Upload unit tests report
        uses: actions/upload-artifact@v3
        if: failure()
        with:
          name: unit test report
          path: |
            build/reports
            spark-connector/**/*.log

  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    strategy:
      matrix:
        java-version: [ 8, 11, 17 ]
    timeout-minutes: 30
    needs: changes
    if: needs.changes.outputs.source_changes == 'true'
    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3

      - uses: actions/setup-java@v4
        with:
          java-version: ${{ matrix.java-version }}
          distribution: 'temurin'
          cache: 'gradle'

      - name: Test publish to local
        run: ./gradlew publishToMavenLocal -x test -PjdkVersion=${{ matrix.java-version }}

      - name: Free up disk space
        run: |
          dev/ci/util_free_space.sh

      - name: Build with Gradle
        run: ./gradlew build -PskipITs -PjdkVersion=${{ matrix.java-version }} -PskipDockerTests=false -x :clients:client-python:build

      - name: Upload unit tests report
        uses: actions/upload-artifact@v3
        if: failure()
        with:
          name: unit test report
          path: |
            build/reports
            catalogs/**/*.log
            catalogs/**/*.tar