Skip to content

Commit e56f809

Browse files
committed
added environment variable configuration in Dockerfile
1 parent 4f2b1b6 commit e56f809

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
name: Build, test and push Docker image
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
build-test-push:
10+
runs-on: ubuntu-latest
11+
12+
env:
13+
POSTGRES_USER: postgres
14+
POSTGRES_PASSWORD: postgres
15+
POSTGRES_DB: app
16+
POSTGRES_PORT: 5432
17+
REDIS_PORT: 6379
18+
API_PORT: 3000
19+
20+
steps:
21+
- name: Checkout repository
22+
uses: actions/checkout@v4
23+
24+
- name: Log in to Docker Hub
25+
uses: docker/login-action@v3
26+
with:
27+
username: ${{ secrets.DOCKERHUB_USERNAME }}
28+
password: ${{ secrets.DOCKERHUB_TOKEN }}
29+
30+
- name: Set up Docker Buildx
31+
uses: docker/setup-buildx-action@v3
32+
33+
- name: Start database and redis
34+
run: |
35+
docker compose up -d database redis
36+
37+
echo "Waiting for database to become healthy..."
38+
for i in {1..30}; do
39+
status=$(docker inspect --format='{{.State.Health.Status}}' database || echo "unknown")
40+
echo "database health: $status"
41+
if [ "$status" = "healthy" ]; then
42+
echo "Database is healthy ✅"
43+
break
44+
fi
45+
if [ "$status" = "unhealthy" ]; then
46+
echo "Database became unhealthy ❌"
47+
docker compose logs database
48+
exit 1
49+
fi
50+
sleep 2
51+
done
52+
53+
echo "Waiting a bit for redis..."
54+
sleep 5
55+
56+
- name: Set up QEMU for multi-arch builds
57+
uses: docker/setup-qemu-action@v3
58+
59+
- name: Build Docker image (no push)
60+
uses: docker/build-push-action@v5
61+
with:
62+
context: .
63+
file: ./Dockerfile
64+
push: false
65+
load: true
66+
platforms: linux/amd64
67+
tags: chauhansomay/nodejs-api:ci-${{ github.sha }}
68+
69+
- name: Run API container for smoke test
70+
run: |
71+
# Dynamically detect the Docker Compose network
72+
NETWORK_NAME=$(docker network ls --filter "name=app-network" --format "{{.Name}}" | head -n 1)
73+
74+
if [ -z "$NETWORK_NAME" ]; then
75+
echo "Could not find app-network, listing available networks:"
76+
docker network ls
77+
exit 1
78+
fi
79+
80+
echo "Using Docker network: $NETWORK_NAME"
81+
82+
docker run -d --rm \
83+
--name nodejs-api-test \
84+
--network $NETWORK_NAME \
85+
-p 3000:3000 \
86+
-e NODE_ENV=production \
87+
-e DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:${POSTGRES_PORT}/${POSTGRES_DB} \
88+
-e REDIS_URL=redis://redis:${REDIS_PORT} \
89+
chauhansomay/nodejs-api:ci-${{ github.sha }}
90+
91+
echo "Waiting for API container to become ready..."
92+
for i in {1..30}; do
93+
if curl -sf http://localhost:3000/ > /dev/null 2>&1; then
94+
echo "API is ready ✅"
95+
break
96+
fi
97+
echo "Attempt $i/30: API not ready yet, waiting..."
98+
if [ $i -eq 30 ]; then
99+
echo "Health check failed after 30 attempts ❌, showing logs:"
100+
docker logs nodejs-api-test || true
101+
docker stop nodejs-api-test || true
102+
docker compose logs database redis || true
103+
exit 1
104+
fi
105+
sleep 2
106+
done
107+
108+
echo "Health check passed ✅"
109+
docker stop nodejs-api-test
110+
111+
- name: Build and push multi-arch image to Docker Hub
112+
uses: docker/build-push-action@v5
113+
with:
114+
context: .
115+
file: ./Dockerfile
116+
push: true
117+
platforms: linux/amd64,linux/arm64
118+
tags: chauhansomay/nodejs-api:latest
119+
120+
- name: Docker compose down
121+
if: always()
122+
run: |
123+
docker compose down

Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ WORKDIR /app
55
ARG PORT
66
ARG DATABASE_URL
77

8+
ENV PORT=$PORT \
9+
DATABASE_URL=$DATABASE_URL
10+
811
COPY package.json yarn.lock ./
912
RUN yarn install --frozen-lockfile
1013

docker-compose.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
volumes:
2+
database-data:
3+
redis-data:
4+
5+
networks:
6+
app-network:
7+
driver: bridge
8+
9+
services:
10+
database:
11+
container_name: database
12+
image: postgres:latest
13+
restart: unless-stopped
14+
volumes:
15+
- database-data:/var/lib/postgresql
16+
environment:
17+
POSTGRES_USER: ${POSTGRES_USER:-postgres}
18+
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
19+
POSTGRES_DB: ${POSTGRES_DB:-app}
20+
networks:
21+
- app-network
22+
ports:
23+
- "${POSTGRES_PORT:-5432}:5432"
24+
healthcheck:
25+
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-postgres}"]
26+
interval: 10s
27+
timeout: 5s
28+
retries: 5
29+
30+
redis:
31+
container_name: redis
32+
image: redis:latest
33+
restart: unless-stopped
34+
volumes:
35+
- redis-data:/data
36+
networks:
37+
- app-network
38+
ports:
39+
- "${REDIS_PORT:-6379}:6379"
40+
healthcheck:
41+
test: ["CMD", "redis-cli", "ping"]
42+
interval: 10s
43+
timeout: 5s
44+
retries: 5
45+
46+
api:
47+
container_name: nodejs-api
48+
build:
49+
context: .
50+
dockerfile: Dockerfile
51+
restart: unless-stopped
52+
networks:
53+
- app-network
54+
ports:
55+
- "${API_PORT:-3000}:3000"
56+
environment:
57+
NODE_ENV: ${NODE_ENV:-production}
58+
DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@database:5432/${POSTGRES_DB:-app}
59+
REDIS_URL: redis://redis:6379
60+
depends_on:
61+
database:
62+
condition: service_healthy
63+
redis:
64+
condition: service_healthy

0 commit comments

Comments
 (0)