name: CI build

on:
  push:
    branches:
      - master

jobs:
  check_wrapper:
    name: Validate Gradle Wrapper
    runs-on: ubuntu-latest

    steps:
      - name: Clone repo
        uses: actions/checkout@v2

      - name: Validate Gradle Wrapper
        uses: gradle/wrapper-validation-action@v1

  build:
    name: Build artifacts and deploy preview
    needs: check_wrapper
    if: "!startsWith(github.event.head_commit.message, '[SKIP CI]')"
    runs-on: ubuntu-latest

    steps:
      - name: Cancel previous runs
        uses: styfle/cancel-workflow-action@0.9.0
        with:
          access_token: ${{ github.token }}

      - name: Checkout master branch
        uses: actions/checkout@v2
        with:
          ref: master
          path: master
          fetch-depth: 0

      - name: Set up JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8

      - name: Copy CI gradle.properties
        run: |
          cd master
          mkdir -p ~/.gradle
          cp .github/runner-files/ci-gradle.properties ~/.gradle/gradle.properties

      - name: Build Jar
        uses: eskatos/gradle-command-action@v1
        env:
          ProductBuildType: "Preview"
        with:
          build-root-directory: master
          wrapper-directory: master
          arguments: :server:shadowJar --stacktrace
          wrapper-cache-enabled: true
          dependencies-cache-enabled: true
          configuration-cache-enabled: true

      - name: Generate Tag Name
        id: GenTagName
        run: |
          cd master/server/build
          genTag=$(ls *.jar | sed -e's/Tachidesk-Server-\|.jar//g')
          echo "$genTag"
          echo "::set-output name=value::$genTag"

      - name: make bundle packages
        run: |
          cd master/scripts
          ./windows-bundler.sh win32
          ./windows-bundler.sh win64
          ./unix-bundler.sh linux-x64
          ./debian-packager.sh
          ./unix-bundler.sh macOS-x64
          ./unix-bundler.sh macOS-arm64

      - name: Checkout preview branch
        uses: actions/checkout@v2
        with:
          repository: 'Suwayomi/Tachidesk-Server-preview'
          ref: main
          path: preview
          token: ${{ secrets.DEPLOY_PREVIEW_TOKEN }}

      - name: Create Tag
        run: |
          TAG="${{ steps.GenTagName.outputs.value }}"
          echo "tag: $TAG"
          cd preview
          echo "{ \"latest\": \"$TAG\" }" > index.json
          git add index.json
          git config --global user.email "github-actions[bot]@users.noreply.github.com"
          git config --global user.name "github-actions[bot]"
          git commit -m "Updated to $TAG"
          git push origin main

          git tag $TAG
          git push origin $TAG

      - name: Upload Preview Release
        uses: ncipollo/release-action@v1
        with:
          token: ${{ secrets.DEPLOY_PREVIEW_TOKEN }}
          artifacts: "master/server/build/*.jar,master/server/build/*.msi,master/server/build/*.zip,master/server/build/*.tar.gz,master/server/build/*.deb"
          owner: "Suwayomi"
          repo: "Tachidesk-Server-preview"
          tag: ${{ steps.GenTagName.outputs.value }}
