name: Build and Deploy
on:
  push:
    branches: [master]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Build Docker image
        run: docker build -t silverlabs-website:latest -t silverlabs-website:${{ github.sha }} .
      - name: Deploy to server
        run: |
          # Install SSH tools
          apt-get update && apt-get install -y sshpass
          # Save and transfer image
          docker save silverlabs-website:latest | gzip > /tmp/silverlabs-website.tar.gz
          sshpass -p "${{ secrets.DEPLOY_PASSWORD }}" scp -o StrictHostKeyChecking=no /tmp/silverlabs-website.tar.gz sysadmin@10.0.0.247:/tmp/
          # SSH and deploy
          sshpass -p "${{ secrets.DEPLOY_PASSWORD }}" ssh -o StrictHostKeyChecking=no sysadmin@10.0.0.247 bash -s << 'EOF'
          set -e
          docker load < /tmp/silverlabs-website.tar.gz
          docker stop silverlabs-website 2>/dev/null || true
          docker rm silverlabs-website 2>/dev/null || true
          docker run -d \
            --name silverlabs-website \
            --restart unless-stopped \
            -p 8100:80 \
            silverlabs-website:latest
          docker ps | grep silverlabs-website
          rm /tmp/silverlabs-website.tar.gz
          echo "Deployment complete!"
          EOF
