name: CI Pipeline - Quality & Build on: push: branches: - develop - main - master pull_request: branches: - develop - main - master jobs: validate-frontend: name: Build & Validate Frontend runs-on: ubuntu-latest steps: - name: Checkout Code run: | git config --global --add safe.directory "*" git init git remote add origin https://git.bryan-bano.com/Bryan.bano/PORTFOLIO.git 2>/dev/null || git remote set-url origin https://git.bryan-bano.com/Bryan.bano/PORTFOLIO.git git fetch --depth 1 origin ${{ github.sha }} git checkout --force FETCH_HEAD - name: Validate Pull Request Metadata if: github.event_name == 'pull_request' env: PR_HEAD_REF: ${{ github.head_ref }} PR_BASE_REF: ${{ github.base_ref }} PR_TITLE: ${{ github.event.pull_request.title }} PR_BODY: ${{ github.event.pull_request.body }} run: | echo "==> Validando Pull Request: '$PR_TITLE'" echo " Rama origen: $PR_HEAD_REF -> Rama destino: $PR_BASE_REF" if [[ "$PR_BASE_REF" == "develop" ]]; then if ! [[ "$PR_HEAD_REF" =~ ^(feature|feat|chore|fix|bugfix|refactor|docs|test|perf)/ ]]; then echo "❌ Error de Rama: Pull Requests hacia 'develop' deben provenir de una rama específica (feature/*, chore/*, fix/*, refactor/*, etc.)." echo " Rama recibida: '$PR_HEAD_REF'" exit 1 fi elif [[ "$PR_BASE_REF" == "main" || "$PR_BASE_REF" == "master" ]]; then if ! [[ "$PR_HEAD_REF" =~ ^(release|hotfix)/ || "$PR_HEAD_REF" == "develop" ]]; then echo "❌ Error de Rama: Pull Requests hacia producción ('main') deben provenir de 'develop', 'release/*' o 'hotfix/*'." echo " Rama recibida: '$PR_HEAD_REF'" exit 1 fi fi TITLE_REGEX="^(feat|fix|chore|docs|style|refactor|perf|test|build|ci|revert)(\([a-zA-Z0-9_\-\./]+\))?!?: .+" if ! [[ "$PR_TITLE" =~ $TITLE_REGEX ]]; then echo "❌ Error de Título: El título del PR debe seguir Conventional Commits." echo " Formato: (<ámbito opcional>): " echo " Ejemplos: feat(auth): add login form, fix: resolve token refresh, chore(ci): update runner" echo " Título recibido: '$PR_TITLE'" exit 1 fi CLEAN_BODY=$(echo "$PR_BODY" | tr -d '[:space:]') if [[ ${#CLEAN_BODY} -lt 15 ]]; then echo "❌ Error de Descripción: La descripción del Pull Request no puede estar vacía." echo " Por favor explica brevemente qué cambios incluye este PR para facilitar la revisión." exit 1 fi echo "✅ Validaciones de Pull Request aprobadas." - name: Scan for Leaked Secrets & Credentials run: | ARCH=$(uname -m) case "$ARCH" in x86_64) GITLEAKS_ARCH="x64" ;; aarch64|arm64) GITLEAKS_ARCH="arm64" ;; *) GITLEAKS_ARCH="x64" ;; esac mkdir -p /tmp/bin if ! command -v gitleaks &> /dev/null; then echo "Descargando Gitleaks para $ARCH ($GITLEAKS_ARCH)..." curl -sSL "https://github.com/gitleaks/gitleaks/releases/download/v8.18.4/gitleaks_8.18.4_linux_${GITLEAKS_ARCH}.tar.gz" | tar -xz -C /tmp/bin gitleaks export PATH="/tmp/bin:$PATH" fi echo "Ejecutando escaneo de credenciales y secretos..." gitleaks detect --source . --verbose --no-git - name: Install Frontend Dependencies run: | cd src/frontend npm ci - name: Build Angular 20 Production Bundle run: | cd src/frontend npm run build -- --configuration production validate-backend: name: Build & Test Backend (.NET) runs-on: dotnet-runner steps: - name: Checkout Code run: | command -v git >/dev/null || (apt-get update && apt-get install -y --no-install-recommends git) git config --global --add safe.directory "*" git init git remote add origin https://git.bryan-bano.com/Bryan.bano/PORTFOLIO.git 2>/dev/null || git remote set-url origin https://git.bryan-bano.com/Bryan.bano/PORTFOLIO.git git fetch --depth 1 origin ${{ github.sha }} git checkout --force FETCH_HEAD - name: Build & Run Backend Unit Tests run: | dotnet test src/backend/Portfolio.sln -c Release --verbosity normal - name: Validate Backend Production Publish run: | dotnet publish src/backend/src/Portfolio.WebApi/Portfolio.WebApi.csproj -c Release -o /tmp/publish deploy-backend: name: Continuous Deployment to Production VPS needs: [validate-frontend, validate-backend] if: github.event_name == 'push' && (github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/main') runs-on: ubuntu-latest steps: - name: Deploy Docker Stack on VPS via SSH env: SSH_KEY: ${{ secrets.VPS_SSH_KEY }} VPS_HOST: ${{ secrets.VPS_HOST }} VPS_USER: ${{ secrets.VPS_USER }} VPS_PORT: ${{ secrets.VPS_PORT }} VPS_DEPLOY_PATH: ${{ secrets.VPS_DEPLOY_PATH }} run: | if [ -z "$SSH_KEY" ] || [ -z "$VPS_HOST" ]; then echo "ℹ️ Modo Standby de Despliegue Automático:" echo " Los secretos VPS_SSH_KEY o VPS_HOST aún no están configurados en Forgejo (Settings -> Actions -> Secrets)." echo " Para habilitar despliegue desatendido en cada push, añade estos secretos." echo " Alternativamente, puedes ejecutar en tu VPS: cd /opt/portfolio/deploy && ./deploy.sh" exit 0 fi echo "==> Iniciando conexión SSH segura con el VPS ($VPS_HOST)..." mkdir -p ~/.ssh echo "$SSH_KEY" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa ssh -o StrictHostKeyChecking=no -p "${VPS_PORT:-22}" "${VPS_USER:-root}@${VPS_HOST}" "bash -s" << 'EOF' set -e echo "==> Conectado al VPS. Actualizando repositorio..." TARGET_DIR="${VPS_DEPLOY_PATH:-/opt/portfolio}" if [ ! -d "$TARGET_DIR" ]; then TARGET_DIR="$HOME/portfolio" fi cd "$TARGET_DIR" git fetch origin develop git checkout develop git pull origin develop echo "==> Reconstruyendo contenedor de la API en producción..." cd deploy docker compose -f docker-compose.prod.yml up -d --build portfolio-api echo "✅ Despliegue continuo completado con éxito." EOF