From 2b0f9ec49d13cba5aeec0689621ba10c821af9dd Mon Sep 17 00:00:00 2001 From: josedario87 Date: Sun, 5 Oct 2025 02:24:38 -0600 Subject: [PATCH] Add CI/CD support for automated tunnel deployment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - setup-ci.sh: Script no interactivo que verifica archivos de config - Workflow actualizado para ejecutar setup-ci.sh antes del deploy - Los archivos credentials.json y .env persisten en el workspace del runner - Solo requiere setup.sh manual la primera vez en el servidor - Documentación actualizada con instrucciones para CI/CD --- .env.example | 4 +++ .gitea/workflows/deploy.yml | 17 ++++--------- README.md | 30 +++++++++++++++++----- setup-ci.sh | 50 +++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 18 deletions(-) create mode 100755 setup-ci.sh diff --git a/.env.example b/.env.example index 82f7432..9767e62 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,7 @@ # Cloudflare Tunnel ID # Obtén este ID cuando crees el tunnel con: cloudflared tunnel create TUNNEL_ID=your-tunnel-id-here + +# Cloudflare API Credentials (opcional, solo para CI/CD automatizado) +# CLOUDFLARE_ACCOUNT_ID=your-account-id +# CLOUDFLARE_API_TOKEN=your-api-token diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 9feded1..2923891 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -8,21 +8,14 @@ jobs: #───────────────── deploy ───────────────── deploy: runs-on: docker + env: + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} steps: - uses: actions/checkout@v3 - - name: Setup tunnel if needed - run: | - if [ ! -f credentials.json ] || [ ! -f .env ]; then - echo "⚠️ credentials.json o .env no encontrados" - echo " Ejecuta './setup.sh' manualmente en el servidor para configurar el tunnel" - echo "" - echo " O copia manualmente:" - echo " - credentials.json desde ~/.cloudflared/.json" - echo " - .env con TUNNEL_ID configurado" - exit 1 - fi - echo "✓ credentials.json y .env encontrados" + - name: Check and setup tunnel if needed + run: ./setup-ci.sh - name: Pull fresh cloudflared image run: docker compose pull diff --git a/README.md b/README.md index ee8524d..75ba07a 100644 --- a/README.md +++ b/README.md @@ -150,24 +150,42 @@ El contenedor se conecta a la red `principal` para poder comunicarse con Traefik Este repositorio incluye un workflow de Gitea Actions (`.gitea/workflows/deploy.yml`) que automáticamente despliega el tunnel cuando haces push a `main`. **¿Qué hace el workflow?** -1. Verifica que existan `credentials.json` y `.env` +1. Ejecuta `setup-ci.sh` para verificar que existan `credentials.json` y `.env` 2. Descarga la última imagen de `cloudflare/cloudflared` 3. Detiene el stack actual 4. Levanta el stack actualizado 5. Muestra los logs del tunnel -**Setup inicial en el servidor:** +### Setup inicial para CI/CD -En el servidor donde corre el gitea-runner, ejecuta el setup una sola vez: +**Primera vez en el servidor donde corre el gitea-runner:** ```bash -# El gitea-runner clonará el repo automáticamente -# Solo necesitas ejecutar setup.sh la primera vez +# 1. El runner clonará el repo automáticamente en su workspace +# 2. SSH al servidor y navega al workspace del runner cd /ruta/donde/runner/clona/el/repo + +# 3. Ejecuta setup.sh UNA SOLA VEZ (requiere login interactivo) ./setup.sh ``` -Una vez configurado, cada `git push` actualizará automáticamente el tunnel. +El script creará `credentials.json` y `.env` que **persisten** en el workspace del runner entre ejecuciones. + +**De ahí en adelante:** +- Cada `git push` ejecuta el workflow automáticamente +- `setup-ci.sh` verifica que los archivos existan +- Si existen, continúa con el deploy +- Si no existen, el workflow falla con instrucciones + +### (Opcional) Variables de entorno en Gitea Secrets + +Puedes configurar secrets en Gitea para el workflow (aunque actualmente no son necesarios con el enfoque de setup manual): + +``` +Settings > Secrets > Actions: +- CLOUDFLARE_ACCOUNT_ID: tu-account-id +- CLOUDFLARE_API_TOKEN: tu-api-token +``` **Nota**: `credentials.json` y `.env` se generan localmente y NO se commitean al repo (están en `.gitignore`), por seguridad. diff --git a/setup-ci.sh b/setup-ci.sh new file mode 100755 index 0000000..9c10959 --- /dev/null +++ b/setup-ci.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# Setup no interactivo para CI/CD (Gitea Actions) +# Usa Cloudflare API Token en vez de login interactivo + +set -e + +TUNNEL_NAME="nucleorio-tunnel" +DOMAIN="nucleoriofrio.com" +ACCOUNT_ID="${CLOUDFLARE_ACCOUNT_ID}" +API_TOKEN="${CLOUDFLARE_API_TOKEN}" + +echo "🤖 Cloudflare Tunnel CI Setup" +echo "==============================" +echo "" + +# Verificar variables de entorno +if [ -z "$ACCOUNT_ID" ]; then + echo "❌ CLOUDFLARE_ACCOUNT_ID no está configurado" + echo " Configúralo en Gitea: Settings > Secrets" + exit 1 +fi + +if [ -z "$API_TOKEN" ]; then + echo "❌ CLOUDFLARE_API_TOKEN no está configurado" + echo " Configúralo en Gitea: Settings > Secrets" + exit 1 +fi + +# Verificar si ya existen los archivos +if [ -f "credentials.json" ] && [ -f ".env" ]; then + echo "✓ credentials.json y .env ya existen" + TUNNEL_ID=$(jq -r '.TunnelID' credentials.json 2>/dev/null || echo "") + + if [ -n "$TUNNEL_ID" ]; then + echo "✓ TUNNEL_ID: $TUNNEL_ID" + echo "✓ Setup ya está completo, no se requiere acción" + exit 0 + fi +fi + +echo "⚠️ Archivos de configuración no encontrados" +echo "" +echo "Para CI/CD automatizado necesitas:" +echo "1. Ejecutar ./setup.sh manualmente UNA VEZ en el servidor" +echo "2. Esto genera credentials.json y .env" +echo "3. Estos archivos persisten entre deploys del runner" +echo "" +echo "El workflow automáticamente los usará en futuros deploys." +echo "" +exit 1