10
10
name : ' 🐳 Build & Deploy'
11
11
steps :
12
12
- name : ' 🔍 Checkout Code'
13
- uses : actions/checkout@v4 # Use the latest version
13
+ uses : actions/checkout@v4
14
14
15
15
# ========================
16
16
# 🔐 Secrets & Config Setup
@@ -37,38 +37,40 @@ jobs:
37
37
- name : ' ⚙️ Create .env File'
38
38
run : |
39
39
echo "${{ secrets.ENV_FILE_CONTENT }}" > .env
40
- echo "" >> .env # Ensure trailing newline
40
+ echo "" >> .env
41
41
42
42
# =======================================================
43
43
# 🐳 Docker Compose Operations (This section is updated)
44
44
# =======================================================
45
45
- name : ' 🚀 Launch or Update Services'
46
46
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
53
55
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
60
63
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
69
72
70
73
- 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.
72
75
run : |
73
76
docker image prune -af
74
- # This is an optional but recommended step to clean up old, unused image layers.
0 commit comments