name: Deploy to Scaleway

on:
  pull_request:
    types: [closed]
    branches:
      - main

jobs:
  build-and-deploy:
    # On ne déploie que si la PR est mergée (et non juste fermée sans merge)
    if: github.event.pull_request.merged == true
    runs-on: self-hosted

    container:
      image: node:20-bookworm

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          submodules: recursive
          fetch-depth: 0

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: "latest"
          extended: true

      - name: Build Hugo site
        run: hugo --minify

      - name: Install Scaleway CLI
        run: |
          curl -s https://raw.githubusercontent.com/scaleway/scaleway-cli/master/scripts/get.sh | sh
          scw version

      - name: Deploy to Object Storage
        env:
          SCW_ACCESS_KEY: ${{ secrets.SCW_ACCESS_KEY }}
          SCW_SECRET_KEY: ${{ secrets.SCW_SECRET_KEY }}
          SCW_DEFAULT_ORGANIZATION_ID: ${{ secrets.SCW_ORGANIZATION_ID }}
          SCW_DEFAULT_REGION: fr-par
          SCW_DEFAULT_ZONE: fr-par-1
        run: |
          # Copie récursive du dossier public vers le bucket S3
          scw object cp public/ s3://notebook-arnodo-fr/ --recursive

      - name: Purge CDN Cache
        env:
          SCW_ACCESS_KEY: ${{ secrets.SCW_ACCESS_KEY }}
          SCW_SECRET_KEY: ${{ secrets.SCW_SECRET_KEY }}
          SCW_DEFAULT_ORGANIZATION_ID: ${{ secrets.SCW_ORGANIZATION_ID }}
          SCW_DEFAULT_REGION: fr-par
        run: |
          scw edge-services pipeline purge pipeline-id=${{ secrets.SCW_PIPELINE_ID }}