name: Deploy Jekyll site to GitHub Pages

on:
  push:
    branches: ["main"]
    paths:
      - 'site/**'
      - 'docs/esempi/**'
      - '.github/workflows/jekyll.yml'
  workflow_dispatch:

concurrency:
  group: "pages"
  cancel-in-progress: false

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

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 2

      - name: Setup Pages
        uses: actions/configure-pages@v5

      - name: Setup Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: "3.3"
          bundler-cache: true
          cache-version: 0
          working-directory: site

      - name: Build with Jekyll
        run: bundle exec jekyll build
        working-directory: site
        env:
          JEKYLL_ENV: production

      - name: Publish examples folder
        run: |
          rm -rf site/_site/esempi
          cp -R docs/esempi site/_site/esempi

      - name: Upload Pages artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: site/_site

      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
