Skip to content

Commit 6eb5cc3

Browse files
authored
Add github workflow to validate docker compose (#61)
* Add github workflow to validate docker compose and check services are healthy * Add checks to the docker compose test github workflow - add a github workflow step after waiting for the health check, to check all services are running and didnt exit unexpectedly. - add a github workflow step to check for error logs.
1 parent a105e19 commit 6eb5cc3

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: Docker Compose Validation
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [ main ]
7+
8+
jobs:
9+
validate-compose:
10+
name: Spin up and test health of services
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout repository
15+
uses: actions/checkout@v4
16+
17+
- name: Set up Docker Buildx
18+
uses: docker/setup-buildx-action@v3
19+
20+
- name: Start Docker Compose stack
21+
run: |
22+
docker compose up -d --build
23+
24+
- name: Wait for services with health checks to become healthy
25+
run: |
26+
timeout 180 bash -c '
27+
while true; do
28+
unhealthy=$(docker compose ps --format "{{.Service}} {{.Health}}" | awk '\''$2 != "healthy" && $2 != "" {print $1}'\'')
29+
if [ -z "$unhealthy" ]; then
30+
echo "✅ All services with health checks are healthy!"
31+
break
32+
fi
33+
echo "Still waiting for unhealthy services:"
34+
echo "$unhealthy"
35+
sleep 5
36+
done
37+
'
38+
39+
- name: Check that services are running.
40+
run: |
41+
errors=0
42+
docker compose ps --all --format "{{.Name}} {{.Service}} {{.State}}" | while read -r line; do
43+
name=$(echo "$line" | awk '{print $1}')
44+
service=$(echo "$line" | awk '{print $2}')
45+
state=$(echo "$line" | awk '{print $3}')
46+
health=$(echo "$line" | awk '{print $4}')
47+
48+
if [ "$state" = "running" ]; then
49+
if [ "$health" = "unhealthy" ]; then
50+
echo "❌ $service ($name) is running but unhealthy: $health"
51+
errors=$((errors+1))
52+
elif [ "$health" = "healthy" ]; then
53+
echo "🟢 $service ($name) is running, health: $health"
54+
else
55+
echo "🟢 $service ($name) is running, health: no health check"
56+
fi
57+
elif [ "$state" = "exited" ]; then
58+
echo "⚪ $service ($name) has exited normally (exit 0)"
59+
else
60+
echo "❌ $service ($name) failed or exited unexpectedly: state=$state, health=$health"
61+
docker compose ps --all
62+
errors=$((errors+1))
63+
fi
64+
done
65+
66+
if [ "$errors" -gt 0 ]; then
67+
echo "❌ $errors service(s) failed or unhealthy"
68+
exit 1
69+
else
70+
echo "✅ All services are running or exited normally"
71+
fi
72+
73+
- name: Check errors inside service logs
74+
run: docker compose logs --no-color | grep -v "uvicorn\.error" | grep -i "error" && exit 1 || echo "No errors detected."
75+
76+
- name: Tear down Docker Compose
77+
if: always()
78+
run: docker compose down -v

0 commit comments

Comments
 (0)