@@ -33,61 +33,82 @@ jobs:
3333 SERVER_USER : ${{ secrets.SERVER_USER }}
3434 SERVER_SSH_KEY : ${{ secrets.SERVER_SSH_KEY }}
3535
36- - name : ' 🐳 Log in to GitHub Container Registry'
37- uses : docker/login-action@v2
38- with :
39- registry : ghcr.io
40- username : ${{ github.actor }}
41- password : ${{ secrets.GITHUB_TOKEN }}
36+ - name : ' 🔒 Verify Secrets Exist'
37+ run : |
38+ if [ -z "${{ secrets.GOOGLE_SERVICES_JSON_BASE64 }}" ]; then
39+ echo "❌ Critical error: GOOGLE_SERVICES_JSON_BASE64 secret missing!"
40+ exit 1
41+ fi
42+ echo "✅ All secrets present"
4243
43- - name : ' 📦 Build & Push Docker Image'
44- uses : docker/build-push-action@v3
45- with :
46- context : .
47- push : true
48- tags : ghcr.io/codebuilderinc/codebuilder-api:latest
44+ - name : ' 📁 Create google-services.json'
45+ run : |
46+ echo "$GOOGLE_SERVICES_JSON_BASE64" | base64 --decode > google-services.json
47+ echo "🔄 Validating JSON..."
48+ if ! jq empty google-services.json; then
49+ echo "❌ JSON validation failed!"
50+ exit 1
51+ fi
4952 env :
50- BUILDKIT_PROGRESS : plain
53+ GOOGLE_SERVICES_JSON_BASE64 : ${{ secrets.GOOGLE_SERVICES_JSON_BASE64 }}
5154
52- - name : ' 🚀 Deploy to Remote Server'
53- if : github.ref == 'refs/heads/main'
54- uses : appleboy/ssh-action@v0.1.6
55- with :
56- host : ${{ secrets.SERVER_HOST }}
57- username : ${{ secrets.SERVER_USER }}
58- key : ${{ secrets.SERVER_SSH_KEY }}
59- script : |
60- echo "➡️ Ensuring Docker network 'codebuilder-net' exists..."
61- if ! docker network ls | grep -q codebuilder-net; then
62- echo "🆕 Creating network codebuilder-net"
63- docker network create codebuilder-net
64- else
65- echo "✔️ Network codebuilder-net already exists"
66- fi
55+ - name : ' ⚙️ Create .env File'
56+ run : |
57+ echo "${{ secrets.ENV_FILE_CONTENT }}" > .env
58+ echo "" >> .env
6759
68- echo "➡️ Ensuring database container is running..."
69- if docker ps -a --format '{{.Names}}' | grep -q '^nest-db$'; then
70- if ! docker ps --format '{{.Names}}' | grep -q '^nest-db$'; then
71- echo "🟡 Starting existing 'nest-db' container..."
72- docker start nest-db
73- else
74- echo "✔️ Database container 'nest-db' is already running"
75- fi
76- else
77- echo "🆕 Deploying fresh database container..."
78- docker-compose -f docker-compose.nest.yml up -d db
60+ # =======================================================
61+ # 🐳 Docker Operations
62+ # =======================================================
63+ - name : ' 🚀 Build, Launch, and Update Services'
64+ run : |
65+ # Step 1: Ensure the Docker network exists.
66+ if ! docker network ls | grep -q "codebuilder-net"; then
67+ echo "Network 'codebuilder-net' not found. Creating it..."
68+ docker network create codebuilder-net
69+ else
70+ echo "Network 'codebuilder-net' already exists. Skipping creation."
71+ fi
72+
73+ # Step 2: Ensure the database container is running.
74+ DB_CONTAINER_NAME="codebuilder-postgres-db"
75+ if [ $(docker ps -a -q -f name=^/${DB_CONTAINER_NAME}$) ]; then
76+ if ! [ $(docker ps -q -f name=^/${DB_CONTAINER_NAME}$) ]; then
77+ echo "Database container exists but is stopped. Starting it..."
78+ docker start ${DB_CONTAINER_NAME}
7979 fi
80+ else
81+ echo "Database container not found. Creating it..."
82+ # Added -p flag to associate with the correct stack
83+ docker compose -p codebuilder-frontend up -d db
84+ fi
85+
86+ # Step 3: Wait for the database to be healthy.
87+ echo "Waiting for database to become available on localhost:5434..."
88+ while ! nc -z localhost 5434; do sleep 1; done
89+ echo "✅ Database is healthy."
90+
91+ # =====================================================================
92+ # THE FIX: Force the build to run in default server mode.
93+ # This overrides any conflicting environment variables.
94+ # =====================================================================
95+ echo "Ensuring build runs in default server mode..."
96+ export NEXT_OUTPUT_MODE='standalone'
8097
81- echo "⏳ Waiting for database to be healthy on db:5432..."
82- until nc -z db 5432; do sleep 1; done
83- echo "✅ Database is healthy"
98+ # Step 4: Build the new webapp image.
99+ echo "Building the latest webapp image..."
100+ # Added -p flag to ensure build context is correct
101+ docker compose -p codebuilder-frontend build webapp
84102
85- echo "➡️ Pulling latest NestJS image..."
86- docker pull ghcr.io/codebuilderinc/codebuilder-api:latest
103+ # Step 5: Forcefully remove the old webapp container to prevent conflicts.
104+ echo "Forcefully removing old webapp container if it exists..."
105+ docker rm -f codebuilder-webapp || true
87106
88- echo "🔄 Deploying NestJS API service on port 4000..."
89- docker-compose -f docker-compose.nest.yml up -d --no-deps nest-api
107+ # Step 6: Deploy the new webapp container.
108+ echo "Deploying the new webapp container..."
109+ # Added -p flag to associate with the correct stack
110+ docker compose -p codebuilder-frontend up -d --no-deps webapp
90111
91- - name : ' 🗑️ Prune Old Docker Images'
112+ - name : ' 🗑 Prune Old Docker Images'
92113 if : always()
93114 run : docker image prune -af
0 commit comments