@@ -50,31 +50,35 @@ jobs:
50
50
echo "Network 'codebuilder-net' already exists. Skipping creation."
51
51
fi
52
52
53
- # Step 2: Start the database service independently.
54
- # The 'up -d' command is idempotent; it will start the 'db' if it's not running,
55
- # and do nothing if it already is.
56
- echo "Starting database service..."
57
- docker compose up -d db
53
+ # Step 2: Explicitly check and manage the database container.
54
+ # This avoids the 'docker compose up' conflict entirely.
55
+ DB_CONTAINER_NAME="codebuilder-postgres-db"
56
+ if [ $(docker ps -a -q -f name=^/${DB_CONTAINER_NAME}$) ]; then
57
+ # The container exists, check if it is running.
58
+ if [ $(docker ps -q -f name=^/${DB_CONTAINER_NAME}$) ]; then
59
+ echo "Database container '${DB_CONTAINER_NAME}' is already running."
60
+ else
61
+ # The container exists but is stopped, so start it.
62
+ echo "Database container '${DB_CONTAINER_NAME}' exists but is stopped. Starting it..."
63
+ docker start ${DB_CONTAINER_NAME}
64
+ fi
65
+ else
66
+ # The container does not exist, so create it for the first time.
67
+ echo "Database container '${DB_CONTAINER_NAME}' not found. Creating it..."
68
+ docker compose up -d db
69
+ fi
58
70
59
71
# Step 3: Wait for the database to be fully ready.
60
- # We use netcat (nc) to poll the database port exposed to the host machine.
61
- # This is critical to ensure the build doesn't start prematurely.
62
- # NOTE: We use port 5434 because that is what you mapped in your docker-compose.yml
63
72
echo "Waiting for database to become available on localhost:5434..."
64
73
while ! nc -z localhost 5434; do
65
- sleep 1 # wait for 1 second before trying again
74
+ sleep 1
66
75
done
67
76
echo "✅ Database is healthy and listening."
68
77
69
- # Step 4: NOW that the DB is running, build the webapp image.
70
- # The Docker builder will be able to connect to the 'db' service over the network.
71
- echo "Building the webapp image..."
72
- docker compose build webapp
73
-
74
- # Step 5: Deploy the newly built webapp container.
75
- # This command surgically replaces the webapp without touching the database.
76
- echo "Deploying the new webapp container..."
77
- docker compose up -d --no-deps --force-recreate webapp
78
+ # Step 4: Build and deploy the webapp.
79
+ # This part remains the same as it correctly targets only the webapp.
80
+ echo "Building and deploying the webapp..."
81
+ docker compose up -d --build --force-recreate --no-deps webapp
78
82
79
83
- name : ' 🗑 Prune Old Docker Images'
80
84
if : always()
0 commit comments