name: Nightly build

on:
  schedule:
    # strange schedule to reduce the risk of DDOS GitHub infra
    - cron: "24 3 * * *"
  workflow_dispatch:

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.6
        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

  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.6
        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

  testLatestDep:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Set up JDK 11
        uses: actions/setup-java@v1
        with:
          java-version: 11

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

      - 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 -PtestLatestDeps=true --stacktrace

  snapshot:
    runs-on: ubuntu-latest
    needs: [ build, test, testLatestDep ]
    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: Publish snapshot
        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 }}
          BINTRAY_USER: ${{ secrets.BINTRAY_USER }}
          BINTRAY_API_KEY: ${{ secrets.BINTRAY_API_KEY }}
          SONATYPE_USER: ${{ secrets.SONATYPE_USER }}
          SONATYPE_KEY: ${{ secrets.SONATYPE_KEY }}
          GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
          GPG_PASSWORD: ${{ secrets.GPG_PASSWORD }}
        run: ./gradlew snapshot --stacktrace

  issue:
    name: Open issue on failure
    needs: [ build, test, testLatestDep, snapshot ]
    runs-on: ubuntu-latest
    if: always()
    steps:
      # run this action to get workflow conclusion
      # You can get conclusion by env (env.WORKFLOW_CONCLUSION)
      - uses: technote-space/workflow-conclusion-action@v1
      - uses: actions/checkout@v2
      - uses: JasonEtco/create-an-issue@v2
        if: env.WORKFLOW_CONCLUSION == 'failure' # notify only if failure
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          filename: .github/workflows/nightly-failed.md
