name: Build and deploy docs

on:
  push:
    branches: [master]
    paths:
      - 'docs/**'
  workflow_dispatch:

concurrency:
  group: docs
  cancel-in-progress: false

jobs:
  build:
    runs-on: node20
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v2
      - name: Install dependencies
        run: pnpm install --frozen-lockfile
        working-directory: docs
      - name: Build with VitePress
        working-directory: docs
        run: |
          pnpm run build
          touch .vitepress/dist/.nojekyll
          echo mtcute.dev > .vitepress/dist/CNAME
          echo "ignore-workspace-root-check=true" >> ../.npmrc
      - name: Deploy
        # do not run on forks
        if: github.repository == 'teidesu/mtcute'
        uses: https://github.com/cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CLOUDFLARE_PAGES_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          command: pages deploy docs/.vitepress/dist --project-name=mtcute-docs
