name: CI
on: [ push, pull_request ]

jobs:
  deploy:
    env:
      REPO: ${{ github.event.repository.name }}
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup JDK
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: '21'

      - name: new Version
        if: github.ref_name == 'main'
        run: |
          ./mvnw -B --no-transfer-progress versions:set -DremoveSnapshot

      - name: Build Runner
        run: |
          ./mvnw -B --no-transfer-progress clean verify -Pnative -Dquarkus.native.remote-container-build=true -Djgitver.skip=true

      - name: Add coverage to PR
        id: jacoco
        uses: madrapps/jacoco-report@v1.7.1
        with:
          paths: ${{ github.workspace }}/**/target/coverage/jacoco.xml
          token: ${{ secrets.GITHUB_TOKEN }}
          min-coverage-overall: 40
          min-coverage-changed-files: 60
          title: ${{ env.REPO }} Coverage

      - id: version
        name: Version
        run: echo "VERSION=$(./mvnw help:evaluate -Dexpression=project.version -q -DforceStdout)" >> ${GITHUB_OUTPUT}

      - name: Build unstable Container
        if: github.ref_name != 'main'
        run: |
          echo ${{secrets.PACKAGES_TOKEN}} | docker login --username ${{ secrets.PACKAGES_USER }} --password-stdin git.arindy.de
          docker build -f src/main/docker/Dockerfile.native-micro -t git.arindy.de/arindy/dice-tower:unstable -t git.arindy.de/arindy/dice-tower:${{ steps.version.outputs.VERSION }} .
          docker push git.arindy.de/arindy/dice-tower:unstable

      - name: Build stable Container
        if: github.ref_name == 'main'
        run: |
          echo ${{secrets.PACKAGES_TOKEN}} | docker login --username ${{ secrets.PACKAGES_USER }} --password-stdin git.arindy.de
          docker build -f src/main/docker/Dockerfile.native-micro -t git.arindy.de/arindy/dice-tower:latest -t git.arindy.de/arindy/dice-tower:${{ steps.version.outputs.VERSION }} .
          docker push git.arindy.de/arindy/dice-tower:${{ steps.version.outputs.VERSION }}
          docker push git.arindy.de/arindy/dice-tower:latest

      - name: Prepare deploy
        run: |
          echo ${{secrets.SSH_KNOWN_HOSTS}} >> ~/.ssh/known_hosts
          base64 -d <<< ${{secrets.SSH_KEY}} > ./.key
          chmod 600 ./.key

      - name: Deploy unstable
        if: github.ref_name != 'main'
        run: "ssh -i ./.key dice-tower@${{secrets.SSH_HOST}} 'docker compose -f compose.unstable.yml pull && docker compose -f compose.unstable.yml up -d'  "

      - name: Deploy
        if: github.ref_name == 'main'
        run: "ssh -i ./.key dice-tower@${{secrets.SSH_HOST}} 'docker compose -f compose.yml pull && docker compose -f compose.yml up -d'  "

      - name: Deploy local
        if: github.ref_name == 'main'
        run: "docker compose up -d"

      - name: clean up
        run: |
          rm ./.key

      - name: create tag
        if: github.ref_name == 'main'
        run: |
          git config user.email "ci@git.arindy.de"
          git config user.name "gitea"
          git add ./pom.xml
          git commit -m "[no ci] release ${{ steps.version.outputs.VERSION }}"
          git tag ${{ steps.version.outputs.VERSION }} -m "release ${{ steps.version.outputs.VERSION }}"
          ./mvnw -B --no-transfer-progress clean validate -Pnew-snapshot
          git add ./pom.xml
          git commit -m "[no ci] prepare new Version"
          git push origin main
          git push origin ${{ steps.version.outputs.VERSION }}
