# This workflow will build a Java project with Gradle
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle

name: Test

on:
  push:
    branches:
      - '*'
  pull_request:
    branches: [ master ]

concurrency:
  # On master/release, we don't want any jobs cancelled so the sha is used to name the group
  # On PR branches, we cancel the job if new commits are pushed
  # More info: https://stackoverflow.com/a/68422069/253468
  group: ${{ (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/release' ) && format('ci-main-{0}', github.sha) || format('ci-main-{0}', github.ref) }}
  cancel-in-progress: true

jobs:
  matrix_prep:
    name: Matrix Preparation
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    env:
      # Ask matrix.js to produce 7 jobs
      MATRIX_JOBS: 7
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 1
      - id: set-matrix
        run: |
          node .github/workflows/matrix.js

  build:
    needs: matrix_prep
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix: ${{fromJson(needs.matrix_prep.outputs.matrix)}}
    name: '${{ matrix.name }}'
    env:
      TZ: ${{ matrix.tz }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 10
      - name: Set up Java ${{ matrix.java_version }}, oracle
        if: ${{ matrix.oracle_java_website != '' }}
        uses: oracle-actions/setup-java@83e2004a40aaa491fbc6b4697353b9a75b095efb # v1.3.4
        with:
          website: ${{ matrix.oracle_java_website }}
          release: ${{ matrix.java_version }}
      - name: Set up Java 17 and ${{ matrix.non_ea_java_version }}, ${{ matrix.java_distribution }}
        uses: actions/setup-java@v4
        with:
          # The latest one will be the default, so we use Java 17 for launching Gradle
          java-version: |
            ${{ matrix.non_ea_java_version }}
            17
          distribution: ${{ matrix.java_distribution }}
          architecture: x64
      - name: Steps to reproduce
        uses: actions/github-script@v7
        with:
          script: |
            console.log('The following command might help reproducing CI results, use Java ${{ matrix.java_version }}')
            console.log('TZ="${{ matrix.tz }}" ./gradlew build ${{ matrix.extraGradleArgs }} -Ptestng.test.extra.jvmargs="${{ matrix.testExtraJvmArgs }}" -PtestDisableCaching="${{ matrix.testDisableCaching }}"')
      - name: 'Generate unique build id'
        id: build_id
        run: echo "unique_id=$(date +%s)" >> $GITHUB_OUTPUT
      - name: Test
        id: run_test_cases
        uses: burrunan/gradle-cache-action@v1
        with:
          job-id: jdk${{ matrix.jdk.version }}
          arguments: |
            --no-parallel --no-daemon --scan
            build
            ${{ matrix.extraGradleArgs }}
          properties: |
            testng.test.extra.jvmargs=${{ matrix.testExtraJvmArgs }}
            testDisableCaching=${{ matrix.testDisableCaching }}
            jdkBuildVersion=17
            jdkTestVersion=${{ matrix.java_version }}
            jdkTestVendor=${{ matrix.java_vendor }}
            # We provision JDKs with GitHub Actions for caching purposes, so Gradle should rather fail in case JDK is not found
            org.gradle.java.installations.auto-download=false
      - name: Upload build reports
        if: ${{ failure() && steps.run_test_cases.outcome == 'failure' }}
        uses: actions/upload-artifact@v4
        with:
          name: build-reports-${{ matrix.jdk.group }}-${{ matrix.jdk.version }}-${{ steps.build_id.outputs.unique_id }}
          path: testng-core/build/reports/tests/test/**
