Ajouter un endpoint /api/rebuild pour déclencher le redéploiement production via webhook StoryBlok

This commit is contained in:
Jalil Arfaoui 2026-03-07 20:00:57 +01:00
parent 0788de3c05
commit 789f85609d
3 changed files with 34 additions and 1 deletions

View file

@ -4,3 +4,9 @@ PUBLIC_STORYBLOK_TOKEN=
# Mettre à true sur l'instance preview (SSR + visual editor StoryBlok) # Mettre à true sur l'instance preview (SSR + visual editor StoryBlok)
# Ne pas définir ou mettre à false en production (SSG) # Ne pas définir ou mettre à false en production (SSG)
# PUBLIC_STORYBLOK_IS_PREVIEW=true # 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=

View file

@ -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_TOKEN` | Public Access Token | Preview Access Token |
| `PUBLIC_STORYBLOK_IS_PREVIEW` | *(non défini)* | `true` | | `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` | | `CC_POST_BUILD_HOOK` | `npm run build` | `npm run build` |
| `HOST` | - | `0.0.0.0` | | `HOST` | - | `0.0.0.0` |
### Configuration StoryBlok ### Configuration StoryBlok
- **Settings > Visual Editor** : mettre l'URL de l'instance preview comme environnement par défaut - **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://<preview-instance>/api/rebuild` pour déclencher le rebuild production à chaque publication

25
src/pages/api/rebuild.ts Normal file
View file

@ -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 });
};