ycdemo / fastapi / .github/ workflows/ build-docs.yml

name: Build Docs
on:
  push:
    branches:
      - master
  pull_request:
permissions: {}

jobs:
  changes:
    runs-on: ubuntu-latest
    # Required permissions
    permissions:
      pull-requests: read
    timeout-minutes: 5
    # Set job outputs to values from filter step
    outputs:
      docs: ${{ steps.filter.outputs.docs }}
    steps:
    - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      with:
        persist-credentials: false
    # For pull requests it's not necessary to checkout the code but for the main branch it is
    - uses: dorny/paths-filter@ceb8a2b8f2d89434be7ff52d3de7ec3738c5cc9d # v4.0.3
      id: filter
      with:
        filters: |
          docs:
            - README.md
            - docs/**
            - docs_src/**
            - pyproject.toml
            - uv.lock
            - .github/workflows/build-docs.yml
            - .github/workflows/deploy-docs.yml
            - scripts/docs.py
  langs:
    needs:
      - changes
    if: ${{ needs.changes.outputs.docs == 'true' }}
    runs-on: ubuntu-latest
    timeout-minutes: 5
    outputs:
      langs: ${{ steps.show-langs.outputs.langs }}
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Set up Python
        uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
        with:
          python-version-file: ".python-version"
      - name: Setup uv
        uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2
        with:
          version: "latest-known"
          enable-cache: true
          cache-dependency-glob: |
            pyproject.toml
            uv.lock
      - name: Install docs extras
        run: uv sync --locked --no-dev --group docs
      - name: Export Language Codes
        id: show-langs
        run: |
          echo "langs=$(uv run ./scripts/docs.py langs-json)" >> $GITHUB_OUTPUT

  build-docs:
    needs:
      - changes
      - langs
    if: ${{ needs.changes.outputs.docs == 'true' }}
    runs-on: ubuntu-latest
    timeout-minutes: 7
    strategy:
      matrix:
        lang: ${{ fromJson(needs.langs.outputs.langs) }}
    steps:
      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Set up Python
        uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
        with:
          python-version-file: ".python-version"
      - name: Setup uv
        uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2
        with:
          version: "latest-known"
          enable-cache: true
          cache-dependency-glob: |
            pyproject.toml
            uv.lock
      - name: Install docs extras
        run: uv sync --locked --no-dev --group docs
      - name: Update Languages
        run: uv run ./scripts/docs.py update-languages
      - name: Build Docs
        run: | # zizmor: ignore[template-injection] - comes from trusted source
          uv run ./scripts/docs.py build-lang ${{ matrix.lang }}
      - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
        with:
          name: docs-site-${{ matrix.lang }}
          # English owns root static assets. Translated pages reference /img, /css,
          # and /js, so omit duplicated language-local copies from artifacts.
          path: |
            ./site/**
            !./site/${{ matrix.lang }}/img/**
            !./site/${{ matrix.lang }}/css/**
            !./site/${{ matrix.lang }}/js/**
          include-hidden-files: true

  # https://github.com/marketplace/actions/alls-green#why
  docs-all-green:  # This job does nothing and is only used for the branch protection
    if: always()
    needs:
      - langs
      - build-docs
    runs-on: ubuntu-latest
    steps:
      - name: Decide whether the needed jobs succeeded or failed
        uses: re-actors/alls-green@b5b5b37504aa4183270bd3d855c52a67f212be35 # v1.3.0
        with:
          jobs: ${{ toJSON(needs) }}
          allowed-skips: langs, build-docs