Skip to content

Commit 77bff3e

Browse files
Fixing workflow build issues.
1 parent adc08cd commit 77bff3e

File tree

2 files changed

+59
-48
lines changed

2 files changed

+59
-48
lines changed

.github/workflows/deploy.yml

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ jobs:
1212
- name: '🔍 Checkout Code'
1313
uses: actions/checkout@v4
1414

15-
# ========================
16-
# 🔐 Secrets & Config Setup
17-
# ========================
15+
# (Secrets and .env setup steps remain the same)
1816
- name: '🔒 Verify Secrets Exist'
1917
run: |
2018
if [ -z "${{ secrets.GOOGLE_SERVICES_JSON_BASE64 }}" ]; then
@@ -40,37 +38,44 @@ jobs:
4038
echo "" >> .env
4139
4240
# =======================================================
43-
# 🐳 Docker Compose Operations (This section is updated)
41+
# 🐳 Docker Operations (This section is updated)
4442
# =======================================================
45-
- name: '🚀 Launch or Update Services'
43+
- name: '🚀 Build, Launch, and Update Services'
4644
run: |
47-
# Step 1: Explicitly check for the network and create it only if it's missing.
48-
# This is more robust than the previous '|| true' syntax.
45+
# Step 1: Ensure the Docker network exists.
4946
if ! docker network ls | grep -q "codebuilder-net"; then
5047
echo "Network 'codebuilder-net' not found. Creating it..."
5148
docker network create codebuilder-net
5249
else
5350
echo "Network 'codebuilder-net' already exists. Skipping creation."
5451
fi
5552
56-
# Step 2: Bring up all services defined in the compose file.
57-
# This command is idempotent. On the first run, it creates and starts everything.
58-
# On subsequent runs, it will connect to existing containers and ensure they
59-
# are started if they were stopped, but it will NOT try to re-create them.
60-
# This safely ensures the database is running before we proceed.
61-
echo "Ensuring all services are up..."
62-
docker compose up -d
53+
# Step 2: Start the database service independently.
54+
# The 'up -d' command is idempotent; it will start the 'db' if it's not running,
55+
# and do nothing if it already is.
56+
echo "Starting database service..."
57+
docker compose up -d db
6358
64-
# Step 3: Force a rebuild and replacement of the webapp service ONLY.
65-
# This is the key command for zero-downtime-style updates of your app.
66-
# --no-deps: Guarantees that the 'db' container will not be touched.
67-
# --build: Rebuilds the webapp image from your latest source code.
68-
# --force-recreate: Explicitly stops the old webapp container and starts a new one
69-
# from the new image, even if the configuration is the same.
70-
echo "Rebuilding and force-recreating the webapp service..."
71-
docker compose up -d --no-deps --build --force-recreate webapp
59+
# Step 3: Wait for the database to be fully ready.
60+
# We use netcat (nc) to poll the database port exposed to the host machine.
61+
# This is critical to ensure the build doesn't start prematurely.
62+
# NOTE: We use port 5434 because that is what you mapped in your docker-compose.yml
63+
echo "Waiting for database to become available on localhost:5434..."
64+
while ! nc -z localhost 5434; do
65+
sleep 1 # wait for 1 second before trying again
66+
done
67+
echo "✅ Database is healthy and listening."
68+
69+
# Step 4: NOW that the DB is running, build the webapp image.
70+
# The Docker builder will be able to connect to the 'db' service over the network.
71+
echo "Building the webapp image..."
72+
docker compose build webapp
73+
74+
# Step 5: Deploy the newly built webapp container.
75+
# This command surgically replaces the webapp without touching the database.
76+
echo "Deploying the new webapp container..."
77+
docker compose up -d --no-deps --force-recreate webapp
7278
7379
- name: '🗑 Prune Old Docker Images'
74-
if: always() # This step will run even if the deployment fails.
75-
run: |
76-
docker image prune -af
80+
if: always()
81+
run: docker image prune -af

Dockerfile

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,52 @@
1-
# ====================
2-
# 1) Build Stage
3-
# ====================
1+
# Stage 1: Build the application
2+
# We name this stage 'builder'
43
FROM node:22-alpine AS builder
4+
5+
# Set the working directory
56
WORKDIR /app
7+
8+
# Install pnpm globally
69
RUN npm install -g pnpm
710

11+
# Copy package manifests and lockfile
812
COPY package.json pnpm-lock.yaml ./
13+
14+
# Install dependencies
915
RUN pnpm install --strict-peer-dependencies=false
1016

17+
# Copy the rest of your application code
1118
COPY . .
19+
20+
# Run Prisma Generate (this is safe and often needed for types)
1221
RUN npx prisma generate
22+
23+
# Build the application. This will now succeed because the database
24+
# service will be running and available on the network.
1325
RUN pnpm run build
1426

15-
# ====================
16-
# 2) Production Stage
17-
# ====================
27+
# Stage 2: Production Image
28+
# Create a smaller, cleaner image for production
1829
FROM node:22-alpine
30+
1931
WORKDIR /app
20-
RUN npm install -g pnpm
2132

22-
# We use 'nc' to check if the database port is open.
33+
# Install netcat for the entrypoint healthcheck
2334
RUN apk add --no-cache netcat-openbsd
2435

25-
# Copy only the necessary files from the builder stage.
26-
COPY --from=builder /app/node_modules ./
27-
COPY --from=builder /app/package.json ./
28-
COPY --from=builder /app/pnpm-lock.yaml ./
36+
# Copy only the necessary production artifacts from the 'builder' stage
37+
COPY --from=builder /app/node_modules ./node_modules
38+
COPY --from=builder /app/package.json ./package.json
39+
COPY --from=builder /app/pnpm-lock.yaml ./pnpm-lock.yaml
2940
COPY --from=builder /app/.next ./.next
3041
COPY --from=builder /app/prisma ./prisma
3142
COPY --from=builder /app/public ./public
32-
33-
# Copy and prepare the entrypoint script.
3443
COPY docker-entrypoint.sh .
35-
RUN chmod +x docker-entrypoint.sh
36-
3744

38-
# Install production-only dependencies.
39-
RUN pnpm install --prod --strict-peer-dependencies=false
40-
41-
EXPOSE 3000
45+
# Ensure the entrypoint is executable
46+
RUN chmod +x docker-entrypoint.sh
4247

43-
# The ENTRYPOINT is our script.
44-
# The CMD is the command that gets passed to our script after migrations run.
48+
# This is the command that will run when the container starts
4549
ENTRYPOINT ["./docker-entrypoint.sh"]
46-
CMD ["pnpm", "start"]
50+
51+
# The default command for the entrypoint script
52+
CMD ["pnpm", "start"]

0 commit comments

Comments
 (0)