Merge pull request 'feat: integración final de Jenkins con inyección de .env segura' (#18) from pre-dev into dev
All checks were successful
DEPLOY_MULTI_BRACH/pipeline/head This commit looks good
All checks were successful
DEPLOY_MULTI_BRACH/pipeline/head This commit looks good
Reviewed-on: #18
This commit was merged in pull request #18.
This commit is contained in:
10
.env
10
.env
@@ -1,10 +0,0 @@
|
|||||||
# Seguridad
|
|
||||||
DEBUG=True
|
|
||||||
SECRET_KEY=una-clave-muy-secreta-y-larga-123456
|
|
||||||
|
|
||||||
# Base de Datos (Conectando al PostgreSQL que instalamos)
|
|
||||||
DB_NAME=django_test
|
|
||||||
DB_USER=django_user
|
|
||||||
DB_PASSWORD=django_password
|
|
||||||
DB_HOST=db
|
|
||||||
DB_PORT=5432
|
|
||||||
10
.env.example
10
.env.example
@@ -1,10 +0,0 @@
|
|||||||
# Seguridad
|
|
||||||
DEBUG=True
|
|
||||||
SECRET_KEY=una-clave-muy-secreta-y-larga-123456
|
|
||||||
|
|
||||||
# Base de Datos (Conectando al PostgreSQL que instalamos)
|
|
||||||
DB_NAME=gitea
|
|
||||||
DB_USER=gitea
|
|
||||||
DB_PASSWORD=gitea
|
|
||||||
DB_HOST=gitea-db
|
|
||||||
DB_PORT=5432
|
|
||||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -11,3 +11,7 @@ postgres_data/
|
|||||||
local_postgres_data/
|
local_postgres_data/
|
||||||
*.pyc
|
*.pyc
|
||||||
apps/backend_admin/migrations/0001_initial.py
|
apps/backend_admin/migrations/0001_initial.py
|
||||||
|
# Bloquear todos los .env en cualquier carpeta
|
||||||
|
.env
|
||||||
|
**/core/.env
|
||||||
|
**/deployments/.env
|
||||||
17
apps/backend_admin/migrations/0002_alter_log_options.py
Normal file
17
apps/backend_admin/migrations/0002_alter_log_options.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# Generated by Django 5.0.3 on 2026-04-14 19:11
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('backend_admin', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterModelOptions(
|
||||||
|
name='log',
|
||||||
|
options={'managed': False},
|
||||||
|
),
|
||||||
|
]
|
||||||
3
core/.env.example
Normal file
3
core/.env.example
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# core/.env
|
||||||
|
APP_CUSTOM_SETTING="Este es un valor privado de la app"
|
||||||
|
EXTERNAL_SERVICE_API_KEY="sk_test_12345"
|
||||||
@@ -3,35 +3,30 @@ import os
|
|||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
# Cargar variables desde el archivo .env
|
# 1. RUTA DEL SETTINGS Y CARGA DEL .ENV
|
||||||
load_dotenv()
|
# Obtenemos la ruta de la carpeta donde está este archivo (core/)
|
||||||
|
CURRENT_DIR = Path(__file__).resolve().parent
|
||||||
|
BASE_DIR = CURRENT_DIR.parent
|
||||||
|
|
||||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
# Cargamos el .env específico de esta carpeta (core/.env)
|
||||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
load_dotenv(dotenv_path=CURRENT_DIR / '.env')
|
||||||
|
|
||||||
# SECURITY WARNING: keep the secret key used in production secret!
|
# SECURITY WARNING: keep the secret key used in production secret!
|
||||||
SECRET_KEY = os.getenv('SECRET_KEY', 'django-insecure-change-me-for-production')
|
SECRET_KEY = os.getenv('SECRET_KEY', 'django-insecure-default-key-change-it')
|
||||||
|
|
||||||
# SECURITY WARNING: don't run with debug turned on in production!
|
# SECURITY WARNING: don't run with debug turned on in production!
|
||||||
DEBUG = os.getenv('DEBUG', 'True').lower() == 'true'
|
# Mejoramos el parseo de DEBUG para que no falle si viene como string
|
||||||
|
DEBUG = os.getenv('DEBUG', 'True').lower() in ('true', '1', 't')
|
||||||
# Constants from .env
|
|
||||||
DB_NAME = os.getenv('DB_NAME')
|
|
||||||
DB_USER = os.getenv('DB_USER')
|
|
||||||
DB_PASSWORD = os.getenv('DB_PASSWORD')
|
|
||||||
DB_HOST = os.getenv('DB_HOST')
|
|
||||||
DB_PORT = os.getenv('DB_PORT')
|
|
||||||
|
|
||||||
|
# 2. ALLOWED HOSTS
|
||||||
|
# Limpiamos y centralizamos los hosts permitidos
|
||||||
ALLOWED_HOSTS = [
|
ALLOWED_HOSTS = [
|
||||||
'v-encore-lab.com',
|
'v-encore-lab.com',
|
||||||
'dev.v-encore-lab.com',
|
'dev.v-encore-lab.com',
|
||||||
'185.187.169.109', # Añade la IP aquí
|
'185.187.169.109',
|
||||||
'localhost',
|
'localhost',
|
||||||
'127.0.0.1',
|
'127.0.0.1',
|
||||||
'django_app_dev',
|
os.getenv('APP_CONTAINER_NAME', 'django_app_dev'), # Dinámico para Docker
|
||||||
'django_app_master',
|
|
||||||
'rest_framework',
|
|
||||||
'rest_framework.authtoken'
|
|
||||||
]
|
]
|
||||||
|
|
||||||
# Application definition
|
# Application definition
|
||||||
@@ -42,12 +37,16 @@ INSTALLED_APPS = [
|
|||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
|
|
||||||
|
# Plugins
|
||||||
'rest_framework',
|
'rest_framework',
|
||||||
'rest_framework.authtoken',
|
'rest_framework.authtoken',
|
||||||
|
'corsheaders',
|
||||||
|
|
||||||
|
# Tus Apps (Asegúrate de que el path sea correcto)
|
||||||
'apps.promociones',
|
'apps.promociones',
|
||||||
'apps.backend_admin',
|
'apps.backend_admin',
|
||||||
'apps.common',
|
'apps.common',
|
||||||
'corsheaders',
|
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
@@ -81,15 +80,16 @@ TEMPLATES = [
|
|||||||
|
|
||||||
WSGI_APPLICATION = 'core.wsgi.application'
|
WSGI_APPLICATION = 'core.wsgi.application'
|
||||||
|
|
||||||
# Database
|
# 3. DATABASE
|
||||||
|
# Extraemos con fallback por si el .env falla
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
'default': {
|
'default': {
|
||||||
'ENGINE': 'django.db.backends.postgresql',
|
'ENGINE': 'django.db.backends.postgresql',
|
||||||
'NAME': DB_NAME,
|
'NAME': os.getenv('DB_NAME', 'postgres'),
|
||||||
'USER': DB_USER,
|
'USER': os.getenv('DB_USER', 'postgres'),
|
||||||
'PASSWORD': DB_PASSWORD,
|
'PASSWORD': os.getenv('DB_PASSWORD', ''),
|
||||||
'HOST': DB_HOST,
|
'HOST': os.getenv('DB_HOST', 'localhost'),
|
||||||
'PORT': DB_PORT,
|
'PORT': os.getenv('DB_PORT', '5432'),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,7 +99,7 @@ TIME_ZONE = 'Europe/Madrid'
|
|||||||
USE_I18N = True
|
USE_I18N = True
|
||||||
USE_TZ = True
|
USE_TZ = True
|
||||||
|
|
||||||
# Static files (CSS, JavaScript, Images)
|
# Static files
|
||||||
STATIC_URL = '/static/'
|
STATIC_URL = '/static/'
|
||||||
STATIC_ROOT = BASE_DIR / 'staticfiles'
|
STATIC_ROOT = BASE_DIR / 'staticfiles'
|
||||||
|
|
||||||
@@ -120,27 +120,43 @@ SIMPLE_JWT = {
|
|||||||
"REFRESH_TOKEN_LIFETIME": timedelta(days=1),
|
"REFRESH_TOKEN_LIFETIME": timedelta(days=1),
|
||||||
}
|
}
|
||||||
|
|
||||||
# CORS
|
# CORS & CSRF
|
||||||
CORS_ALLOW_ALL_ORIGINS = True
|
CORS_ALLOW_ALL_ORIGINS = DEBUG # Solo permitir todo en modo DEBUG
|
||||||
CSRF_TRUSTED_ORIGINS = [
|
CSRF_TRUSTED_ORIGINS = [
|
||||||
'chrome-extension://amknoiejhlmhancpahfcfcfhllgkpbld',
|
'https://v-encore-lab.com',
|
||||||
'http://localhost:8000',
|
'http://localhost:8000',
|
||||||
'http://127.0.0.1:8000',
|
'http://127.0.0.1:8000',
|
||||||
]
|
]
|
||||||
|
|
||||||
# Logging
|
# Logging simplificado
|
||||||
LOGGING = {
|
LOGGING = {
|
||||||
'version': 1,
|
'version': 1,
|
||||||
'disable_existing_loggers': False,
|
'disable_existing_loggers': False,
|
||||||
|
'formatters': {
|
||||||
|
'verbose': {
|
||||||
|
'format': '{levelname} {asctime} {module} {message}',
|
||||||
|
'style': '{',
|
||||||
|
},
|
||||||
|
},
|
||||||
'handlers': {
|
'handlers': {
|
||||||
'console': {
|
'console': {
|
||||||
'class': 'logging.StreamHandler',
|
'class': 'logging.StreamHandler',
|
||||||
|
'formatter': 'verbose',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
'loggers': {
|
'loggers': {
|
||||||
'': {
|
'': {
|
||||||
'handlers': ['console'],
|
'handlers': ['console'],
|
||||||
'level': 'INFO',
|
'level': os.getenv('LOG_LEVEL', 'INFO'),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# --- CONFIGURACIONES PERSONALIZADAS DE LA APP ---
|
||||||
|
|
||||||
|
# Leemos la variable del .env (cargado previamente con load_dotenv)
|
||||||
|
# Ponemos un valor por defecto por si se nos olvida ponerlo en el .env
|
||||||
|
APP_CUSTOM_SETTING = os.getenv('APP_CUSTOM_SETTING', 'valor_por_defecto_seguro')
|
||||||
|
|
||||||
|
# Ejemplo de otra variable de API
|
||||||
|
EXTERNAL_SERVICE_API_KEY = os.getenv('EXTERNAL_SERVICE_API_KEY', None)
|
||||||
BIN
db.sqlite3
BIN
db.sqlite3
Binary file not shown.
34
deployments/.env.example
Normal file
34
deployments/.env.example
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
# =================================================================
|
||||||
|
# 🏗️ INFRAESTRUCTURA (Docker & Jenkins)
|
||||||
|
# =================================================================
|
||||||
|
# Nombres que tomarán los contenedores en Docker
|
||||||
|
APP_CONTAINER_NAME=
|
||||||
|
DB_CONTAINER_NAME=
|
||||||
|
|
||||||
|
# Nombre del proyecto para Docker Compose (usado en Jenkins)
|
||||||
|
PROJECT_NAME=
|
||||||
|
|
||||||
|
# Puertos que se abrirán al exterior (Host)
|
||||||
|
PORT=
|
||||||
|
DATABASE_EXPOSE_PORT=
|
||||||
|
|
||||||
|
# =================================================================
|
||||||
|
# 🐍 CONFIGURACIÓN DE DJANGO
|
||||||
|
# =================================================================
|
||||||
|
# True para desarrollo, False para producción
|
||||||
|
DEBUG=
|
||||||
|
# Genera una clave segura: https://djecrety.ir/
|
||||||
|
SECRET_KEY=
|
||||||
|
# Lista separada por comas: localhost,127.0.0.1,tudominio.com
|
||||||
|
ALLOWED_HOSTS=
|
||||||
|
|
||||||
|
# =================================================================
|
||||||
|
# 🗄️ BASE DE DATOS (PostgreSQL)
|
||||||
|
# =================================================================
|
||||||
|
DB_NAME=
|
||||||
|
DB_USER=
|
||||||
|
DB_PASSWORD=
|
||||||
|
|
||||||
|
# El HOST debe ser "db" cuando se usa Docker Compose
|
||||||
|
DB_HOST=
|
||||||
|
DB_PORT=
|
||||||
@@ -4,27 +4,31 @@ FROM python:3.12-slim
|
|||||||
# Evitar que Python genere archivos .pyc y que el buffer se sature
|
# Evitar que Python genere archivos .pyc y que el buffer se sature
|
||||||
ENV PYTHONDONTWRITEBYTECODE 1
|
ENV PYTHONDONTWRITEBYTECODE 1
|
||||||
ENV PYTHONUNBUFFERED 1
|
ENV PYTHONUNBUFFERED 1
|
||||||
# Definimos la zona horaria como variable de entorno
|
|
||||||
ENV TZ=Europe/Madrid
|
ENV TZ=Europe/Madrid
|
||||||
|
|
||||||
# Directorio de trabajo
|
# Directorio de trabajo
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Instalar dependencias del sistema necesarias
|
# Instalar dependencias del sistema necesarias y limpiar caché en un solo paso
|
||||||
RUN apt-get update && apt-get install -y \
|
RUN apt-get update && apt-get install -y \
|
||||||
libpq-dev \
|
libpq-dev \
|
||||||
gcc \
|
gcc \
|
||||||
gettext \
|
gettext \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Instalar dependencias de Python
|
# Copiar dependencias (Ajustado: asumiendo que están en deployments/)
|
||||||
COPY requirements.txt /app/
|
COPY deployments/requirements.txt /app/
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
# Copiar el resto del código
|
# Copiar el resto del código del proyecto
|
||||||
|
# El contexto de docker-compose suele ser la raíz, por lo que copiamos todo a /app
|
||||||
COPY . /app/
|
COPY . /app/
|
||||||
|
|
||||||
# Exponer el puerto de Django
|
# Exponer el puerto de Django
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
|
|
||||||
# Comando por defecto para arrancar (usaremos manage.py en dev y gunicorn en prod)
|
# Script de entrada (lo crearemos en el siguiente paso)
|
||||||
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
|
COPY deployments/entrypoint.sh /entrypoint.sh
|
||||||
|
RUN chmod +x /entrypoint.sh
|
||||||
|
|
||||||
|
ENTRYPOINT ["/entrypoint.sh"]
|
||||||
44
deployments/Jenkinsfile
vendored
44
deployments/Jenkinsfile
vendored
@@ -5,37 +5,55 @@ pipeline {
|
|||||||
stage('Configurar Entorno') {
|
stage('Configurar Entorno') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
|
// Seleccionamos ID de credencial y config según la rama
|
||||||
if (env.BRANCH_NAME == 'master') {
|
if (env.BRANCH_NAME == 'master') {
|
||||||
env.PROJECT_NAME = "django_master"
|
env.PROJECT_NAME = "django_master"
|
||||||
env.CONTAINER_NAME = "django_app_master"
|
env.APP_CONTAINER_NAME = "django_app_master"
|
||||||
env.PORT = "8001"
|
env.PORT = "8001"
|
||||||
env.DEBUG_MODE = "0"
|
env.DEBUG_MODE = "0"
|
||||||
|
env.ENV_CREDENTIAL_ID = "2" // Tu ID para master
|
||||||
} else {
|
} else {
|
||||||
env.PROJECT_NAME = "django_dev"
|
env.PROJECT_NAME = "django_dev"
|
||||||
env.CONTAINER_NAME = "django_app_dev"
|
env.APP_CONTAINER_NAME = "django_app_dev"
|
||||||
env.PORT = "8000"
|
env.PORT = "8000"
|
||||||
env.DEBUG_MODE = "1"
|
env.DEBUG_MODE = "1"
|
||||||
|
env.ENV_CREDENTIAL_ID = "1" // Tu ID para dev
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Despliegue') {
|
stage('Fase Final: Containerización') {
|
||||||
when { anyOf { branch 'dev'; branch 'master' } }
|
when { anyOf { branch 'dev'; branch 'master' } }
|
||||||
steps {
|
steps {
|
||||||
echo "DESPLEGANDO: ${env.CONTAINER_NAME} en el puerto ${env.PORT}"
|
// 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
|
||||||
|
|
||||||
// CAMBIAMOS docker-compose (guion) por docker compose (espacio)
|
echo "🚀 DESPLEGANDO: ${env.APP_CONTAINER_NAME} en puerto ${env.PORT}"
|
||||||
sh """
|
|
||||||
CONTAINER_NAME=${env.CONTAINER_NAME} \
|
|
||||||
PORT=${env.PORT} \
|
|
||||||
DEBUG_MODE=${env.DEBUG_MODE} \
|
|
||||||
docker compose -p ${env.PROJECT_NAME} -f deployments/docker-compose.yml up -d --build web
|
|
||||||
"""
|
|
||||||
|
|
||||||
echo "Ejecutando migraciones en ${env.CONTAINER_NAME}..."
|
export APP_CONTAINER_NAME=${env.APP_CONTAINER_NAME}
|
||||||
sh "docker exec ${env.CONTAINER_NAME} python manage.py migrate --noinput"
|
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"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,36 +1,42 @@
|
|||||||
services:
|
services:
|
||||||
db:
|
db:
|
||||||
image: postgres:15
|
image: postgres:15
|
||||||
container_name: django_db_local
|
container_name: ${DB_CONTAINER_NAME:-django_db_local}
|
||||||
# Cargamos el archivo directamente
|
restart: always
|
||||||
env_file:
|
# Solo usamos el .env de esta carpeta
|
||||||
- ../.env
|
env_file: .env
|
||||||
environment:
|
environment:
|
||||||
# POSTGRES_DB espera estas variables exactas,
|
|
||||||
# así que las mapeamos a lo que tienes en tu .env
|
|
||||||
- POSTGRES_DB=${DB_NAME}
|
- POSTGRES_DB=${DB_NAME}
|
||||||
- POSTGRES_USER=${DB_USER}
|
- POSTGRES_USER=${DB_USER}
|
||||||
- POSTGRES_PASSWORD=${DB_PASSWORD}
|
- POSTGRES_PASSWORD=${DB_PASSWORD}
|
||||||
ports:
|
ports:
|
||||||
- "5432:5432"
|
- "${DATABASE_EXPOSE_PORT:-5432}:5432"
|
||||||
volumes:
|
volumes:
|
||||||
- local_postgres_data:/var/lib/postgresql/data
|
- local_postgres_data:/var/lib/postgresql/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready -U ${DB_USER} -d ${DB_NAME}"]
|
||||||
|
interval: 5s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
|
||||||
web:
|
web:
|
||||||
build: .
|
build:
|
||||||
container_name: django_app_dev
|
context: ..
|
||||||
|
dockerfile: deployments/Dockerfile
|
||||||
|
container_name: ${APP_CONTAINER_NAME:-django_app_dev}
|
||||||
|
restart: always
|
||||||
volumes:
|
volumes:
|
||||||
- ..:/app
|
- ..:/app
|
||||||
# Cargamos el archivo directamente aquí también
|
ports:
|
||||||
env_file:
|
- "${PORT:-8000}:8000"
|
||||||
- ../.env
|
# Inyectamos el .env local al contenedor web
|
||||||
|
env_file: .env
|
||||||
environment:
|
environment:
|
||||||
- DB_HOST=db
|
- DB_HOST=db
|
||||||
- DB_PORT=5432
|
- DB_PORT=5432
|
||||||
ports:
|
|
||||||
- "8000:8000"
|
|
||||||
depends_on:
|
depends_on:
|
||||||
- db
|
db:
|
||||||
|
condition: service_healthy
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
local_postgres_data:
|
local_postgres_data:
|
||||||
15
deployments/entrypoint.sh
Normal file
15
deployments/entrypoint.sh
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Salir inmediatamente si un comando falla
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "--> Ejecutando migraciones..."
|
||||||
|
python manage.py makemigrations --noinput
|
||||||
|
python manage.py migrate --noinput
|
||||||
|
|
||||||
|
# En el futuro, aquí podrías añadir:
|
||||||
|
# python manage.py collectstatic --noinput
|
||||||
|
|
||||||
|
echo "--> Arrancando el servidor Django..."
|
||||||
|
# Usamos exec para que Django sea el proceso principal (PID 1) y reciba señales de Docker
|
||||||
|
exec python manage.py runserver 0.0.0.0:8000
|
||||||
Reference in New Issue
Block a user