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
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.
0 commit comments