name: Docs (mdBook)

on:
  push:
    branches: [main]
    paths: ["docs/**", ".github/workflows/docs.yml"]
  pull_request:
    paths: ["docs/**", ".github/workflows/docs.yml"]
  workflow_dispatch:

permissions:
  contents: read

jobs:
  build:
    name: Build guide
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install mdBook
        run: |
          curl -sSL https://github.com/rust-lang/mdBook/releases/download/v0.4.40/mdbook-v0.4.40-x86_64-unknown-linux-gnu.tar.gz | tar -xz -C /usr/local/bin
      - name: Build
        run: mdbook build docs
      - uses: actions/upload-pages-artifact@v3
        with:
          path: docs/book

  deploy:
    name: Deploy to Pages
    if: github.ref == 'refs/heads/main'
    needs: build
    runs-on: ubuntu-latest
    permissions:
      pages: write
      id-token: write
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - id: deployment
        uses: actions/deploy-pages@v4
