name: Build & Deploy

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v3
        with:
          hugo-version: latest
          extended: true

      - name: Build
        run: hugo --minify

      - name: Build search index
        run: npx pagefind@latest --site public

      - name: Deploy via scp
        env:
          SSH_KEY: ${{ secrets.SSH_KEY }}
        run: |
          mkdir -p ~/.ssh
          echo "$SSH_KEY" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519
          ssh-keyscan -H pyte.dev >> ~/.ssh/known_hosts
          ssh -i ~/.ssh/id_ed25519 demian@pyte.dev "rm -rf /opt/docker/website/public"
          scp -i ~/.ssh/id_ed25519 -r public demian@pyte.dev:/opt/docker/website/
          ssh -i ~/.ssh/id_ed25519 demian@pyte.dev "chmod -R 755 /opt/docker/website/public && docker ps -q --filter name=nginx | xargs -r docker restart"
