name: Build and Deploy API
on:
  push:
    paths:
      - 'finnow-api/**'
      - '.gitea/workflows/api.yaml'
    branches:
      - main
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dlang-community/setup-dlang@v2
        with:
          compiler: ldc-latest
      - name: Test
        run: dub test
        working-directory: ./finnow-api
      - name: Build
        run: dub build --build=release
        working-directory: ./finnow-api
      - name: Install SSH Key
        uses: shimataro/ssh-key-action@v2
        with:
          key: ${{ secrets.DEPLOY_KEY }}
          known_hosts: ${{ secrets.SSH_HOST }}
      - name: Setup rsync
        uses: GuillaumeFalourd/setup-rsync@v1.2
      - name: Upload API Executable
        run: rsync -avz -e ssh --delete finnow-api root@andrewlalis.com:/opt/finnow/finnow-api
        working-directory: ./finnow-api
      - name: Restart API
        run: ssh -f root@andrewlalis.com 'systemctl restart finnow-api.service'
