From 789f85609d06b347c8e058bffa6a5f9352578f5d Mon Sep 17 00:00:00 2001 From: Jalil Arfaoui Date: Sat, 7 Mar 2026 20:00:57 +0100 Subject: [PATCH] =?UTF-8?q?Ajouter=20un=20endpoint=20/api/rebuild=20pour?= =?UTF-8?q?=20d=C3=A9clencher=20le=20red=C3=A9ploiement=20production=20via?= =?UTF-8?q?=20webhook=20StoryBlok?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 6 ++++++ README.md | 4 +++- src/pages/api/rebuild.ts | 25 +++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/pages/api/rebuild.ts diff --git a/.env.example b/.env.example index 17ffaf4..78a54fc 100644 --- a/.env.example +++ b/.env.example @@ -4,3 +4,9 @@ PUBLIC_STORYBLOK_TOKEN= # Mettre à true sur l'instance preview (SSR + visual editor StoryBlok) # Ne pas définir ou mettre à false en production (SSG) # PUBLIC_STORYBLOK_IS_PREVIEW=true + +# Webhook rebuild — uniquement sur l'instance preview +# Token OAuth Clever Cloud (généré via clever login puis ~/.config/clever-cloud/clever-tools.json) +# CLEVER_TOKEN= +# ID de l'application Clever Cloud de production (app_xxxxxxxx) +# CLEVER_APP_ID_PRODUCTION= diff --git a/README.md b/README.md index b3dbd49..3965abb 100644 --- a/README.md +++ b/README.md @@ -49,10 +49,12 @@ Le serveur démarre sur le port `8080` par défaut (configurable via `PORT`). |---|---|---| | `PUBLIC_STORYBLOK_TOKEN` | Public Access Token | Preview Access Token | | `PUBLIC_STORYBLOK_IS_PREVIEW` | *(non défini)* | `true` | +| `CLEVER_TOKEN` | - | Token OAuth Clever Cloud | +| `CLEVER_APP_ID_PRODUCTION` | - | ID de l'app production (app_xxx) | | `CC_POST_BUILD_HOOK` | `npm run build` | `npm run build` | | `HOST` | - | `0.0.0.0` | ### Configuration StoryBlok - **Settings > Visual Editor** : mettre l'URL de l'instance preview comme environnement par défaut -- **Settings > Webhooks** : configurer un webhook vers l'instance production pour déclencher le rebuild à chaque publication +- **Settings > Webhooks** : configurer un webhook `POST` vers `https:///api/rebuild` pour déclencher le rebuild production à chaque publication diff --git a/src/pages/api/rebuild.ts b/src/pages/api/rebuild.ts new file mode 100644 index 0000000..5f6f770 --- /dev/null +++ b/src/pages/api/rebuild.ts @@ -0,0 +1,25 @@ +import type { APIRoute } from 'astro'; + +export const POST: APIRoute = async () => { + const token = import.meta.env.CLEVER_TOKEN; + const appId = import.meta.env.CLEVER_APP_ID_PRODUCTION; + + if (!token || !appId) { + return new Response('Missing CLEVER_TOKEN or CLEVER_APP_ID_PRODUCTION', { status: 500 }); + } + + const response = await fetch( + `https://api.clever-cloud.com/v2/self/applications/${appId}/instances`, + { + method: 'POST', + headers: { Authorization: `Bearer ${token}` }, + }, + ); + + if (!response.ok) { + const body = await response.text(); + return new Response(`Clever Cloud API error: ${response.status} ${body}`, { status: 502 }); + } + + return new Response('Rebuild triggered', { status: 200 }); +};