-
Notifications
You must be signed in to change notification settings - Fork 0
140 lines (118 loc) · 4.31 KB
/
docker-publish.yml
File metadata and controls
140 lines (118 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
name: Build, test and push Docker image
on:
push:
branches:
- master
jobs:
build-test-push:
runs-on: ubuntu-latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: app
POSTGRES_PORT: 5432
REDIS_PORT: 6379
API_PORT: 3000
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Start database and redis
run: |
docker compose up -d database redis
echo "Waiting for database to become healthy..."
for i in {1..30}; do
status=$(docker inspect --format='{{.State.Health.Status}}' database || echo "unknown")
echo "database health: $status"
if [ "$status" = "healthy" ]; then
echo "Database is healthy ✅"
break
fi
if [ "$status" = "unhealthy" ]; then
echo "Database became unhealthy ❌"
docker compose logs database
exit 1
fi
sleep 2
done
echo "Waiting a bit for redis..."
sleep 5
- name: Build Docker image (no push)
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: false
load: true
platforms: linux/amd64
tags: chauhansomay/nodejs-api:ci-${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Run API container for smoke test
run: |
# Dynamically detect the Docker Compose network
NETWORK_NAME=$(docker network ls --filter "name=app-network" --format "{{.Name}}" | head -n 1)
if [ -z "$NETWORK_NAME" ]; then
echo "Could not find app-network, listing available networks:"
docker network ls
exit 1
fi
echo "Using Docker network: $NETWORK_NAME"
# Run container with Docker health check - no arbitrary timeouts
docker run -d \
--name nodejs-api-test \
--network $NETWORK_NAME \
-p 3000:3000 \
-e NODE_ENV=production \
-e DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:${POSTGRES_PORT}/${POSTGRES_DB} \
-e REDIS_URL=redis://redis:${REDIS_PORT} \
--health-cmd="wget -qO- http://localhost:3000/health || exit 1" \
--health-interval=5s \
--health-timeout=3s \
--health-retries=60 \
--health-start-period=10s \
chauhansomay/nodejs-api:ci-${{ github.sha }}
echo "Waiting for API container to become healthy..."
while true; do
status=$(docker inspect --format='{{.State.Health.Status}}' nodejs-api-test 2>/dev/null || echo "unknown")
echo "API health: $status"
if [ "$status" = "healthy" ]; then
echo "API is healthy ✅"
break
fi
if [ "$status" = "unhealthy" ]; then
echo "API became unhealthy ❌, showing logs:"
docker logs nodejs-api-test || true
docker stop nodejs-api-test || true
docker compose logs database redis || true
exit 1
fi
# Check if container exited unexpectedly
if ! docker ps -q -f name=nodejs-api-test | grep -q .; then
echo "API container exited unexpectedly ❌"
docker logs nodejs-api-test || true
exit 1
fi
sleep 3
done
echo "Smoke test passed ✅"
docker stop nodejs-api-test
- name: Push image to Docker Hub
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
platforms: linux/amd64,linux/arm64
tags: chauhansomay/nodejs-api:latest
cache-from: type=gha
- name: Docker compose down
if: always()
run: |
docker compose down