name: Build & Deploy

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

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

      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '8.0.x'

      - name: Restore dependencies
        run: dotnet restore src/

      - name: Build
        run: dotnet build src/ --configuration Release --no-restore

      - name: Publish
        run: dotnet publish src/ -c Release -o publish

      - name: Copy files to server
        uses: appleboy/scp-action@v0.1.7
        with:
          host: ${{ secrets.DEPLOY_HOST }}
          username: ${{ secrets.DEPLOY_USER }}
          key: ${{ secrets.LD50_DEV_DEPLOY_SSH_KEY }}
          port: 7022
          source: "publish/*"
          target: "/var/www/Berufsschule_HAM"
          strip_components: 1

      - name: Restart app
        uses: appleboy/ssh-action@v1.2.0
        with:
          host: ${{ secrets.DEPLOY_HOST }}
          username: ${{ secrets.DEPLOY_USER }}
          key: ${{ secrets.LD50_DEV_DEPLOY_SSH_KEY }}
          port: 7022
          script: |
            sudo systemctl restart Berufsschule_HAM.service