Skip to content

Commit 519c32f

Browse files
Fixed to handle existing containers & existing networks on production machine.
1 parent a32fb3d commit 519c32f

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

.github/workflows/deploy.yml

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
name: '🐳 Build & Deploy'
1111
steps:
1212
- name: '🔍 Checkout Code'
13-
uses: actions/checkout@v3
13+
uses: actions/checkout@v4 # Use the latest version
1414

1515
# ========================
1616
# 🔐 Secrets & Config Setup
@@ -27,7 +27,10 @@ jobs:
2727
run: |
2828
echo "$GOOGLE_SERVICES_JSON_BASE64" | base64 --decode > google-services.json
2929
echo "🔄 Validating JSON..."
30-
jq empty google-services.json # Requires jq installed
30+
if ! jq empty google-services.json; then
31+
echo "❌ JSON validation failed!"
32+
exit 1
33+
fi
3134
env:
3235
GOOGLE_SERVICES_JSON_BASE64: ${{ secrets.GOOGLE_SERVICES_JSON_BASE64 }}
3336

@@ -41,13 +44,31 @@ jobs:
4144
# =======================================================
4245
- name: '🚀 Launch or Update Services'
4346
run: |
44-
docker compose up --build -d
45-
# 'up' brings all services up.
46-
# '--build' rebuilds the webapp image if code has changed.
47-
# '-d' runs the containers in the background (detached mode).
48-
# Docker Compose automatically stops and replaces only the containers that need updating.
47+
# Step 1: Ensure the Docker network exists.
48+
# This command will create the network if it's missing,
49+
# and do nothing if it already exists. The '|| true' part
50+
# prevents the workflow from failing if it already exists.
51+
echo "Ensuring network 'codebuilder-net' exists..."
52+
docker network create codebuilder-net || true
53+
54+
# Step 2: Bring up the database if it's not running.
55+
# This command ensures the 'db' service is up and running.
56+
# On the first run, it will create and start the db container.
57+
# On subsequent runs, it will see the db is already running and do nothing.
58+
echo "Ensuring database service is running..."
59+
docker compose up -d db
60+
61+
# Step 3: Rebuild and restart ONLY the webapp service.
62+
# This is the core of the update process.
63+
# --no-deps: Prevents Compose from touching the 'db' service.
64+
# --build: Forces a rebuild of the 'webapp' image using the latest code.
65+
# Docker Compose will automatically stop the old webapp container
66+
# and start a new one based on the new image.
67+
echo "Rebuilding and deploying the webapp..."
68+
docker compose up -d --no-deps --build webapp
4969
5070
- name: '🗑 Prune Old Docker Images'
71+
if: always() # Run this step even if the deployment fails
5172
run: |
5273
docker image prune -af
5374
# This is an optional but recommended step to clean up old, unused image layers.

docker-compose.yml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,13 @@ services:
4242
- "5434:5432"
4343

4444
# Define the custom network that our services will share.
45-
# This allows them to communicate with each other using their service names
46-
# (e.g., the webapp can connect to 'db' on port 5432).
4745
networks:
48-
# This network will be named exactly 'codebuilder-net'
49-
# with no project prefix.
5046
codebuilder-net:
51-
name: codebuilder-net
52-
driver: bridge
47+
# This explicitly tells Compose that the network is created
48+
# and managed outside of its control, which our deploy.yml now does.
49+
external: true
5350

5451
# Define the named volumes for persistent data storage.
5552
# Docker manages the lifecycle of these volumes.
5653
volumes:
57-
postgres-data:
54+
postgres-data:

0 commit comments

Comments
 (0)