name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

env:
  JO_SUBPATH: "/"

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up Ruby
        uses: ruby/setup-ruby@v1

      - name: "Build site"
        run: bash -c scripts/build.sh

      - name: "Create tarball"
        run: tar czvf site.tar.gz build

      - name: "Upload tarball"
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          port: ${{ secrets.PORT }}
          key: ${{ secrets.KEY }}
          passphrase: ${{ secrets.PASSPHRASE }}
          source: "site.tar.gz"
          target: "tmp"

      - name: "Update file system"
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          port: ${{ secrets.PORT }}
          key: ${{ secrets.KEY }}
          passphrase: ${{ secrets.PASSPHRASE }}
          script: |
            cd tmp
            tar xzvf site.tar.gz
            rm -rf ${{ secrets.DEST }}/*
            cp -r build/* ${{ secrets.DEST }}/
            rm -r build site.tar.gz
