name: Deploy API

on:
  push:
    branches:
      - master
      - multi-hotel-refactor

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

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

      - name: Build Docker image
        run: |
          docker build -t hotel-demo:latest .

      - name: Save Docker image
        run: |
          docker save hotel-demo:latest -o hotel-demo.tar


      - name: setup SSH
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519
          ssh-keyscan 79.137.75.155 >> ~/.ssh/known_hosts

          #Upload
          scp hotel-demo.tar deploy@79.137.75.155:/tmp
      
          docker load -i hotel-demo.tar

          docker stop hotel-demo || true
          docker rm hotel-demo || true

          docker run -d \
              --name hotel-demo \
              -e JWT_SECRET=${{ secrets.JWT_SECRET }} \
              -v "/var/lib/hotel-demo/:/app/db" \
              -p 5090:7080 \
              hotel-demo:latest

          rm hotel-demo.tar
            