name: Build

on:
  push:
    branches:
      - master

env:
  ZOLA_VERSION: "0.18.0"
  HOST: ${{ secrets.SSH_HOSTNAME }}
  HOST_DIR: ${{ secrets.SSH_TARGET_DIR }}
  SSH_USERNAME: ${{ secrets.SSH_USERNAME }}
  SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}

jobs:
  build:
    runs-on: ubuntu-latest

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

      - name: Install Dependencies
        run: |
          apt update -y
          apt-get install -y --no-install-recommends \
          cpanminus ghostscript make rsync \
          libpdf-api2-perl libimage-info-perl

      - name: Install Chordpro
        run: cpanm install chordpro

      - 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 Chordpro
        run: make

      - name: Build Zola
        run: ./zola build

      - name: Deploy
        run: |
          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 -ra --delete-after public/* "${SSH_USERNAME}@${HOST}:${HOST_DIR}"
