Déploiement sur VPS : workflow Gitea Actions, release atomique, en-têtes nginx
- .gitea/workflows/deploy.yml : build + publication à chaque push sur main - deploy/release.sh, rollback.sh : bascule atomique par lien symbolique, 3 versions - deploy/headers-to-nginx.mjs : dist/_headers -> nginx, sans duplication - vite.config.ts : la garde VITE_SUPABASE_* s'applique aussi avec REQUIRE_SUPABASE=1 - README et privacy.html : hébergeur Netlify remplacé par le VPS Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fcc9670f29
commit
e507d73982
7 files changed
+159
-15
No files matched your search
Executable
+60
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env node
|
||||
// Convertit dist/_headers (format Netlify, généré par vite.config.ts) en snippets nginx.
|
||||
// Usage : node deploy/headers-to-nginx.mjs dist/_headers <dossier-de-sortie>
|
||||
// Sortie : security-headers.conf (bloc "/*") et locations.conf (un "location =" par autre chemin).
|
||||
// Les en-têtes ne sont écrits qu'à un seul endroit (vite.config.ts) : rien à dupliquer à la main.
|
||||
import { readFileSync, writeFileSync, mkdirSync } from 'node:fs'
|
||||
|
||||
const [, , input, outDir] = process.argv
|
||||
if (!input || !outDir) {
|
||||
console.error('usage: headers-to-nginx.mjs <_headers> <outDir>')
|
||||
process.exit(2)
|
||||
}
|
||||
|
||||
const blocks = new Map() // chemin -> [[nom, valeur], ...]
|
||||
let current = null
|
||||
for (const raw of readFileSync(input, 'utf8').split('\n')) {
|
||||
const line = raw.replace(/\s+$/, '')
|
||||
if (!line || line.startsWith('#')) continue
|
||||
if (!/^\s/.test(line)) {
|
||||
current = line.trim()
|
||||
if (!/^\/[A-Za-z0-9._\/*-]*$/.test(current)) fail(`chemin invalide : ${current}`)
|
||||
blocks.set(current, [])
|
||||
continue
|
||||
}
|
||||
if (current === null) fail(`en-tête sans chemin : ${line}`)
|
||||
const i = line.indexOf(':')
|
||||
const name = line.slice(0, i).trim()
|
||||
const value = line.slice(i + 1).trim()
|
||||
if (!/^[A-Za-z][A-Za-z0-9-]*$/.test(name) || !value) fail(`en-tête invalide : ${line}`)
|
||||
if (/[$"\\]/.test(value)) fail(`caractère interdit dans ${name}`)
|
||||
blocks.get(current).push([name, value])
|
||||
}
|
||||
|
||||
function fail(msg) {
|
||||
console.error(`headers-to-nginx: ${msg}`)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const add = (h) => h.map(([n, v]) => `add_header ${n} "${v}" always;`)
|
||||
const global = blocks.get('/*')
|
||||
if (!global || !global.some(([n]) => n.toLowerCase() === 'content-security-policy')) {
|
||||
fail('bloc "/*" absent ou sans Content-Security-Policy : refus de publier sans CSP')
|
||||
}
|
||||
|
||||
mkdirSync(outDir, { recursive: true })
|
||||
writeFileSync(`${outDir}/security-headers.conf`, add(global).join('\n') + '\n')
|
||||
|
||||
const locations = []
|
||||
for (const [path, h] of blocks) {
|
||||
if (path === '/*') continue
|
||||
if (path.includes('*')) fail(`motif non géré : ${path}`)
|
||||
locations.push(
|
||||
`location = ${path} {`,
|
||||
' include /srv/azuucine/current/nginx/security-headers.conf;',
|
||||
...add(h).map((l) => ` ${l}`),
|
||||
'}',
|
||||
)
|
||||
}
|
||||
writeFileSync(`${outDir}/locations.conf`, locations.join('\n') + '\n')
|
||||
console.log(`ok : ${global.length} en-têtes globaux, ${locations.filter((l) => l.startsWith('location')).length} chemin(s) spécifique(s)`)
|
||||
Executable
+25
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env bash
|
||||
# Publie dist/ de façon atomique dans /srv/azuucine/releases/<date>-<sha> puis bascule le lien "current".
|
||||
# Garde les 3 dernières versions. Lancé par le workflow (utilisateur "deploy", sans sudo).
|
||||
set -euo pipefail
|
||||
ROOT=${AZUUCINE_ROOT:-/srv/azuucine}
|
||||
KEEP=3
|
||||
SRC=${1:?usage: release.sh <dist> <sha>}
|
||||
SHA=${2:?usage: release.sh <dist> <sha>}
|
||||
HERE=$(cd "$(dirname "$0")" && pwd)
|
||||
|
||||
[ -f "$SRC/index.html" ] && [ -f "$SRC/_headers" ] || { echo "dist/ incomplet (index.html ou _headers manquant)"; exit 1; }
|
||||
REL="$ROOT/releases/$(date -u +%Y%m%d%H%M%S)-${SHA:0:8}"
|
||||
mkdir -p "$REL"
|
||||
cp -a "$SRC" "$REL/dist"
|
||||
node "$HERE/headers-to-nginx.mjs" "$REL/dist/_headers" "$REL/nginx"
|
||||
|
||||
# bascule atomique : lien relatif temporaire puis rename
|
||||
ln -s "releases/$(basename "$REL")" "$ROOT/current.tmp"
|
||||
mv -T "$ROOT/current.tmp" "$ROOT/current"
|
||||
date -u +%FT%TZ > "$ROOT/.deployed" # déclenche le rechargement de nginx dans le conteneur web
|
||||
echo "publié : $(basename "$REL")"
|
||||
|
||||
# purge : ne garder que les $KEEP plus récentes (la courante est toujours la plus récente)
|
||||
ls -1d "$ROOT"/releases/*/ | sort -r | tail -n +$((KEEP + 1)) | xargs -r rm -rf
|
||||
ls -1 "$ROOT/releases"
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env bash
|
||||
# Revient à la version précédente (ou à celle donnée en argument). À lancer en root sur le VPS :
|
||||
# /srv/azuucine/rollback.sh -> version juste avant la courante
|
||||
# /srv/azuucine/rollback.sh <nom> -> une version précise (voir ls /srv/azuucine/releases)
|
||||
set -euo pipefail
|
||||
ROOT=${AZUUCINE_ROOT:-/srv/azuucine}
|
||||
CUR=$(basename "$(readlink "$ROOT/current")")
|
||||
if [ -n "${1:-}" ]; then TARGET=$1
|
||||
else TARGET=$(ls -1 "$ROOT/releases" | sort -r | grep -A1 -x "$CUR" | tail -n1)
|
||||
fi
|
||||
[ -n "$TARGET" ] && [ "$TARGET" != "$CUR" ] && [ -d "$ROOT/releases/$TARGET" ] || { echo "aucune version cible"; exit 1; }
|
||||
ln -s "releases/$TARGET" "$ROOT/current.tmp"; mv -T "$ROOT/current.tmp" "$ROOT/current"
|
||||
date -u +%FT%TZ > "$ROOT/.deployed"
|
||||
echo "retour : $CUR -> $TARGET"
|
||||
Reference in new issue
Block a user