10
10
name : ' 🐳 Build & Deploy'
11
11
steps :
12
12
- name : ' 🔍 Checkout Code'
13
- uses : actions/checkout@v3
13
+ uses : actions/checkout@v4 # Use the latest version
14
14
15
15
# ========================
16
16
# 🔐 Secrets & Config Setup
27
27
run : |
28
28
echo "$GOOGLE_SERVICES_JSON_BASE64" | base64 --decode > google-services.json
29
29
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
31
34
env :
32
35
GOOGLE_SERVICES_JSON_BASE64 : ${{ secrets.GOOGLE_SERVICES_JSON_BASE64 }}
33
36
@@ -41,13 +44,31 @@ jobs:
41
44
# =======================================================
42
45
- name : ' 🚀 Launch or Update Services'
43
46
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
49
69
50
70
- name : ' 🗑 Prune Old Docker Images'
71
+ if : always() # Run this step even if the deployment fails
51
72
run : |
52
73
docker image prune -af
53
74
# This is an optional but recommended step to clean up old, unused image layers.
0 commit comments