stages:
  - render
  - commit

check_readme_changes:
  stage: render
  script:
    - |
      if git diff --name-only $CI_COMMIT_BEFORE_SHA $CI_COMMIT_SHA | grep -q 'README.qmd'; then
        echo "README.qmd modifié, rendu en cours."
        quarto render README.qmd
      else
        echo "Aucune modification de README.qmd, arrêt de la pipeline."
        exit 0
      fi
  artifacts:
    paths:
      - README.md

commit_and_push:
  stage: commit
  script:
    - |
      if git diff --name-only $CI_COMMIT_BEFORE_SHA $CI_COMMIT_SHA | grep -q 'README.qmd'; then
        git config --global user.email "ci@example.com"
        git config --global user.name "CI Bot"
        git add README.md
        git commit -m "[ci skip]: maj auto du README.md après rendu Quarto"
        git push origin HEAD:$CI_COMMIT_REF_NAME
      else
        echo "Aucun commit/push nécessaire."
      fi
  only:
    - branches
  dependencies:
    - check_readme_changes
