// ============================================================================== // JENKINSFILE TEMPLATE - Control Plane // ============================================================================== // Proposito: Template base para pipelines de CI/CD // Uso: Copiar y adaptar para cada servicio // ============================================================================== pipeline { agent any environment { // Variables del servicio (personalizar) SERVICE_NAME = '{{SERVICE_NAME}}' PROJECT_NAME = '{{PROJECT_NAME}}' // Paths del Control Plane CONTROL_PLANE = "${env.WORKSPACE_ROOT}/control-plane" REGISTRIES = "${CONTROL_PLANE}/registries" VALIDATION_SCRIPTS = "${CONTROL_PLANE}/devtools/scripts/validation" // Docker DOCKER_REGISTRY = 'ghcr.io/{{ORG_NAME}}' DOCKER_IMAGE = "${DOCKER_REGISTRY}/${PROJECT_NAME}-${SERVICE_NAME}" } options { timeout(time: 30, unit: 'MINUTES') disableConcurrentBuilds() buildDiscarder(logRotator(numToKeepStr: '10')) } stages { // ====================================================================== // FASE 1: VALIDACION PRE-BUILD // ====================================================================== stage('Validate') { steps { echo "=== Validando contra registries ===" // Validar puertos sh """ ${VALIDATION_SCRIPTS}/validate-ports.sh || exit 1 """ // Validar service descriptor sh """ ${VALIDATION_SCRIPTS}/validate-service-descriptors.sh || exit 1 """ // Validar dominios sh """ ${VALIDATION_SCRIPTS}/validate-domains.sh || exit 1 """ } } // ====================================================================== // FASE 2: BUILD // ====================================================================== stage('Build') { steps { echo "=== Building ${SERVICE_NAME} ===" dir("${PROJECT_PATH}") { // Node.js projects sh """ npm ci npm run build npm run lint """ } } } // ====================================================================== // FASE 3: TEST // ====================================================================== stage('Test') { steps { echo "=== Running tests ===" dir("${PROJECT_PATH}") { sh """ npm run test:ci || true """ } } post { always { // Publicar resultados de tests junit allowEmptyResults: true, testResults: '**/junit.xml' } } } // ====================================================================== // FASE 4: DOCKER BUILD // ====================================================================== stage('Docker Build') { when { anyOf { branch 'main' branch 'develop' tag pattern: "v\\d+\\.\\d+\\.\\d+.*", comparator: "REGEXP" } } steps { echo "=== Building Docker image ===" dir("${PROJECT_PATH}") { sh """ docker build -t ${DOCKER_IMAGE}:${GIT_COMMIT_SHORT} . docker tag ${DOCKER_IMAGE}:${GIT_COMMIT_SHORT} ${DOCKER_IMAGE}:latest """ } } } // ====================================================================== // FASE 5: DEPLOY (solo en tags) // ====================================================================== stage('Deploy') { when { tag pattern: "v\\d+\\.\\d+\\.\\d+.*", comparator: "REGEXP" } steps { echo "=== Deploying ${SERVICE_NAME} ===" sh """ docker push ${DOCKER_IMAGE}:${GIT_COMMIT_SHORT} docker push ${DOCKER_IMAGE}:latest """ // Trigger deploy script sh """ echo "Deploy triggered for ${SERVICE_NAME}" """ } } } post { success { echo "Pipeline completed successfully" } failure { echo "Pipeline failed" // Notificar en caso de fallo } always { cleanWs() } } }