name: CI build

on:
  workflow_dispatch:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Set up JDK 11 for running checks
        uses: actions/setup-java@v1
        with:
          java-version: 11

      - name: Restore cache
        uses: burrunan/gradle-cache-action@v1.5
        with:
          job-id: jdk11

      - name: Build
        env:
          S3_BUILD_CACHE_ACCESS_KEY_ID: ${{ secrets.S3_BUILD_CACHE_ACCESS_KEY_ID }}
          S3_BUILD_CACHE_SECRET_KEY: ${{ secrets.S3_BUILD_CACHE_SECRET_KEY }}
        run: ./gradlew build --stacktrace

  example-distro:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Set up JDK 11 for running checks
        uses: actions/setup-java@v1
        with:
          java-version: 11

      - name: Restore cache
        uses: burrunan/gradle-cache-action@v1.6
        with:
          job-id: jdk11

      - name: Build
        run: ./gradlew build --stacktrace
        working-directory: examples/distro

  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        java: [ 8, 15 ]
      fail-fast: false
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - id: setup-test-java
        name: Set up JDK ${{ matrix.java }} for running tests
        uses: actions/setup-java@v1
        with:
          java-version: ${{ matrix.java }}

      - name: Set up JDK 11 for running Gradle
        uses: actions/setup-java@v1
        with:
          java-version: 11

      - name: Restore cache
        uses: burrunan/gradle-cache-action@v1.5
        with:
          job-id: jdk${{ matrix.java }}

      - name: Test
        env:
          S3_BUILD_CACHE_ACCESS_KEY_ID: ${{ secrets.S3_BUILD_CACHE_ACCESS_KEY_ID }}
          S3_BUILD_CACHE_SECRET_KEY: ${{ secrets.S3_BUILD_CACHE_SECRET_KEY }}
        run: ./gradlew test -PtestJavaVersion=${{ matrix.java }} --stacktrace -Porg.gradle.java.installations.paths=${{ steps.setup-test-java.outputs.path }} -Porg.gradle.java.installations.auto-download=false
