name: Build and Deploy

on:
  push:
    branches: ["main"]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Java
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '21'

      - name: Build
        run: mvn -B clean package

      - name: Upload to JFrog
        run: |
          curl -u "${{ secrets.JFROG_USER }}:${{ secrets.JFROG_PASS }}" \
               -T target/*.jar \
               "http://JFROG-SERVER/artifactory/myrepo/app-latest.jar"

  deploy:
    needs: build
    runs-on: ubuntu-latest

    steps:
      - name: Trigger remote deploy script
        uses: appleboy/ssh-action@v0.1.7
        with:
          host: ${{ secrets.SERVER_IP }}
          username: ${{ secrets.SERVER_USER }}
          key: ${{ secrets.SERVER_SSH_KEY }}
          script: |
            /opt/myapp/update.sh
