name: CI

on: [ pull_request, push ]

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        java: [ 8, 11 ]
    name: Test JDK ${{ matrix.java }}
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-java@v2
        with:
          java-version: ${{ matrix.java }}
          java-package: 'jdk'
          distribution: 'adopt'
          cache: 'gradle'
      - name: Build
        run: ./gradlew build
      - name: Upload Unit Test Results
        if: always()
        uses: actions/upload-artifact@v2
        with:
          name: test-results-jdk-${{ matrix.java }}
          path: komga/build/test-results/
      - name: Upload Unit Test Reports
        if: always()
        uses: actions/upload-artifact@v2
        with:
          name: test-reports-jdk-${{ matrix.java }}
          path: komga/build/reports/tests/

  publish-test-results:
    name: "Publish Unit Tests Results"
    needs: test
    runs-on: ubuntu-latest
    if: success() || failure()
    steps:
      - name: Download Artifacts
        uses: actions/download-artifact@v2
        with:
          path: artifacts
      - name: Publish Unit Test Results
        uses: EnricoMi/publish-unit-test-result-action@v1
        if: always()
        with:
          files: artifacts/test-results-*/**/*.xml

  webui:
    runs-on: ubuntu-latest
    name: Test webui builds
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '16'
          cache: 'npm'
          cache-dependency-path: |
            package-lock.json
            komga-webui/package-lock.json
      - uses: actions/setup-java@v2
        with:
          java-version: '8'
          java-package: 'jdk'
          distribution: 'adopt'
          cache: 'gradle'
      - name: npmBuild
        run: ./gradlew npmBuild
      - name: npmTest
        run: npm run test:unit
        working-directory: komga-webui

  release:
    name: Semantic Release
    runs-on: ubuntu-latest
    needs: [ test, webui ]
    if: ${{ github.event_name != 'pull_request' }}
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '16'
          cache: 'npm'
          cache-dependency-path: |
            package-lock.json
            komga-webui/package-lock.json
      - uses: actions/setup-java@v2
        with:
          java-version: '8'
          java-package: 'jdk'
          distribution: 'adopt'
          cache: 'gradle'
      - name: Install dependencies
        run: npm install --only=production
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      - name: Release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: npx semantic-release
