From a66da4a59f264b61ea99dbdfda9a83cd40575ccf Mon Sep 17 00:00:00 2001 From: denshooter Date: Thu, 15 Jan 2026 18:15:18 +0100 Subject: [PATCH] chore: Enhance Gitea deployment workflow for database and Redis management - Added logic to start PostgreSQL and Redis containers if they are not already running. - Implemented checks to ensure the existence of necessary Docker networks. - Updated environment variables for database and Redis connections. - Improved feedback messages for better clarity during the deployment process. --- .gitea/workflows/dev-deploy.yml | 59 +++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/.gitea/workflows/dev-deploy.yml b/.gitea/workflows/dev-deploy.yml index e3822be..3a28549 100644 --- a/.gitea/workflows/dev-deploy.yml +++ b/.gitea/workflows/dev-deploy.yml @@ -57,10 +57,45 @@ jobs: # Check for existing container (running or stopped) EXISTING_CONTAINER=$(docker ps -aq -f name=$CONTAINER_NAME || echo "") + # Start DB and Redis if not running + echo "🗄️ Starting database and Redis..." + COMPOSE_FILE="docker-compose.dev.minimal.yml" + + # Check if DB container exists + if ! docker ps -a --format "{{.Names}}" | grep -q "portfolio_postgres_dev"; then + echo "📦 Starting PostgreSQL container..." + docker compose -f $COMPOSE_FILE up -d postgres + else + echo "✅ PostgreSQL container exists, ensuring it's running..." + docker start portfolio_postgres_dev 2>/dev/null || docker compose -f $COMPOSE_FILE up -d postgres + fi + + # Check if Redis container exists + if ! docker ps -a --format "{{.Names}}" | grep -q "portfolio_redis_dev"; then + echo "📦 Starting Redis container..." + docker compose -f $COMPOSE_FILE up -d redis + else + echo "✅ Redis container exists, ensuring it's running..." + docker start portfolio_redis_dev 2>/dev/null || docker compose -f $COMPOSE_FILE up -d redis + fi + + # Wait for DB to be ready + echo "⏳ Waiting for database to be ready..." + for i in {1..30}; do + if docker exec portfolio_postgres_dev pg_isready -U portfolio_user -d portfolio_dev >/dev/null 2>&1; then + echo "✅ Database is ready!" + break + fi + echo "⏳ Waiting for database... ($i/30)" + sleep 1 + done + # Export environment variables export NODE_ENV=production export LOG_LEVEL=${LOG_LEVEL:-debug} export NEXT_PUBLIC_BASE_URL=${NEXT_PUBLIC_BASE_URL_DEV:-https://dev.dk0.dev} + export DATABASE_URL="postgresql://portfolio_user:portfolio_dev_pass@portfolio_postgres_dev:5432/portfolio_dev?schema=public" + export REDIS_URL="redis://portfolio_redis_dev:6379" export MY_EMAIL=${MY_EMAIL} export MY_INFO_EMAIL=${MY_INFO_EMAIL} export MY_PASSWORD=${MY_PASSWORD} @@ -158,8 +193,8 @@ jobs: fi fi - # Ensure proxy network exists - echo "🌐 Checking for proxy network..." + # Ensure networks exist + echo "🌐 Checking for networks..." if ! docker network inspect proxy >/dev/null 2>&1; then echo "⚠️ Proxy network not found, creating it..." docker network create proxy 2>/dev/null || echo "Network might already exist or creation failed" @@ -167,16 +202,28 @@ jobs: echo "✅ Proxy network exists" fi + if ! docker network inspect portfolio_dev >/dev/null 2>&1; then + echo "⚠️ Portfolio dev network not found, creating it..." + docker network create portfolio_dev 2>/dev/null || echo "Network might already exist or creation failed" + else + echo "✅ Portfolio dev network exists" + fi + + # Connect proxy network to portfolio_dev network if needed + # (This allows the app to access both proxy and DB/Redis) + # Start new container with updated image echo "🆕 Starting new dev container..." docker run -d \ --name $CONTAINER_NAME \ --restart unless-stopped \ - --network proxy \ + --network portfolio_dev \ -p ${HEALTH_PORT}:3000 \ -e NODE_ENV=production \ -e LOG_LEVEL=${LOG_LEVEL:-debug} \ -e NEXT_PUBLIC_BASE_URL=${NEXT_PUBLIC_BASE_URL_DEV:-https://dev.dk0.dev} \ + -e DATABASE_URL=${DATABASE_URL} \ + -e REDIS_URL=${REDIS_URL} \ -e MY_EMAIL=${MY_EMAIL} \ -e MY_INFO_EMAIL=${MY_INFO_EMAIL} \ -e MY_PASSWORD=${MY_PASSWORD} \ @@ -187,6 +234,10 @@ jobs: -e N8N_SECRET_TOKEN=${N8N_SECRET_TOKEN:-''} \ $IMAGE_NAME + # Connect container to proxy network as well (for external access) + echo "🔗 Connecting container to proxy network..." + docker network connect proxy $CONTAINER_NAME 2>/dev/null || echo "Container might already be connected to proxy network" + # Wait for new container to be healthy echo "⏳ Waiting for new container to be healthy..." HEALTH_CHECK_PASSED=false @@ -232,6 +283,8 @@ jobs: NODE_ENV: production LOG_LEVEL: ${{ vars.LOG_LEVEL || 'debug' }} NEXT_PUBLIC_BASE_URL_DEV: ${{ vars.NEXT_PUBLIC_BASE_URL_DEV || 'https://dev.dk0.dev' }} + DATABASE_URL: postgresql://portfolio_user:portfolio_dev_pass@portfolio_postgres_dev:5432/portfolio_dev?schema=public + REDIS_URL: redis://portfolio_redis_dev:6379 MY_EMAIL: ${{ vars.MY_EMAIL }} MY_INFO_EMAIL: ${{ vars.MY_INFO_EMAIL }} MY_PASSWORD: ${{ secrets.MY_PASSWORD }}