Files
django-core-base/deployments/Jenkinsfile
minguezsanzjuanjose 039349b5b1
All checks were successful
DEPLOY_MULTI_BRACH/pipeline/head This commit looks good
feat: integración final de Jenkins con inyección de .env segura
2026-04-14 21:30:19 +02:00

59 lines
2.2 KiB
Groovy

pipeline {
agent any
stages {
stage('Configurar Entorno') {
steps {
script {
// Seleccionamos ID de credencial y config según la rama
if (env.BRANCH_NAME == 'master') {
env.PROJECT_NAME = "django_master"
env.APP_CONTAINER_NAME = "django_app_master"
env.PORT = "8001"
env.DEBUG_MODE = "0"
env.ENV_CREDENTIAL_ID = "2" // Tu ID para master
} else {
env.PROJECT_NAME = "django_dev"
env.APP_CONTAINER_NAME = "django_app_dev"
env.PORT = "8000"
env.DEBUG_MODE = "1"
env.ENV_CREDENTIAL_ID = "1" // Tu ID para dev
}
}
}
}
stage('Fase Final: Containerización') {
when { anyOf { branch 'dev'; branch 'master' } }
steps {
// Bloque mágico: extrae el archivo .env de la bóveda de Jenkins
withCredentials([file(credentialsId: env.ENV_CREDENTIAL_ID, variable: 'SECRET_ENV')]) {
sh """
echo "Copiando configuración segura..."
cp \$SECRET_ENV deployments/.env
echo "🚀 DESPLEGANDO: ${env.APP_CONTAINER_NAME} en puerto ${env.PORT}"
export APP_CONTAINER_NAME=${env.APP_CONTAINER_NAME}
export PORT=${env.PORT}
export DEBUG_MODE=${env.DEBUG_MODE}
docker compose -p ${env.PROJECT_NAME} -f deployments/docker-compose.yml up -d --build web
"""
}
}
}
}
post {
success {
echo "✅ Despliegue completado con éxito."
// Limpieza preventiva: borramos el .env físico después del despliegue
sh "rm -f deployments/.env"
}
failure {
echo "❌ Error en el despliegue. Revisa los logs."
sh "rm -f deployments/.env"
}
}
}