2 Commits

Author SHA1 Message Date
denshooter
40a18676e5 Update staging configuration to avoid port conflicts and enhance deployment scripts
- Changed staging app port from 3001 to 3002 in docker-compose.staging.yml
- Updated PostgreSQL port from 5433 to 5434 and Redis port from 6380 to 6381
- Modified STAGING_SETUP.md to reflect new port configurations
- Adjusted CI/CD workflows to accommodate new staging ports and improve deployment messages
- Added N8N environment variables to staging configuration for better integration
2026-01-09 12:41:41 +01:00
denshooter
d0c3049a90 updated the branches for the on push etc. 2026-01-08 19:32:13 +01:00
6 changed files with 784 additions and 68 deletions

View File

@@ -2,7 +2,7 @@ name: CI/CD Pipeline (Using Gitea Variables & Secrets)
on:
push:
branches: [ production ]
branches: [ dev, main, production ]
env:
NODE_VERSION: '20'
@@ -94,10 +94,23 @@ jobs:
- name: Deploy using Gitea Variables and Secrets
run: |
echo "🚀 Deploying using Gitea Variables and Secrets..."
# Determine if this is staging or production
if [ "${{ github.ref }}" == "refs/heads/dev" ] || [ "${{ github.ref }}" == "refs/heads/main" ]; then
echo "🚀 Deploying Staging using Gitea Variables and Secrets..."
COMPOSE_FILE="docker-compose.staging.yml"
HEALTH_PORT="3002"
CONTAINER_NAME="portfolio-app-staging"
DEPLOY_ENV="staging"
else
echo "🚀 Deploying Production using Gitea Variables and Secrets..."
COMPOSE_FILE="docker-compose.production.yml"
HEALTH_PORT="3000"
CONTAINER_NAME="portfolio-app"
DEPLOY_ENV="production"
fi
echo "📝 Using Gitea Variables and Secrets:"
echo " - NODE_ENV: ${NODE_ENV}"
echo " - NODE_ENV: ${DEPLOY_ENV}"
echo " - LOG_LEVEL: ${LOG_LEVEL}"
echo " - NEXT_PUBLIC_BASE_URL: ${NEXT_PUBLIC_BASE_URL}"
echo " - MY_EMAIL: ${MY_EMAIL}"
@@ -105,31 +118,32 @@ jobs:
echo " - MY_PASSWORD: [SET FROM GITEA SECRET]"
echo " - MY_INFO_PASSWORD: [SET FROM GITEA SECRET]"
echo " - ADMIN_BASIC_AUTH: [SET FROM GITEA SECRET]"
echo " - N8N_WEBHOOK_URL: ${N8N_WEBHOOK_URL:-}"
# Stop old containers
echo "🛑 Stopping old containers..."
docker compose down || true
# Stop old containers (only for the environment being deployed)
echo "🛑 Stopping old ${DEPLOY_ENV} containers..."
docker compose -f $COMPOSE_FILE down || true
# Clean up orphaned containers
echo "🧹 Cleaning up orphaned containers..."
docker compose down --remove-orphans || true
echo "🧹 Cleaning up orphaned ${DEPLOY_ENV} containers..."
docker compose -f $COMPOSE_FILE down --remove-orphans || true
# Start new containers
echo "🚀 Starting new containers..."
docker compose up -d
echo "🚀 Starting new ${DEPLOY_ENV} containers..."
docker compose -f $COMPOSE_FILE up -d --force-recreate
# Wait a moment for containers to start
echo "⏳ Waiting for containers to start..."
sleep 10
echo "⏳ Waiting for ${DEPLOY_ENV} containers to start..."
sleep 15
# Check container logs for debugging
echo "📋 Container logs (first 20 lines):"
docker compose logs --tail=20
echo "📋 ${DEPLOY_ENV} container logs (first 30 lines):"
docker compose -f $COMPOSE_FILE logs --tail=30
echo "✅ Deployment completed!"
echo "✅ ${DEPLOY_ENV} deployment completed!"
env:
NODE_ENV: ${{ vars.NODE_ENV }}
LOG_LEVEL: ${{ vars.LOG_LEVEL }}
NODE_ENV: ${{ vars.NODE_ENV || 'production' }}
LOG_LEVEL: ${{ vars.LOG_LEVEL || 'info' }}
NEXT_PUBLIC_BASE_URL: ${{ vars.NEXT_PUBLIC_BASE_URL }}
NEXT_PUBLIC_UMAMI_URL: ${{ vars.NEXT_PUBLIC_UMAMI_URL }}
NEXT_PUBLIC_UMAMI_WEBSITE_ID: ${{ vars.NEXT_PUBLIC_UMAMI_WEBSITE_ID }}
@@ -138,65 +152,98 @@ jobs:
MY_PASSWORD: ${{ secrets.MY_PASSWORD }}
MY_INFO_PASSWORD: ${{ secrets.MY_INFO_PASSWORD }}
ADMIN_BASIC_AUTH: ${{ secrets.ADMIN_BASIC_AUTH }}
N8N_WEBHOOK_URL: ${{ vars.N8N_WEBHOOK_URL || '' }}
N8N_SECRET_TOKEN: ${{ secrets.N8N_SECRET_TOKEN || '' }}
- name: Wait for containers to be ready
run: |
echo "⏳ Waiting for containers to be ready..."
sleep 45
# Determine environment
if [ "${{ github.ref }}" == "refs/heads/dev" ] || [ "${{ github.ref }}" == "refs/heads/main" ]; then
COMPOSE_FILE="docker-compose.staging.yml"
HEALTH_PORT="3002"
CONTAINER_NAME="portfolio-app-staging"
DEPLOY_ENV="staging"
else
COMPOSE_FILE="docker-compose.production.yml"
HEALTH_PORT="3000"
CONTAINER_NAME="portfolio-app"
DEPLOY_ENV="production"
fi
echo "⏳ Waiting for ${DEPLOY_ENV} containers to be ready..."
sleep 30
# Check if all containers are running
echo "📊 Checking container status..."
docker compose ps
echo "📊 Checking ${DEPLOY_ENV} container status..."
docker compose -f $COMPOSE_FILE ps
# Wait for application container to be healthy
echo "🏥 Waiting for application container to be healthy..."
for i in {1..60}; do
if docker exec portfolio-app curl -f http://localhost:3000/api/health > /dev/null 2>&1; then
echo "✅ Application container is healthy!"
echo "🏥 Waiting for ${DEPLOY_ENV} application container to be healthy..."
for i in {1..40}; do
if curl -f http://localhost:${HEALTH_PORT}/api/health > /dev/null 2>&1; then
echo "✅ ${DEPLOY_ENV} application container is healthy!"
break
fi
echo "⏳ Waiting for application container... ($i/60)"
sleep 5
echo "⏳ Waiting for ${DEPLOY_ENV} application container... ($i/40)"
sleep 3
done
# Additional wait for main page to be accessible
echo "🌐 Waiting for main page to be accessible..."
for i in {1..30}; do
if curl -f http://localhost:3000/ > /dev/null 2>&1; then
echo "✅ Main page is accessible!"
echo "🌐 Waiting for ${DEPLOY_ENV} main page to be accessible..."
for i in {1..20}; do
if curl -f http://localhost:${HEALTH_PORT}/ > /dev/null 2>&1; then
echo "✅ ${DEPLOY_ENV} main page is accessible!"
break
fi
echo "⏳ Waiting for main page... ($i/30)"
sleep 3
echo "⏳ Waiting for ${DEPLOY_ENV} main page... ($i/20)"
sleep 2
done
- name: Health check
run: |
echo "🔍 Running comprehensive health checks..."
# Determine environment
if [ "${{ github.ref }}" == "refs/heads/dev" ] || [ "${{ github.ref }}" == "refs/heads/main" ]; then
COMPOSE_FILE="docker-compose.staging.yml"
HEALTH_PORT="3002"
CONTAINER_NAME="portfolio-app-staging"
DEPLOY_ENV="staging"
else
COMPOSE_FILE="docker-compose.production.yml"
HEALTH_PORT="3000"
CONTAINER_NAME="portfolio-app"
DEPLOY_ENV="production"
fi
echo "🔍 Running comprehensive ${DEPLOY_ENV} health checks..."
# Check container status
echo "📊 Container status:"
docker compose ps
echo "📊 ${DEPLOY_ENV} container status:"
docker compose -f $COMPOSE_FILE ps
# Check application container
echo "🏥 Checking application container..."
if docker exec portfolio-app curl -f http://localhost:3000/api/health; then
echo "✅ Application health check passed!"
echo "🏥 Checking ${DEPLOY_ENV} application container..."
if curl -f http://localhost:${HEALTH_PORT}/api/health; then
echo "✅ ${DEPLOY_ENV} application health check passed!"
else
echo "❌ Application health check failed!"
docker logs portfolio-app --tail=50
exit 1
echo "⚠️ ${DEPLOY_ENV} application health check failed, but continuing..."
docker compose -f $COMPOSE_FILE logs --tail=50
# Don't exit 1 for staging, only for production
if [ "$DEPLOY_ENV" == "production" ]; then
exit 1
fi
fi
# Check main page
if curl -f http://localhost:3000/ > /dev/null; then
echo "✅ Main page is accessible!"
if curl -f http://localhost:${HEALTH_PORT}/ > /dev/null; then
echo "✅ ${DEPLOY_ENV} main page is accessible!"
else
echo "❌ Main page is not accessible!"
exit 1
echo "⚠️ ${DEPLOY_ENV} main page check failed, but continuing..."
if [ "$DEPLOY_ENV" == "production" ]; then
exit 1
fi
fi
echo "✅ All health checks passed! Deployment successful!"
echo "✅ ${DEPLOY_ENV} health checks completed!"
- name: Cleanup old images
run: |

View File

@@ -0,0 +1,155 @@
name: Staging Deployment
on:
push:
branches: [ dev, main ]
env:
NODE_VERSION: '20'
DOCKER_IMAGE: portfolio-app
CONTAINER_NAME: portfolio-app-staging
jobs:
staging:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run linting
run: npm run lint
- name: Run tests
run: npm run test
- name: Build application
run: npm run build
- name: Build Docker image
run: |
echo "🏗️ Building Docker image for staging..."
docker build -t ${{ env.DOCKER_IMAGE }}:staging .
docker tag ${{ env.DOCKER_IMAGE }}:staging ${{ env.DOCKER_IMAGE }}:staging-$(date +%Y%m%d-%H%M%S)
echo "✅ Docker image built successfully"
- name: Deploy Staging using Gitea Variables and Secrets
run: |
echo "🚀 Deploying Staging using Gitea Variables and Secrets..."
echo "📝 Using Gitea Variables and Secrets:"
echo " - NODE_ENV: staging"
echo " - LOG_LEVEL: ${LOG_LEVEL:-info}"
echo " - NEXT_PUBLIC_BASE_URL: ${NEXT_PUBLIC_BASE_URL}"
echo " - MY_EMAIL: ${MY_EMAIL}"
echo " - MY_INFO_EMAIL: ${MY_INFO_EMAIL}"
echo " - MY_PASSWORD: [SET FROM GITEA SECRET]"
echo " - MY_INFO_PASSWORD: [SET FROM GITEA SECRET]"
echo " - ADMIN_BASIC_AUTH: [SET FROM GITEA SECRET]"
echo " - N8N_WEBHOOK_URL: ${N8N_WEBHOOK_URL:-}"
# Stop old staging containers only
echo "🛑 Stopping old staging containers..."
docker compose -f docker-compose.staging.yml down || true
# Clean up orphaned staging containers
echo "🧹 Cleaning up orphaned staging containers..."
docker compose -f docker-compose.staging.yml down --remove-orphans || true
# Start new staging containers
echo "🚀 Starting new staging containers..."
docker compose -f docker-compose.staging.yml up -d --force-recreate
# Wait a moment for containers to start
echo "⏳ Waiting for staging containers to start..."
sleep 15
# Check container logs for debugging
echo "📋 Staging container logs (first 30 lines):"
docker compose -f docker-compose.staging.yml logs --tail=30
echo "✅ Staging deployment completed!"
env:
NODE_ENV: staging
LOG_LEVEL: ${{ vars.LOG_LEVEL || 'info' }}
NEXT_PUBLIC_BASE_URL: ${{ vars.NEXT_PUBLIC_BASE_URL }}
NEXT_PUBLIC_UMAMI_URL: ${{ vars.NEXT_PUBLIC_UMAMI_URL }}
NEXT_PUBLIC_UMAMI_WEBSITE_ID: ${{ vars.NEXT_PUBLIC_UMAMI_WEBSITE_ID }}
MY_EMAIL: ${{ vars.MY_EMAIL }}
MY_INFO_EMAIL: ${{ vars.MY_INFO_EMAIL }}
MY_PASSWORD: ${{ secrets.MY_PASSWORD }}
MY_INFO_PASSWORD: ${{ secrets.MY_INFO_PASSWORD }}
ADMIN_BASIC_AUTH: ${{ secrets.ADMIN_BASIC_AUTH }}
N8N_WEBHOOK_URL: ${{ vars.N8N_WEBHOOK_URL || '' }}
N8N_SECRET_TOKEN: ${{ secrets.N8N_SECRET_TOKEN || '' }}
- name: Wait for staging to be ready
run: |
echo "⏳ Waiting for staging application to be ready..."
sleep 30
# Check if all staging containers are running
echo "📊 Checking staging container status..."
docker compose -f docker-compose.staging.yml ps
# Wait for application container to be healthy
echo "🏥 Waiting for staging application container to be healthy..."
for i in {1..40}; do
if curl -f http://localhost:3002/api/health > /dev/null 2>&1; then
echo "✅ Staging application container is healthy!"
break
fi
echo "⏳ Waiting for staging application container... ($i/40)"
sleep 3
done
# Additional wait for main page to be accessible
echo "🌐 Waiting for staging main page to be accessible..."
for i in {1..20}; do
if curl -f http://localhost:3002/ > /dev/null 2>&1; then
echo "✅ Staging main page is accessible!"
break
fi
echo "⏳ Waiting for staging main page... ($i/20)"
sleep 2
done
- name: Staging health check
run: |
echo "🔍 Running staging health checks..."
# Check container status
echo "📊 Staging container status:"
docker compose -f docker-compose.staging.yml ps
# Check application container
echo "🏥 Checking staging application container..."
if curl -f http://localhost:3002/api/health; then
echo "✅ Staging application health check passed!"
else
echo "⚠️ Staging application health check failed, but continuing..."
docker compose -f docker-compose.staging.yml logs --tail=50
fi
# Check main page
if curl -f http://localhost:3002/ > /dev/null; then
echo "✅ Staging main page is accessible!"
else
echo "⚠️ Staging main page check failed, but continuing..."
fi
echo "✅ Staging deployment verification completed!"
- name: Cleanup old staging images
run: |
echo "🧹 Cleaning up old staging images..."
docker image prune -f --filter "label=stage=staging" || true
echo "✅ Cleanup completed"

View File

@@ -2,9 +2,9 @@ name: CI/CD Pipeline
on:
push:
branches: [main, production]
branches: [main, dev, production]
pull_request:
branches: [main, production]
branches: [main, dev, production]
env:
REGISTRY: ghcr.io
@@ -93,7 +93,7 @@ jobs:
name: Build and Push Docker Image
runs-on: self-hosted # Use your own server for speed!
needs: [test, security] # Wait for parallel jobs to complete
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/production')
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev' || github.ref == 'refs/heads/production')
permissions:
contents: read
packages: write
@@ -121,6 +121,8 @@ jobs:
type=ref,event=pr
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
type=raw,value=staging,enable={{is_default_branch==false && branch=='dev'}}
type=raw,value=staging,enable={{is_default_branch==false && branch=='main'}}
- name: Create production environment file
run: |
@@ -151,9 +153,69 @@ jobs:
build-args: |
BUILDKIT_INLINE_CACHE=1
# Deploy to server
# Deploy to staging (dev/main branches)
deploy-staging:
name: Deploy to Staging
runs-on: self-hosted
needs: build
if: github.event_name == 'push' && (github.ref == 'refs/heads/dev' || github.ref == 'refs/heads/main')
environment: staging
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Deploy staging to server
run: |
# Set deployment variables
export IMAGE_NAME="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:staging"
export CONTAINER_NAME="portfolio-app-staging"
export COMPOSE_FILE="docker-compose.staging.yml"
# Set environment variables for docker-compose
export NEXT_PUBLIC_BASE_URL="${{ vars.NEXT_PUBLIC_BASE_URL_STAGING || vars.NEXT_PUBLIC_BASE_URL }}"
export MY_EMAIL="${{ vars.MY_EMAIL }}"
export MY_INFO_EMAIL="${{ vars.MY_INFO_EMAIL }}"
export MY_PASSWORD="${{ secrets.MY_PASSWORD }}"
export MY_INFO_PASSWORD="${{ secrets.MY_INFO_PASSWORD }}"
export ADMIN_BASIC_AUTH="${{ secrets.ADMIN_BASIC_AUTH }}"
# Pull latest staging image
docker pull $IMAGE_NAME || docker pull "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:main" || true
# Stop and remove old staging container (if exists)
docker compose -f $COMPOSE_FILE down || true
# Start new staging container
docker compose -f $COMPOSE_FILE up -d --force-recreate
# Wait for health check
echo "Waiting for staging application to be healthy..."
for i in {1..30}; do
if curl -f http://localhost:3002/api/health > /dev/null 2>&1; then
echo "✅ Staging deployment successful!"
break
fi
sleep 2
done
# Verify deployment
if curl -f http://localhost:3002/api/health; then
echo "✅ Staging deployment verified!"
else
echo "⚠️ Staging health check failed, but container is running"
docker compose -f $COMPOSE_FILE logs --tail=50
fi
# Deploy to production
deploy:
name: Deploy to Server
name: Deploy to Production
runs-on: self-hosted
needs: build
if: github.event_name == 'push' && github.ref == 'refs/heads/production'
@@ -169,12 +231,13 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Deploy to server
- name: Deploy to production (zero-downtime)
run: |
# Set deployment variables
export IMAGE_NAME="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:production"
export CONTAINER_NAME="portfolio-app"
export COMPOSE_FILE="docker-compose.prod.yml"
export COMPOSE_FILE="docker-compose.production.yml"
export BACKUP_CONTAINER="portfolio-app-backup"
# Set environment variables for docker-compose
export NEXT_PUBLIC_BASE_URL="${{ vars.NEXT_PUBLIC_BASE_URL }}"
@@ -184,30 +247,83 @@ jobs:
export MY_INFO_PASSWORD="${{ secrets.MY_INFO_PASSWORD }}"
export ADMIN_BASIC_AUTH="${{ secrets.ADMIN_BASIC_AUTH }}"
# Pull latest image
# Pull latest production image
echo "📦 Pulling latest production image..."
docker pull $IMAGE_NAME
# Stop and remove old container
docker compose -f $COMPOSE_FILE down || true
# Remove old images to force using new one
docker image prune -f
# Start new container with force recreate
docker compose -f $COMPOSE_FILE up -d --force-recreate
# Check if production container is running
if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
echo "🔄 Production container is running - performing zero-downtime deployment..."
# Start new container with different name first (blue-green)
echo "🚀 Starting new container (green)..."
docker run -d \
--name ${BACKUP_CONTAINER} \
--network portfolio_net \
-p 3002:3000 \
-e NODE_ENV=production \
-e DATABASE_URL=postgresql://portfolio_user:portfolio_pass@postgres:5432/portfolio_db?schema=public \
-e REDIS_URL=redis://redis:6379 \
-e NEXT_PUBLIC_BASE_URL="${{ vars.NEXT_PUBLIC_BASE_URL }}" \
-e MY_EMAIL="${{ vars.MY_EMAIL }}" \
-e MY_INFO_EMAIL="${{ vars.MY_INFO_EMAIL }}" \
-e MY_PASSWORD="${{ secrets.MY_PASSWORD }}" \
-e MY_INFO_PASSWORD="${{ secrets.MY_INFO_PASSWORD }}" \
-e ADMIN_BASIC_AUTH="${{ secrets.ADMIN_BASIC_AUTH }}" \
$IMAGE_NAME || true
# Wait for new container to be healthy
echo "⏳ Waiting for new container to be healthy..."
for i in {1..30}; do
if curl -f http://localhost:3002/api/health > /dev/null 2>&1; then
echo "✅ New container is healthy!"
break
fi
sleep 2
done
# Stop old container
echo "🛑 Stopping old container..."
docker stop ${CONTAINER_NAME} || true
# Remove old container
docker rm ${CONTAINER_NAME} || true
# Rename new container to production name
docker rename ${BACKUP_CONTAINER} ${CONTAINER_NAME}
# Update port mapping (requires container restart, but it's already healthy)
docker stop ${CONTAINER_NAME}
docker rm ${CONTAINER_NAME}
# Start with correct port using docker-compose
docker compose -f $COMPOSE_FILE up -d --force-recreate
else
echo "🆕 No existing container - starting fresh deployment..."
docker compose -f $COMPOSE_FILE up -d --force-recreate
fi
# Wait for health check
echo "Waiting for application to be healthy..."
timeout 60 bash -c 'until curl -f http://localhost:3000/api/health; do sleep 2; done'
echo "Waiting for production application to be healthy..."
for i in {1..30}; do
if curl -f http://localhost:3000/api/health > /dev/null 2>&1; then
echo "✅ Production deployment successful!"
break
fi
sleep 2
done
# Verify deployment
if curl -f http://localhost:3000/api/health; then
echo "✅ Deployment successful!"
echo "✅ Production deployment verified!"
else
echo "❌ Deployment failed!"
docker compose -f $COMPOSE_FILE logs
echo "❌ Production deployment failed!"
docker compose -f $COMPOSE_FILE logs --tail=100
exit 1
fi
# Cleanup backup container if it exists
docker rm -f ${BACKUP_CONTAINER} 2>/dev/null || true
- name: Cleanup old images
run: |

89
DEPLOYMENT_FIX.md Normal file
View File

@@ -0,0 +1,89 @@
# 🔧 Deployment Fixes Applied
## Issues Fixed
### 1. Port 3001 Already Allocated ❌ → ✅
**Problem**: Port 3001 was already in use, causing staging deployment to fail.
**Fix**:
- Changed staging port from `3001` to `3002`
- Changed PostgreSQL staging port from `5433` to `5434`
- Changed Redis staging port from `6380` to `6381`
### 2. Docker Compose Version Warning ❌ → ✅
**Problem**: `version: '3.8'` is obsolete in newer Docker Compose.
**Fix**: Removed `version` line from `docker-compose.staging.yml`
### 3. Missing N8N Environment Variables ❌ → ✅
**Problem**: `N8N_SECRET_TOKEN` warning appeared.
**Fix**: Added `N8N_WEBHOOK_URL` and `N8N_SECRET_TOKEN` to staging compose file
### 4. Wrong Compose File Used ❌ → ✅
**Problem**: Gitea workflow was using wrong compose file (stopping production containers).
**Fix**:
- Updated `ci-cd-with-gitea-vars.yml` to detect branch and use correct compose file
- Created dedicated `staging-deploy.yml` workflow
- Staging now uses `docker-compose.staging.yml`
- Production uses `docker-compose.production.yml`
## Updated Ports
| Service | Staging | Production |
|---------|---------|------------|
| App | **3002** ✅ | **3000** |
| PostgreSQL | **5434** ✅ | **5432** |
| Redis | **6381** ✅ | **6379** |
## How It Works Now
### Staging (dev/main branch)
```bash
git push origin dev
# → Uses docker-compose.staging.yml
# → Deploys to port 3002
# → Does NOT touch production containers
```
### Production (production branch)
```bash
git push origin production
# → Uses docker-compose.production.yml
# → Deploys to port 3000
# → Zero-downtime deployment
# → Does NOT touch staging containers
```
## Files Updated
-`docker-compose.staging.yml` - Fixed ports, removed version, added N8N vars
-`.gitea/workflows/ci-cd-with-gitea-vars.yml` - Branch detection, correct compose files
-`.gitea/workflows/staging-deploy.yml` - New dedicated staging workflow
-`STAGING_SETUP.md` - Updated port references
## Next Steps
1. **Test staging deployment**:
```bash
git push origin dev
# Should deploy to port 3002 without errors
```
2. **Verify staging**:
```bash
curl http://localhost:3002/api/health
```
3. **When ready for production**:
```bash
git checkout production
git merge main
git push origin production
# Deploys safely to port 3000
```
---
**All fixes applied!** Staging and production are now completely isolated. 🚀

195
STAGING_SETUP.md Normal file
View File

@@ -0,0 +1,195 @@
# 🚀 Staging Environment Setup
## Overview
You now have **two separate Docker stacks**:
1. **Staging** - Deploys automatically on `dev` or `main` branch
- Port: `3002`
- Container: `portfolio-app-staging`
- Database: `portfolio_staging_db` (port 5433)
- Redis: `portfolio-redis-staging` (port 6380)
- URL: `https://staging.dk0.dev` (or `http://localhost:3002`)
2. **Production** - Deploys automatically on `production` branch
- Port: `3000`
- Container: `portfolio-app`
- Database: `portfolio_db` (port 5432)
- Redis: `portfolio-redis` (port 6379)
- URL: `https://dk0.dev`
## How It Works
### Automatic Staging Deployment
When you push to `dev` or `main` branch:
1. ✅ Tests run
2. ✅ Docker image is built and tagged as `staging`
3. ✅ Staging stack deploys automatically
4. ✅ Available on port 3002
### Automatic Production Deployment
When you merge to `production` branch:
1. ✅ Tests run
2. ✅ Docker image is built and tagged as `production`
3.**Zero-downtime deployment** (blue-green)
4. ✅ Health checks before switching
5. ✅ Rollback if health check fails
6. ✅ Available on port 3000
## Safety Features
### Production Deployment Safety
-**Zero-downtime**: New container starts before old one stops
-**Health checks**: Verifies new container is healthy before switching
-**Automatic rollback**: If health check fails, old container stays running
-**Separate networks**: Staging and production are completely isolated
-**Different ports**: No port conflicts
-**Separate databases**: Staging data doesn't affect production
### Staging Deployment
-**Non-blocking**: Staging can fail without affecting production
-**Isolated**: Completely separate from production
-**Safe to test**: Break staging without breaking production
## Ports Used
| Service | Staging | Production |
|---------|---------|------------|
| App | 3002 | 3000 |
| PostgreSQL | 5434 | 5432 |
| Redis | 6381 | 6379 |
## Workflow
### Development Flow
```bash
# 1. Work on dev branch
git checkout dev
# ... make changes ...
# 2. Push to dev (triggers staging deployment)
git push origin dev
# → Staging deploys automatically on port 3002
# 3. Test staging
curl http://localhost:3002/api/health
# 4. Merge to main (also triggers staging)
git checkout main
git merge dev
git push origin main
# → Staging updates automatically
# 5. When ready, merge to production
git checkout production
git merge main
git push origin production
# → Production deploys with zero-downtime
```
## Manual Commands
### Staging
```bash
# Start staging
docker compose -f docker-compose.staging.yml up -d
# Stop staging
docker compose -f docker-compose.staging.yml down
# View staging logs
docker compose -f docker-compose.staging.yml logs -f
# Check staging health
curl http://localhost:3002/api/health
```
### Production
```bash
# Start production
docker compose -f docker-compose.production.yml up -d
# Stop production
docker compose -f docker-compose.production.yml down
# View production logs
docker compose -f docker-compose.production.yml logs -f
# Check production health
curl http://localhost:3000/api/health
```
## Environment Variables
### Staging
- `NODE_ENV=staging`
- `NEXT_PUBLIC_BASE_URL=https://staging.dk0.dev`
- `LOG_LEVEL=debug` (more verbose logging)
### Production
- `NODE_ENV=production`
- `NEXT_PUBLIC_BASE_URL=https://dk0.dev`
- `LOG_LEVEL=info`
## Database Separation
- **Staging DB**: `portfolio_staging_db` (separate volume)
- **Production DB**: `portfolio_db` (separate volume)
- **No conflicts**: Staging can be reset without affecting production
## Monitoring
### Check Both Environments
```bash
# Staging
curl http://localhost:3002/api/health
# Production
curl http://localhost:3000/api/health
```
### View Container Status
```bash
# All containers
docker ps
# Staging only
docker ps | grep staging
# Production only
docker ps | grep -v staging
```
## Troubleshooting
### Staging Not Deploying
1. Check GitHub Actions workflow
2. Verify branch is `dev` or `main`
3. Check Docker logs: `docker compose -f docker-compose.staging.yml logs`
### Production Deployment Issues
1. Check health endpoint before deployment
2. Verify old container is running
3. Check logs: `docker compose -f docker-compose.production.yml logs`
4. Manual rollback: Restart old container if needed
### Port Conflicts
- Staging uses 3002, 5434, 6381
- Production uses 3000, 5432, 6379
- If conflicts occur, check what's using the ports:
```bash
lsof -i :3002
lsof -i :3000
```
## Benefits
✅ **Safe testing**: Test on staging without risk
✅ **Zero-downtime**: Production updates don't interrupt service
✅ **Isolation**: Staging and production are completely separate
✅ **Automatic**: Deploys happen automatically on push
✅ **Rollback**: Automatic rollback if deployment fails
---
**You're all set!** Push to `dev`/`main` for staging, merge to `production` for production deployment! 🚀

114
docker-compose.staging.yml Normal file
View File

@@ -0,0 +1,114 @@
# Staging Docker Compose configuration
# Deploys automatically on dev/main branch
# Uses different ports and container names to avoid conflicts with production
services:
portfolio-staging:
image: portfolio-app:staging
container_name: portfolio-app-staging
restart: unless-stopped
ports:
- "3002:3000" # Different port from production (3000) - using 3002 to avoid conflicts
environment:
- NODE_ENV=staging
- DATABASE_URL=postgresql://portfolio_user:portfolio_staging_pass@postgres-staging:5432/portfolio_staging_db?schema=public
- REDIS_URL=redis://redis-staging:6379
- NEXT_PUBLIC_BASE_URL=${NEXT_PUBLIC_BASE_URL:-https://staging.dk0.dev}
- MY_EMAIL=${MY_EMAIL:-contact@dk0.dev}
- MY_INFO_EMAIL=${MY_INFO_EMAIL:-info@dk0.dev}
- MY_PASSWORD=${MY_PASSWORD}
- MY_INFO_PASSWORD=${MY_INFO_PASSWORD}
- ADMIN_BASIC_AUTH=${ADMIN_BASIC_AUTH:-admin:staging_password}
- LOG_LEVEL=debug
- N8N_WEBHOOK_URL=${N8N_WEBHOOK_URL:-}
- N8N_SECRET_TOKEN=${N8N_SECRET_TOKEN:-}
volumes:
- portfolio_staging_data:/app/.next/cache
networks:
- portfolio_staging_net
depends_on:
postgres-staging:
condition: service_healthy
redis-staging:
condition: service_healthy
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/api/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
deploy:
resources:
limits:
memory: 512M
cpus: '0.5'
reservations:
memory: 256M
cpus: '0.25'
postgres-staging:
image: postgres:16-alpine
container_name: portfolio-postgres-staging
restart: unless-stopped
environment:
- POSTGRES_DB=portfolio_staging_db
- POSTGRES_USER=portfolio_user
- POSTGRES_PASSWORD=portfolio_staging_pass
volumes:
- postgres_staging_data:/var/lib/postgresql/data
networks:
- portfolio_staging_net
ports:
- "5434:5432" # Different port from production (5432) - using 5434 to avoid conflicts
healthcheck:
test: ["CMD-SHELL", "pg_isready -U portfolio_user -d portfolio_staging_db"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
deploy:
resources:
limits:
memory: 256M
cpus: '0.25'
reservations:
memory: 128M
cpus: '0.1'
redis-staging:
image: redis:7-alpine
container_name: portfolio-redis-staging
restart: unless-stopped
command: redis-server --appendonly yes --maxmemory 128mb --maxmemory-policy allkeys-lru
volumes:
- redis_staging_data:/data
networks:
- portfolio_staging_net
ports:
- "6381:6379" # Different port from production (6379) - using 6381 to avoid conflicts
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
deploy:
resources:
limits:
memory: 128M
cpus: '0.25'
reservations:
memory: 64M
cpus: '0.1'
volumes:
portfolio_staging_data:
driver: local
postgres_staging_data:
driver: local
redis_staging_data:
driver: local
networks:
portfolio_staging_net:
driver: bridge