Repository Public
1#!/usr/bin/env bash2# =============================================================================3# SP BOUCHER | SOFTWARE & RESEARCH4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project : agentilab-web8# File : scripts/release.sh9# Module : Scripts10# Repository : https://git.spboucher.ai/agentilab-web11# Project Website : Not specified12# Created : 2026-12-0513# Last Modified : 2026-12-0514#15# Author : Simon-Pierre Boucher16# Position : Professor of Finance17# Institution : Université du Québec en Outaouais (UQO)18# Organization : SP Boucher19# Website : https://spboucher.ai20# Git : https://git.spboucher.ai21#22# Preferred Contact: contact@spboucher.ai23# Development : dev@spboucher.ai24# Support : support@spboucher.ai25# Security/Admin : admin@spboucher.ai26#27# Copyright (c) 202628# Simon-Pierre Boucher29# All rights reserved where applicable. This file is governed by the30# applicable project license, copyright notices and relevant law.31# Existing third-party rights and notices remain unaffected.32#33# License : Proprietary34#35# Do not embed credentials, private keys, access tokens,36# certificates or confidential personal information.37#38# =============================================================================39# END OF SP BOUCHER SOURCE HEADER40# =============================================================================4142# Mise en service atomique sur le nœud, avec retour arrière.43#44# scripts/release.sh [ref] construit `ref` (défaut : origin/main) à côté de la version en45# service, bascule, redémarre, vérifie la santé ; revient seul à46# la version précédente si la vérification échoue.47# scripts/release.sh --rollback remet en service la version précédente (.next-previous).48#49# À lancer dans le dossier déployé (voir `mld status`). Variables : PM2_NAME, PORT.50set -euo pipefail51cd "$(dirname "$0")/.."52export PATH="/opt/homebrew/bin:$PATH" NEXT_TELEMETRY_DISABLED=153PM2_NAME="${PM2_NAME:-agentilab-web}"54PORT="${PORT:-8236}"5556healthy() {57 for _ in $(seq 1 20); do58 [[ "$(curl -s -o /dev/null -w '%{http_code}' "http://127.0.0.1:${PORT}/healthz")" == 200 ]] && return 059 sleep 160 done61 return 162}63swap_back() {64 [[ -d .next-previous ]] || { echo "Aucune version précédente (.next-previous)."; exit 1; }65 rm -rf .next-failed66 mv .next .next-failed67 mv .next-previous .next68 [[ -f .release-previous ]] && git checkout --quiet "$(cat .release-previous)"69 pm2 restart "$PM2_NAME" --update-env >/dev/null70}7172if [[ "${1:-}" == "--rollback" ]]; then73 swap_back74 healthy && { echo "Retour arrière fait : $(git rev-parse --short HEAD) en service."; exit 0; }75 echo "ÉCHEC : la version précédente ne répond pas."; exit 176fi7778[[ -z "$(git status --porcelain)" ]] || { echo "Dépôt modifié sur le nœud : commiter ou écarter avant de publier."; exit 1; }79REF="${1:-origin/main}"80PREVIOUS="$(git rev-parse HEAD)"81git fetch --quiet --tags origin82git checkout --quiet --detach "$REF" 2>/dev/null || git checkout --quiet "$REF"83# Sur main, rester sur la branche (dépôt du nœud = origin/main).84if [[ "$REF" == "origin/main" ]]; then git checkout --quiet main && git merge --quiet --ff-only origin/main; fi8586pnpm install --frozen-lockfile --silent87rm -rf .next-building88NEXT_DIST_DIR=.next-building node_modules/.bin/next build >/dev/null89[[ -f .next-building/BUILD_ID ]] || { echo "ÉCHEC du build : rien n’a changé en service."; git checkout --quiet "$PREVIOUS"; exit 1; }9091rm -rf .next-previous92[[ -d .next ]] && mv .next .next-previous93mv .next-building .next94echo "$PREVIOUS" > .release-previous95pm2 restart "$PM2_NAME" --update-env >/dev/null9697if healthy; then98 echo "En service : $(git rev-parse --short HEAD) (précédente : ${PREVIOUS:0:7}, conservée dans .next-previous)."99else100 echo "Santé KO : retour automatique à ${PREVIOUS:0:7}."101 swap_back102 healthy || echo "ATTENTION : la version précédente ne répond pas non plus."103 exit 1104fi105