// ============================================================================= // TRADING PLATFORM - Jenkins Pipeline // ============================================================================= // Repositorio: 72.60.226.4:3000/rckrdmrd/trading-platform.git // Servidor: 72.60.226.4 // Dominios: trading.isem.dev / api.trading.isem.dev // ============================================================================= pipeline { agent any environment { PROJECT_NAME = 'trading-platform' DOCKER_REGISTRY = '72.60.226.4:5000' DEPLOY_SERVER = '72.60.226.4' DEPLOY_USER = 'deploy' DEPLOY_PATH = '/opt/apps/trading-platform' // Puertos FRONTEND_PORT = '3080' BACKEND_PORT = '3081' // URLs FRONTEND_URL = 'https://trading.isem.dev' BACKEND_URL = 'https://api.trading.isem.dev' // Version VERSION = "${env.BUILD_NUMBER}" GIT_COMMIT_SHORT = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim() } options { timeout(time: 45, unit: 'MINUTES') disableConcurrentBuilds() buildDiscarder(logRotator(numToKeepStr: '10')) timestamps() } stages { stage('Checkout') { steps { checkout scm script { env.GIT_BRANCH = sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim() currentBuild.displayName = "#${BUILD_NUMBER} - ${GIT_COMMIT_SHORT}" } } } stage('Install Dependencies') { parallel { stage('Backend') { steps { dir('apps/backend') { sh 'npm ci --prefer-offline' } } } stage('Frontend') { steps { dir('apps/frontend') { sh 'npm ci --prefer-offline' } } } } } stage('Lint') { parallel { stage('Backend Lint') { steps { dir('apps/backend') { sh 'npm run lint' } } } stage('Frontend Lint') { steps { dir('apps/frontend') { sh 'npm run lint' } } } } } stage('Type Check') { parallel { stage('Backend Type Check') { steps { dir('apps/backend') { sh 'npx tsc --noEmit' } } } stage('Frontend Type Check') { steps { dir('apps/frontend') { sh 'npx tsc --noEmit' } } } } } stage('Test') { parallel { stage('Backend Test') { steps { dir('apps/backend') { sh 'npm run test' } } } stage('Frontend Test') { steps { dir('apps/frontend') { sh 'npm run test' } } } } } stage('Security Scan') { parallel { stage('Backend Security Scan') { steps { dir('apps/backend') { sh 'npm audit --audit-level=high' } } } stage('Frontend Security Scan') { steps { dir('apps/frontend') { sh 'npm audit --audit-level=high' } } } } } stage('Build') { parallel { stage('Build Backend') { steps { dir('apps/backend') { sh 'npm run build' } } } stage('Build Frontend') { steps { dir('apps/frontend') { sh 'npm run build' } } } } } stage('Docker Build & Push') { when { anyOf { branch 'main' branch 'develop' } } steps { script { def services = ['backend', 'frontend'] services.each { service -> dir("apps/${service}") { def imageName = "${DOCKER_REGISTRY}/${PROJECT_NAME}-${service}" sh """ docker build -t ${imageName}:${VERSION} . docker tag ${imageName}:${VERSION} ${imageName}:latest docker push ${imageName}:${VERSION} docker push ${imageName}:latest """ } } } } } stage('Deploy to Staging') { when { branch 'develop' } steps { script { deployToEnvironment('staging') } } } stage('Deploy to Production') { when { branch 'main' } steps { input message: '¿Desplegar a Producción?', ok: 'Desplegar' script { deployToEnvironment('prod') } } } stage('Health Check') { when { anyOf { branch 'main' branch 'develop' } } steps { script { echo "Performing health checks..." // Backend health check retry(5) { sleep(time: 15, unit: 'SECONDS') sh "curl -f ${BACKEND_URL}/health || exit 1" } echo "✓ Backend /health endpoint is healthy" // Backend API readiness check retry(3) { sleep(time: 5, unit: 'SECONDS') sh "curl -f ${BACKEND_URL}/api/status || exit 1" } echo "✓ Backend /api/status endpoint is healthy" // Frontend health check retry(3) { sleep(time: 5, unit: 'SECONDS') sh "curl -f -I ${FRONTEND_URL} | grep -q '200\\|301\\|302' || exit 1" } echo "✓ Frontend is accessible" echo "All health checks passed successfully!" } } } } post { success { echo "✅ Trading Platform deployed successfully!" } failure { echo "❌ Trading Platform deployment failed!" } always { cleanWs() } } } def deployToEnvironment(String env) { sshagent(['deploy-ssh-key']) { sh """ ssh -o StrictHostKeyChecking=no ${DEPLOY_USER}@${DEPLOY_SERVER} ' cd ${DEPLOY_PATH} docker-compose -f docker/docker-compose.${env}.yml pull docker-compose -f docker/docker-compose.${env}.yml down --remove-orphans docker-compose -f docker/docker-compose.${env}.yml up -d docker system prune -f ' """ } }