name: CI

on:
  push:
    branches: [ main, master ]
  pull_request:
    branches: [ main, master ]

permissions:
  contents: read

jobs:
  test:
    runs-on: ubuntu-latest
    
    strategy:
      matrix:
        java-version: [8, 11]
    
    steps:
    - uses: actions/checkout@v6
    
    - name: Set up JDK ${{ matrix.java-version }}
      uses: actions/setup-java@v5
      with:
        java-version: ${{ matrix.java-version }}
        distribution: 'temurin'
    
    - name: Cache dependencies
      uses: actions/cache@v5
      with:
        path: |
          ~/.ant/lib
          ~/.ivy2/
          lib/
          ivy/
        key: ${{ runner.os }}-ant-${{ hashFiles('build.xml', 'ivy.xml') }}
        restore-keys: |
          ${{ runner.os }}-ant-
    
    - name: Run tests
      run: ant -Dtest.haltonfailure=true test
    
    - name: Build dist
      run: ant dist

    - name: Upload test results
      uses: actions/upload-artifact@v7
      if: always()
      with:
        name: test-results-java-${{ matrix.java-version }}
        path: testresults/

  test-modern-jdk:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        java-version: [17, 21, 25]

    steps:
    - uses: actions/checkout@v6

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

    - name: Set up compile JDK 8
      uses: actions/setup-java@v5
      with:
        java-version: 8
        distribution: 'temurin'

    - name: Cache dependencies
      uses: actions/cache@v5
      with:
        path: |
          ~/.ant/lib
          ~/.ivy2/
          lib/
          ivy/
        key: ${{ runner.os }}-ant-${{ hashFiles('build.xml', 'ivy.xml') }}
        restore-keys: |
          ${{ runner.os }}-ant-

    - name: Run tests on JDK ${{ matrix.java-version }}
      run: ant -Dtest.haltonfailure=true -Djvm=${{ steps.setup-test-jdk.outputs.path }}/bin/java test

    - name: Upload test results
      uses: actions/upload-artifact@v7
      if: always()
      with:
        name: test-results-java-${{ matrix.java-version }}
        path: testresults/

  reproducible-build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v6

    - name: Set up JDK 8
      uses: actions/setup-java@v5
      with:
        java-version: 8
        distribution: 'temurin'

    - name: Cache dependencies
      uses: actions/cache@v5
      with:
        path: |
          ~/.ant/lib
          ~/.ivy2/
          lib/
          ivy/
        key: ${{ runner.os }}-ant-${{ hashFiles('build.xml', 'ivy.xml') }}
        restore-keys: |
          ${{ runner.os }}-ant-

    - name: Verify reproducible build
      run: |
        chmod +x verify-reproducible.sh
        ./verify-reproducible.sh

