1010 name : ' 🐳 Build & Deploy'
1111 steps :
1212 - name : ' 🔍 Checkout Code'
13- uses : actions/checkout@v4 # Use the latest version
13+ uses : actions/checkout@v4
1414
1515 # ========================
1616 # 🔐 Secrets & Config Setup
@@ -37,38 +37,40 @@ jobs:
3737 - name : ' ⚙️ Create .env File'
3838 run : |
3939 echo "${{ secrets.ENV_FILE_CONTENT }}" > .env
40- echo "" >> .env # Ensure trailing newline
40+ echo "" >> .env
4141
4242 # =======================================================
4343 # 🐳 Docker Compose Operations (This section is updated)
4444 # =======================================================
4545 - name : ' 🚀 Launch or Update Services'
4646 run : |
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
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.
49+ if ! docker network ls | grep -q "codebuilder-net"; then
50+ echo "Network 'codebuilder-net' not found. Creating it..."
51+ docker network create codebuilder-net
52+ else
53+ echo "Network 'codebuilder-net' already exists. Skipping creation."
54+ fi
5355
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
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
6063
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
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
6972
7073 - name : ' 🗑 Prune Old Docker Images'
71- if : always() # Run this step even if the deployment fails
74+ if : always() # This step will run even if the deployment fails.
7275 run : |
7376 docker image prune -af
74- # This is an optional but recommended step to clean up old, unused image layers.
0 commit comments