name: Deploy project

on:
  # Runs on pushes targeting the default branch
  push:
    branches:
      - main

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  deploy:
    name: Build
    runs-on: ubuntu-latest
    if: gitea.repository == 'MultiMote/niimblue'

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

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: npm
          cache-dependency-path: ./package-lock.json

      - name: Install dependencies
        run: npm install

      - name: Check code
        run: npm run sv-check

      - name: Build
        run: npx vite build --base="${{ steps.pages.outputs.base_path || '/' }}"
        env:
          COMMIT_HASH: ${{gitea.sha}}

      - name: Generate Sitemap
        run: |
          echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
          <urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">
            <url>
              <loc>$PAGE_URL</loc>
              <lastmod>$LAST_MODIFIED</lastmod>
            </url>
          </urlset>" > ./dist/sitemap.xml
        env:
          PAGE_URL: ${{ steps.pages.outputs.origin }}
          LAST_MODIFIED: ${{ gitea.event.repository.updated_at }}

      - name: Deploy to Cloudflare Pages
        uses: cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          command: pages deploy dist --project-name=niimblue-cf
