name: Deploy Documentation

on:
  push:
    branches: [main]
    paths:
      # Dokka sources (KDoc in source files)
      - 'androidApp/src/**'
      - 'core/**/src/**'
      - 'feature/**/src/**'
      - 'desktopApp/src/**'
      # Docs site sources
      - 'docs/**'
      - 'feature/docs/**'
      # Build infrastructure
      - 'build-logic/**'
      - 'build.gradle.kts'
      - 'settings.gradle.kts'
      - '.github/workflows/docs-deploy.yml'

  workflow_dispatch:

permissions:
  contents: read
  pages: write
  id-token: write

concurrency:
  group: pages
  cancel-in-progress: true

jobs:
  build:
    if: github.repository == 'meshtastic/Meshtastic-Android'
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout
        uses: actions/checkout@v7.0.0
        with:
          submodules: true
          fetch-depth: 0

      - name: Gradle Setup
        uses: ./.github/actions/gradle-setup
        with:
          gradle_encryption_key: ${{ secrets.GRADLE_ENCRYPTION_KEY }}

      - name: Setup Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: '4.0.4'
          bundler-cache: true
          working-directory: docs

      - name: Generate Docs Site
        run: ./gradlew generateDocsBundle validateDocsBundle publishDocsSite -Pdocs.channel=root -Pci=true

      # Dokka (Gradle) and Jekyll (Ruby) are independent — Dokka's output is only
      # copied in afterwards — so run them concurrently to overlap the two slowest
      # steps (~10 min Dokka vs ~5 min Jekyll) instead of summing them (~16 -> ~11 min).
      - name: Build Dokka + Jekyll concurrently
        run: |
          set -euo pipefail
          BUNDLE_GEMFILE=docs/Gemfile bundle exec jekyll build \
            --source build/_site \
            --destination build/jekyll_site \
            --baseurl /${{ github.event.repository.name }} &
          jekyll_pid=$!
          ./gradlew dokkaGeneratePublicationHtml --no-configuration-cache
          wait "$jekyll_pid"
          touch build/jekyll_site/.nojekyll

      - name: Assemble Pages artifact
        run: cp -r build/dokka/html build/jekyll_site/api  # Dokka HTML -> /api

      - name: Upload Pages Artifact
        uses: actions/upload-pages-artifact@v5
        with:
          path: build/jekyll_site/

  deploy:
    if: github.repository == 'meshtastic/Meshtastic-Android'
    needs: build
    runs-on: ubuntu-24.04-arm
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v5

