name: Build / Deploy docs

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '20'

      - name: Install JS dependencies
        run: |
          npm install vitepress@1.3.4 tailwindcss@3.4.10

      - name: Build docs
        run: |
          cd docs
          npx tailwindcss -i .vitepress/theme/style.css -o .vitepress/theme/theme.css -c .vitepress/tailwind.config.js
          npm run docs:build

      - name: Push to docs repository
        run: |
          git clone https://${{ secrets.STATIC_REPO_TOKEN }}@github.com/${{ secrets.STATIC_REPO }}.git target-repo
          rm -rf target-repo/srv/opengist
          mkdir -p target-repo/srv/opengist
          cp -r docs/.vitepress/dist/* target-repo/srv/opengist/
          cd target-repo
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add .
          git commit -m "Deploy docs from ${{ github.repository }}@${{ github.sha }}" || echo "No changes to commit"
          git pull --rebase
          git push
