name: Build and Deploy Auction App

on:
  push:
    branches:
      - main

jobs:
  build_and_deploy:
    runs-on: ubuntu-latest

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

      - name: Install Maven
        run: |
          apt update
          apt install -y maven

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

      - name: Build with Maven
        run: mvn -B clean package

      - name: Copy jar to server (no tar)
        uses: appleboy/scp-action@v0.1.7
        with:
          host: ${{ secrets.SERVER_IP }}
          username: ${{ secrets.SERVER_USER }}
          key: ${{ secrets.SERVER_SSH_KEY }}
          source: "target/*.jar"
          target: "/opt/auction/"
          overwrite: true
          strip_components: 1  # Strips the 'target/' directory
          timeout: 60s

      - name: Restart service
        uses: appleboy/ssh-action@v0.1.7
        with:
          host: ${{ secrets.SERVER_IP }}
          username: ${{ secrets.SERVER_USER }}
          key: ${{ secrets.SERVER_SSH_KEY }}
          script: |
            systemctl restart auction
            echo "Deploy complete"
