name: Docs

on:
  push:
    branches: [ master ]
    paths:
      - 'docs/**'
      - 'mkdocs.yml'
      - 'scripts/build_settings_doc.py'
      - 'gunicorn/config.py'
      - 'requirements_dev.txt'
      - '.github/workflows/docs.yml'
  pull_request:
    paths:
      - 'docs/**'
      - 'mkdocs.yml'
      - 'scripts/build_settings_doc.py'
      - 'gunicorn/config.py'
      - 'requirements_dev.txt'
      - '.github/workflows/docs.yml'
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: '3.12'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -e .
          pip install -r requirements_dev.txt

      - name: Build documentation
        run: mkdocs build

      - name: Upload site artifact
        uses: actions/upload-artifact@v7
        with:
          name: gunicorn-site
          path: site
          retention-days: 7

  deploy:
    if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/master'
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v6

      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: '3.12'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -e .
          pip install -r requirements_dev.txt

      - name: Build documentation
        run: mkdocs build

      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v4
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: site
          publish_branch: gh-pages
          cname: gunicorn.org
          commit_message: "docs: deploy ${{ github.sha }}"
