name: Docs build
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  release:
    types: [published]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  pre-job:
    runs-on: ubuntu-latest
    outputs:
      should_run: ${{ steps.found_paths.outputs.docs == 'true' || steps.should_force.outputs.should_force == 'true' }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - id: found_paths
        uses: dorny/paths-filter@v3
        with:
          filters: |
            docs:
              - 'docs/**'
      - name: Check if we should force jobs to run
        id: should_force
        run: echo "should_force=${{ github.event_name == 'release' || github.ref_name == 'main' }}" >> "$GITHUB_OUTPUT"

  build:
    name: Docs Build
    needs: pre-job
    if: ${{ needs.pre-job.outputs.should_run == 'true' }}
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./docs

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

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version-file: './docs/.nvmrc'

      - name: Run npm install
        run: npm ci

      - name: Check formatting
        run: npm run format

      - name: Run build
        run: npm run build

      - name: Upload build output
        uses: actions/upload-artifact@v4
        with:
          name: docs-build-output
          path: docs/build/
          retention-days: 1
