name: build
on:
  push:
    branches:
      - main
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: "install dependencies"
        run: apt update && apt install --no-install-recommends -y mkdocs mkdocs-material mkdocs-material-extensions
      - name: "build & push"
        run: mkdocs build
      - uses: actions/upload-artifact@v3
        with:
          name: site
          path: site/
  deploy:
    runs-on: ubuntu-latest
    if: ${{ startsWith(secrets.SSH_PRIVATE_KEY, '-----BEGIN OPENSSH PRIVATE KEY-----') }}
    needs: [build]
    steps:
      - name: Install dependencies
        run: apt-get update && apt-get install --no-install-recommends -y rsync openssh-client
      - name: Install SSH key
        run: |
          set -e
          echo -e 'Installing ssh keys...'
          mkdir -p ~/.ssh
          chmod 700 ~/.ssh
          echo -e "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_foobar
          echo -e "${{ vars.SSH_PUBLIC_KEY }}" > ~/.ssh/id_foobar.pub
          chmod 600 ~/.ssh/*
#      - name: Test SSH connection
#        run: |
#          echo -e 'Testing SSH by connecting to ${{ vars.SSH_USER }}@${{ vars.SSH_HOST }}...'
#          ssh -v -i ~/.ssh/id_foobar -o StrictHostKeyChecking=no ${{ vars.SSH_USER }}@${{ vars.SSH_HOST }} exit 0
#          echo -e 'Established SSH connection!'  
      - uses: actions/download-artifact@v3         
      - name: Send built pages to server
        run: rsync -e "ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_foobar" -rclthv --exclude .git --delete-after site/ ${{ vars.SSH_USER }}@${{ vars.SSH_HOST }}:${{ vars.SSH_PATH }}
