name: Eagle0pedia Build

on:
  push:
    branches: [ "main" ]
    paths:
      - 'docs/eagle0pedia/**'
      - 'ci/github_actions/ensure_bazel_installed.sh'
      - '.github/actions/setup-bazel/**'
      - '.github/workflows/eagle0pedia_build.yml'
      - 'MODULE.bazel'
      - 'MODULE.bazel.lock'
      - '.bazelrc'
  pull_request:
    paths:
      - 'docs/eagle0pedia/**'
      - 'ci/github_actions/ensure_bazel_installed.sh'
      - '.github/actions/setup-bazel/**'
      - '.github/workflows/eagle0pedia_build.yml'
      - 'MODULE.bazel'
      - 'MODULE.bazel.lock'
      - '.bazelrc'
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
  build:
    runs-on: [self-hosted, bazel]
    steps:
      - name: Prepare workspace
        run: |
          chmod -R u+w eagle0pedia-dist 2>/dev/null || true
          rm -rf eagle0pedia-dist

      - name: Prepare non-LFS checkout
        env:
          GIT_CONFIG_GLOBAL: ${{ runner.temp }}/gitconfig-no-lfs
          GIT_CONFIG_NOSYSTEM: "1"
        run: |
          git config --global --unset-all filter.lfs.process || true
          git config --global filter.lfs.smudge "cat"
          git config --global filter.lfs.clean "cat"
          git config --global filter.lfs.required false
          if [ -d ".git" ]; then
            git config --local --unset-all filter.lfs.process || true
            git config --local filter.lfs.smudge "cat"
            git config --local filter.lfs.clean "cat"
            git config --local filter.lfs.required false
          fi
          rm -f .git/hooks/post-checkout .git/hooks/post-merge .git/hooks/pre-push

      - name: Checkout repository
        uses: actions/checkout@v7
        env:
          GIT_CONFIG_GLOBAL: ${{ runner.temp }}/gitconfig-no-lfs
          GIT_CONFIG_NOSYSTEM: "1"
        with:
          persist-credentials: false
          lfs: false

      - name: Setup Bazel
        uses: ./.github/actions/setup-bazel

      - name: Build Eagle0pedia
        run: bazel build //docs/eagle0pedia:site

      - name: Stage static site artifact
        run: |
          rm -rf eagle0pedia-dist
          mkdir -p eagle0pedia-dist
          cp -R bazel-bin/docs/eagle0pedia/dist/. eagle0pedia-dist/
          chmod -R u+w eagle0pedia-dist

      - name: Upload static site artifact
        uses: actions/upload-artifact@v7
        with:
          name: eagle0pedia-dist
          path: eagle0pedia-dist/
          if-no-files-found: error
          retention-days: 3
