Files
remigracion/scripts/deploy.sh
danoloan10 a39827625e feat(deploy): CI/CD en Gitea Actions y entorno privado no indexable
Despliega el build de Vite en remigracion.jumpingcrab.com (mismo host que
localesp.es), siguiendo el patrón de prototipo-yb01-ef.localesp.es:
/opt/remigracion/dist + vhost nginx, sin unit de systemd por ser estático.

- .gitea/workflows/deploy.yml: npm ci + build + deploy en cada push a master
  (y workflow_dispatch). El runner self-hosted corre como root en el host,
  así que no hacen falta SSH ni secretos. Verifica en el propio job que el
  bundle lleve meta robots y robots.txt.
- scripts/deploy.sh: sincroniza dist/, etiqueta /opt/remigracion para SELinux
  (httpd_sys_content_t), instala el vhost si cambió (rollback si nginx -t
  falla), recarga nginx y comprueba 200 + X-Robots-Tag.
- deploy/nginx.conf: vhost HTTP-only (el DNS no permite Let's Encrypt) con
  X-Robots-Tag en todas las respuestas, 403 a user-agents de buscadores,
  agregadores SEO y crawlers de IA, robots.txt servido inline y fallback SPA.
- public/robots.txt y <meta name="robots"> en index.html: noindex, nofollow,
  noarchive, nosnippet, noimageindex, notranslate.
- README: sección de despliegue con rutas, comandos y capas anti-indexación.
2026-10-26 15:38:26 +01:00

194 lines
5.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# scripts/deploy.sh — Publica el build de Vite de Remigración en el host.
#
# Lo ejecuta el runner self-hosted de Gitea (usuario root) en cada push a
# master: ver .gitea/workflows/deploy.yml. También sirve a mano:
#
# npm ci && npm run build && ./scripts/deploy.sh
#
# Destino (mismo patrón que prototipo-yb01-ef.localesp.es):
# /opt/remigracion/dist + vhost nginx remigracion.jumpingcrab.com (HTTP)
# El vhost vive en el repo (deploy/nginx.conf) y se instala si cambia.
set -euo pipefail
APP_NAME="remigracion"
APP_HOST="remigracion.jumpingcrab.com"
DEPLOY_ROOT="/opt/remigracion"
VHOST_PATH="/etc/nginx/conf.d/remigracion.jumpingcrab.com.conf"
BACKUP_DIR="/var/backups/nginx"
usage() {
echo "Usage: $0" >&2
exit 2
}
log() {
printf '\033[1;34m[%s]\033[0m %s\n' "$APP_NAME" "$*"
}
fail() {
printf '\033[1;31m[%s:ERROR]\033[0m %s\n' "$APP_NAME" "$*" >&2
exit 1
}
host::require_root() {
if [ "$(id -u)" -ne 0 ]; then
fail "hay que ejecutarlo como root (el runner self-hosted ya lo hace)"
fi
}
host::require_tools() {
local tool
for tool in rsync curl nginx; do
if ! command -v "$tool" >/dev/null 2>&1; then
fail "falta la herramienta '$tool' en el host"
fi
done
}
artifact::require_build() {
local root="$1"
if [ ! -f "$root/dist/index.html" ]; then
fail "falta dist/index.html — ejecuta 'npm run build' antes del deploy"
fi
}
artifact::sync() {
local root="$1"
mkdir -p "$DEPLOY_ROOT/dist"
rsync -a --delete "$root/dist/" "$DEPLOY_ROOT/dist/"
log "build sincronizado en $DEPLOY_ROOT/dist"
}
# SELinux (AlmaLinux, Enforcing): nginx no puede leer /opt si el contexto no
# es httpd_sys_content_t. Se persiste la regla y se etiqueta el árbol.
selinux::label() {
local pattern
local rules
if ! command -v selinuxenabled >/dev/null 2>&1; then
return 0
fi
if ! selinuxenabled; then
return 0
fi
pattern="$DEPLOY_ROOT(/.*)?"
if command -v semanage >/dev/null 2>&1; then
# Se captura y se compara en memoria: 'grep -q' cierra el pipe antes de
# tiempo y con 'set -o pipefail' el SIGPIPE del emisor daria un falso
# negativo, volviendo a ejecutar semanage -a en cada deploy.
rules="$(semanage fcontext -l 2>/dev/null || true)"
if [[ "$rules" != *"$pattern"* ]]; then
semanage fcontext -a -t httpd_sys_content_t "$pattern"
fi
fi
if command -v restorecon >/dev/null 2>&1; then
restorecon -RF "$DEPLOY_ROOT" 2>/dev/null || true
elif command -v chcon >/dev/null 2>&1; then
chcon -R -t httpd_sys_content_t "$DEPLOY_ROOT"
fi
log "contexto SELinux aplicado a $DEPLOY_ROOT"
}
vhost::test() {
nginx -t >/dev/null 2>&1
}
# Instala el vhost del repo. Devuelve 1 si no había cambios (nada que recargar)
# y aborta restaurando el anterior si nginx rechaza la configuración.
vhost::install() {
local root="$1"
local source="$root/deploy/nginx.conf"
local backup
if [ ! -f "$source" ]; then
fail "falta $source en el repo"
fi
if [ -f "$VHOST_PATH" ] && cmp -s "$source" "$VHOST_PATH"; then
log "vhost sin cambios ($VHOST_PATH)"
return 1
fi
mkdir -p "$BACKUP_DIR"
if [ -f "$VHOST_PATH" ]; then
backup="$BACKUP_DIR/$(basename "$VHOST_PATH").$(date +%Y%m%d-%H%M%S)"
cp -a "$VHOST_PATH" "$backup"
fi
install -m 0644 "$source" "$VHOST_PATH"
if vhost::test; then
log "vhost instalado ($VHOST_PATH)"
return 0
fi
if [ -n "$backup" ] && [ -f "$backup" ]; then
mv -f "$backup" "$VHOST_PATH"
fail "nginx rechaza la configuración nueva: restaurado el vhost anterior"
fi
rm -f "$VHOST_PATH"
fail "nginx rechaza la configuración nueva: vhost eliminado"
}
nginx::reload() {
systemctl reload nginx
log "nginx recargado"
}
http::expect() {
local url="$1"
local expected="$2"
local attempt
local code
# nginx recarga en segundo plano: se reintenta antes de dar el deploy por fallido.
for attempt in $(seq 1 15); do
code="$(curl -s -o /dev/null -m 5 -w '%{http_code}' -H "Host: $APP_HOST" "$url" || true)"
if [ "$code" = "$expected" ]; then
return 0
fi
sleep 1
done
fail "esperaba $expected en $url y obtuve ${code:-sin respuesta}"
}
http::verify() {
local headers
http::expect "http://127.0.0.1/" "200"
http::expect "http://127.0.0.1/robots.txt" "200"
headers="$(curl -sI -m 10 -H "Host: $APP_HOST" "http://127.0.0.1/" | tr -d '\r')"
if ! grep -qi '^X-Robots-Tag:.*noindex' <<< "$headers"; then
fail "falta la cabecera X-Robots-Tag: noindex en la respuesta"
fi
log "verificado: HTTP 200 y X-Robots-Tag: noindex"
}
main() {
local root
root="$(cd "$(dirname "$0")/.." && pwd)"
host::require_root
host::require_tools
artifact::require_build "$root"
artifact::sync "$root"
selinux::label
if vhost::install "$root"; then
nginx::reload
fi
http::verify
log "LISTO: http://$APP_HOST/ (sin TLS, no indexable)"
}
main "$@"