Skip to content

Commit 67403e3

Browse files
committed
Refactor CI workflow and Docker setup; enable health checks for services, restore user routes, and ensure database initialization on server start.
1 parent c694a0d commit 67403e3

5 files changed

Lines changed: 58 additions & 54 deletions

File tree

.github/workflows/ci_week7.yml

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,72 @@
1-
# GitHub Actions CI for Docker Compose (Week 7)
2-
3-
name: CI - Docker Compose Week 7
1+
name: End-to-End Tests
42

53
on:
6-
push:
7-
branches:
8-
- week7
9-
workflow_dispatch:
4+
push:
5+
branches: [week7]
106

117

128
jobs:
13-
e2e-tests:
9+
e2e:
10+
name: Run Docker Compose End-to-End Tests
1411
runs-on: ubuntu-latest
15-
services:
16-
docker:
17-
image: docker:24.0.5-dind
18-
options: --dns 8.8.8.8
12+
1913
steps:
20-
- name: Checkout code
21-
uses: actions/checkout@v3
14+
- name: Checkout repository
15+
uses: actions/checkout@v4
2216

23-
- name: Set up Docker Compose
17+
- name: Set up Docker Buildx
2418
uses: docker/setup-buildx-action@v3
2519

26-
- name: Install Docker Compose
27-
run: |
28-
sudo apt-get update
29-
sudo apt-get install -y docker-compose
20+
- name: Set up Docker cache
21+
uses: actions/cache@v4
22+
with:
23+
path: /tmp/.buildx-cache
24+
key: ${{ runner.os }}-buildx-${{ github.sha }}
25+
restore-keys: |
26+
${{ runner.os }}-buildx-
3027
31-
- name: Build and start services
32-
run: |
33-
docker-compose -f week7/docker-compose.yml up --build -d
28+
- name: Build Docker containers
29+
run: docker-compose build
3430

35-
- name: Get running containers
36-
run: |
37-
docker ps -a
31+
- name: Run Docker Compose (detached)
32+
run: docker-compose up -d
3833

39-
- name: Wait for backend healthcheck
34+
- name: Wait for services to be healthy
4035
run: |
41-
CONTAINER_ID=$(docker ps -qf "name=backend")
42-
echo "Backend container ID: $CONTAINER_ID"
43-
for i in {1..10}; do
44-
STATUS=$(docker inspect --format='{{json .State.Health.Status}}' $CONTAINER_ID || echo "none")
45-
echo "Backend health: $STATUS"
46-
if [ "$STATUS" = '"healthy"' ]; then exit 0; fi
47-
sleep 5
36+
set -e
37+
timeout=30
38+
echo "Waiting up to $timeout seconds for services to become healthy..."
39+
for i in $(seq 1 $timeout); do
40+
unhealthy=$(docker inspect --format='{{.State.Health.Status}}' $(docker-compose ps -q db || true) || echo "unhealthy")
41+
if [ "$unhealthy" == "healthy" ]; then
42+
echo "DB is healthy."
43+
break
44+
fi
45+
sleep 1
4846
done
49-
echo "Backend did not become healthy in time" && docker-compose -f week7/docker-compose.yml logs backend && exit 1
5047
51-
- name: Run end-to-end tests
48+
- name: Run health check and test endpoint
5249
run: |
53-
curl -f http://localhost:3000/health
50+
echo "Testing health endpoint..."
51+
curl --fail http://localhost:3000/health
5452
55-
- name: Show logs on failure
53+
echo "Testing user GET (expecting empty array or success)..."
54+
curl --fail http://localhost:3000/api/users
55+
56+
- name: Handle failed services
5657
if: failure()
5758
run: |
58-
docker-compose -f week7/docker-compose.yml logs > week7/compose-logs.txt || true
59+
echo "One or more steps failed. Showing logs..."
60+
docker-compose logs > full_logs.txt
61+
cat full_logs.txt
5962
6063
- name: Upload logs as artifact
6164
if: always()
6265
uses: actions/upload-artifact@v4
6366
with:
64-
name: compose-logs
65-
path: week7/compose-logs.txt
67+
name: docker-compose-logs
68+
path: full_logs.txt
6669

67-
- name: Tear down
70+
- name: Shut down containers
6871
if: always()
69-
run: |
70-
docker-compose -f week7/docker-compose.yml down -v
72+
run: docker-compose down --volumes

week7/backend/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
FROM node:20-alpine
22

3+
4+
RUN apk add --no-cache curl
5+
36
WORKDIR /app
47

58
COPY package*.json ./

week7/backend/src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ app.get('/health', (req, res) => {
1212
res.status(200).json({ status: 'UP' });
1313
});
1414

15-
// app.use('/api/users', userRoutes);
15+
app.use('/api/users', userRoutes);
1616

1717
export default app;

week7/backend/src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ async function startServerWithDbRetry(retries = 10, delayMs = 2000) {
3030
}
3131
}
3232

33-
// startServerWithDbRetry();
33+
startServerWithDbRetry();

week7/docker-compose.yml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ services:
1313
networks:
1414
- app-network
1515
healthcheck:
16-
test: ["CMD-SHELL", "pg_isready -U myuser -d mydb"]
16+
test: ["CMD-SHELL", "pg_isready -U myuser"]
1717
interval: 10s
1818
timeout: 5s
19-
retries: 5
20-
start_period: 30s # Increased from 10s
21-
19+
retries: 5
20+
2221
# pgadmin:
2322
# image: dpage/pgadmin4
2423
# environment:
@@ -36,19 +35,19 @@ services:
3635
environment:
3736
DATABASE_URL: postgres://myuser:mypassword@db:5432/mydb
3837
PORT: 3000
39-
# depends_on:
40-
# db:
41-
# condition: service_healthy
38+
depends_on:
39+
db:
40+
condition: service_healthy
4241
networks:
4342
- app-network
4443
volumes:
4544
- ./backend/.env:/app/.env
4645
healthcheck:
47-
test: ["CMD", "curl", "-v", "-f", "http://localhost:3000/health"] # Added -v for verbose output
46+
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
4847
interval: 10s
4948
timeout: 5s
5049
retries: 5
51-
start_period: 30s # Increased from 10s
50+
5251

5352
# frontend:
5453
# build:

0 commit comments

Comments
 (0)