name: CI

on:
  push:
    branches:
      - '*'
  pull_request:
    branches:
      - '*'

# https://help.github.com/en/actions/automating-your-workflow-with-github-actions/software-installed-on-github-hosted-runners

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:
      # Number of jobs to generate in matrix.js
      MATRIX_JOBS: 4
    steps:
      - uses: actions/checkout@v3
      - id: set-matrix
        run: |
          node .github/workflows/matrix.js

  test:
    needs: matrix_prep
    name: '${{ matrix.name }}'
    runs-on: ${{ matrix.os }}
    env:
      TZ: ${{ matrix.tz }}
    strategy:
      matrix: ${{fromJson(needs.matrix_prep.outputs.matrix)}}
      fail-fast: false
      # max-parallel: 4
    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 50
    - name: Set up Java ${{ matrix.java_version }}, oracle
      if: ${{ matrix.oracle_java_website != '' }}
      uses: oracle-actions/setup-java@1611a647972adb8b04779be3529a044d650fd510 # v1
      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@v3
      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@v6
      with:
        script: |
          console.log('The following command might help reproducing CI results, use Java ${{ matrix.java_version }}')
          console.log('TZ="${{ matrix.tz }}" _JAVA_OPTIONS="${{ matrix.extraJvmArgs }}" ./gradlew build -x distTar -x distTarSource -x distTarSha512 -x distTarSourceSha512 ${{ matrix.extraGradleArgs }} -PtestExtraJvmArgs="${{ matrix.testExtraJvmArgs }}" -PtestDisableCaching="${{ matrix.testDisableCaching }}"')
    - uses: burrunan/gradle-cache-action@v1
      name: Test
      with:
        job-id: jdk${{ matrix.java_version }}
        multi-cache-enabled: false
        # An explicit skip for Sha512 tasks is required due to https://github.com/gradle/gradle/issues/16789
        arguments: --scan --no-parallel build -x distTar -x distTarSource -x distTarSha512 -x distTarSourceSha512 ${{ matrix.extraGradleArgs }}
        properties: |
          testExtraJvmArgs=${{ 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
      env:
        _JAVA_OPTIONS: ${{ matrix.extraJvmArgs }}
        GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GE_ACCESS_TOKEN }}

  errorprone:
    name: 'Error Prone (JDK 11)'
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: 'Set up JDK 11'
      uses: actions/setup-java@v3
      with:
        java-version: 11
        distribution: 'zulu'
    - uses: burrunan/gradle-cache-action@v1
      name: Test
      with:
        job-id: errprone
        multi-cache-enabled: false
        arguments: --scan --no-parallel --no-daemon -PenableErrorprone classes
      env:
        GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GE_ACCESS_TOKEN }}
