name: Build

on:
  push:
    branches:
      - main

env:
  ZOLA_VERSION: ${{ vars.ZOLA_VERSION }}
  HOST: ${{ vars.HOST }}
  HOST_DIR: ${{ vars.HOST_DIR }}
  SSH_USERNAME: ${{ secrets.SSH_USERNAME }}
  SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}

jobs:
  deploy:
    runs-on: ubuntu-latest

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

      - name: Install Zola
        run: |
          wget https://github.com/getzola/zola/releases/download/v${ZOLA_VERSION}/zola-v${ZOLA_VERSION}-x86_64-unknown-linux-gnu.tar.gz
          tar -xvzf *.tar.gz

      - name: Build
        run: ./zola build

      - name: Write SSH Keys
        run: |
          install -m 600 -D /dev/null ~/.ssh/id_ed25519
          echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
          ssh-keyscan -H 192.168.0.21 > ~/.ssh/known_hosts

      - name: Copy built site to server
        run: | 
          ssh cj@192.168.0.21 "rm -rf /var/www/cjsatnarine.space && mkdir /var/www/cjsatnarine.space"
          scp -sr public/. cj@192.168.0.21:/var/www/cjsatnarine.space

#      - name: Deploy
#        run: |
#          apt update -y && apt-get install -y --no-install-recommends rsync
#          eval "$(ssh-agent -s)"
#          ssh-add - <<< "${SSH_PRIVATE_KEY}"
#          mkdir -p ~/.ssh/
#          ssh-keyscan -H ${HOST} >> ~/.ssh/known_hosts
#          rsync -r --delete-after public/* "${SSH_USERNAME}@${HOST}:${HOST_DIR}"
