name: Documentation site

on:
  push:
    branches: [master]
    paths:
      - _config.yml
      - _data/**
      - _layouts/**
      - _includes/**
      - assets/**
      - docs/**
      - index.html
      - sitemap.xml
      - robots.txt
      - scripts/check_site.py
      - scripts/benchmark_report.py
      - tests/pages_contract_test.py
      - README.md
      - CONTRIBUTING.md
      - tests/browser/**
      - .github/workflows/pages.yml
  pull_request:
    paths:
      - _config.yml
      - _data/**
      - _layouts/**
      - _includes/**
      - assets/**
      - docs/**
      - index.html
      - sitemap.xml
      - robots.txt
      - scripts/check_site.py
      - scripts/benchmark_report.py
      - tests/pages_contract_test.py
      - README.md
      - CONTRIBUTING.md
      - tests/browser/**
      - .github/workflows/pages.yml
  workflow_dispatch:

permissions:
  contents: read
concurrency:
  group: pg_local_cache-pages-${{ github.ref }}
  cancel-in-progress: true
jobs:
  validate:
    name: Pages artifact
    runs-on: ubuntu-24.04
    timeout-minutes: 15
    env:
      SITE_BASE_URL: https://profundium.github.io/pg_local_cache/
    steps:
      - name: Check out source
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
      - name: Test documentation checks
        run: python3 tests/pages_contract_test.py
      - name: Build Jekyll site
        uses: actions/jekyll-build-pages@44a6e6beabd48582f863aeeb6cb2151cc1716697 # v1
        with:
          source: ./
          destination: ./_site
      - name: Check metadata
        run: python3 scripts/check_site.py _site --base-url "$SITE_BASE_URL"
      - name: Install browser test dependencies
        run: |
          python3 -m pip install -r tests/browser/requirements.txt
          python3 -m playwright install --with-deps chromium webkit
      - name: Test pages and user interactions
        run: python3 tests/browser/site_smoke.py _site
      - name: Keep screenshots, traces and checks
        if: always()
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: browser
          path: qa-artifacts/browser/
          retention-days: 14
          if-no-files-found: warn
      - name: Package the Pages artifact
        if: github.repository == 'profundium/pg_local_cache' && github.ref == 'refs/heads/master'
        uses: actions/upload-pages-artifact@56afc609e74202658d3ffba0e8f6dda462b719fa # v3.0.1
        with:
          name: pages
          path: _site
          retention-days: 14

  deploy:
    name: Publish verified Pages artifact
    needs: validate
    if: >-
      github.repository == 'profundium/pg_local_cache' &&
      github.ref == 'refs/heads/master' &&
      github.event_name != 'pull_request'
    runs-on: ubuntu-24.04
    timeout-minutes: 15
    permissions:
      contents: read
      pages: write
      id-token: write
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - name: Check Pages configuration
        uses: actions/configure-pages@983d7736d9b0ae728b81ab479565c72886d7745b # v5.0.0
      - name: Deploy the tested artifact without rebuilding
        id: deployment
        uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4
        with:
          artifact_name: pages
      - name: Check published site
        env:
          PAGES_URL: ${{ steps.deployment.outputs.page_url }}
        run: curl --fail --silent --show-error --location --retry 5 --retry-delay 5 "$PAGES_URL" >/dev/null
