Skip to content

Commit b64f105

Browse files
authored
workflow which ensures that talawa Api app starts in docker (#2759)
* added workflow * syntax errors * suggestion-1 * indentation 1 * indentation 2 * removing unions file * fixing tests * docker-compose issue * increasing timeout * test fix * using mongosh * idenation fix * pwd variable fix, redi-stack-server fix * health-check-api * api fix * removing unnecessary files * fixing replic-set issues * fixing workflow * identation * indentaion fix * merge 2 * idenation fix 2 * correction mongo build * suggestions * fixing build error * error fix * error fix * ai suggestions * added caddy service * suggestions * simpler config for replSet * env fix * suggestion * final fix * suggestions * last suggestions * sytnax error * line continuation * revert suggestions * db connection * eslint removal * fix failing tests
1 parent 3ec4fb9 commit b64f105

File tree

15 files changed

+282
-41
lines changed

15 files changed

+282
-41
lines changed

.env.sample

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ REFRESH_TOKEN_SECRET=
2424

2525
# This environment variable is used to provide connection string of the mongoDB
2626
# database for talawa-api to connect to.
27+
# Sample url for docker setup: mongodb://localhost:port/?replicaSet=rs0&directConnection=true
2728
MONGO_DB_URL=
2829

2930

@@ -104,4 +105,7 @@ MINIO_DATA_DIR=
104105

105106
# this environment variable is for setting the environment variable for Image Upload size
106107

107-
IMAGE_SIZE_LIMIT_KB=3000
108+
IMAGE_SIZE_LIMIT_KB=3000
109+
110+
# This environment variable is for setting the working directory when Docker is used
111+
PWD=.

.github/workflows/pull-request.yml

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,130 @@ jobs:
313313
with:
314314
path: './coverage/lcov.info'
315315
min_coverage: 95.0
316+
317+
Docker-Check:
318+
name: Docker Check
319+
needs: Test-Application
320+
runs-on: ubuntu-latest
321+
322+
steps:
323+
- name: Checkout repository
324+
uses: actions/checkout@v4
325+
326+
- name: Set up Node.js
327+
uses: actions/setup-node@v4
328+
with:
329+
node-version: '22.x'
330+
331+
- name: Cache Node.js dependencies
332+
uses: actions/cache@v4
333+
with:
334+
path: |
335+
~/.npm
336+
node_modules
337+
key: ${{ runner.os }}-docker-check-${{ hashFiles('**/package-lock.json') }}
338+
restore-keys: |
339+
${{ runner.os }}-docker-check-
340+
341+
- name: Install Docker Compose
342+
run: |
343+
sudo apt-get update
344+
sudo apt-get install -y docker-compose
345+
346+
- name: Check if Talawa API starts in Docker
347+
run: |
348+
# Ensure no containers are running
349+
docker-compose -f docker-compose.dev.yaml down -v || true
350+
351+
# Verify docker-compose file exists
352+
chmod +x .github/workflows/scripts/docker_file_check.sh
353+
./.github/workflows/scripts/docker_file_check.sh
354+
355+
# Start containers
356+
if ! timeout 300 docker-compose -f docker-compose.dev.yaml up -d --build; then
357+
echo "Failed to start containers"
358+
docker-compose -f docker-compose.dev.yaml logs
359+
exit 1
360+
fi
361+
362+
# Wait for MongoDB and Redis to be ready
363+
echo "Waiting for MongoDB..."
364+
timeout=30
365+
until docker-compose -f docker-compose.dev.yaml exec -T mongo mongosh --eval "db.runCommand({ ping: 1 }).ok">/dev/null 2>&1 || [ $timeout -eq 0 ]; do
366+
echo "Waiting for MongoDB to be ready..."
367+
sleep 1
368+
((timeout--))
369+
done
370+
if [ $timeout -eq 0 ]; then
371+
echo "Error: MongoDB failed to start within timeout"
372+
echo "Fetching MongoDB logs..."
373+
docker-compose -f docker-compose.dev.yaml logs mongodb
374+
echo "Shutting down MongoDB..."
375+
docker-compose -f docker-compose.dev.yaml down -v
376+
exit 1
377+
else
378+
echo "MongoDB is ready!"
379+
fi
380+
381+
echo "Waiting for Redis..."
382+
timeout=30
383+
until docker-compose -f docker-compose.dev.yaml exec -T redis-stack-server redis-cli ping >/dev/null 2>&1 || [ $timeout -eq 0 ]; do
384+
sleep 1
385+
((timeout--))
386+
done
387+
if [ $timeout -eq 0 ]; then
388+
echo "Error: Redis failed to start within timeout"
389+
echo "Fetching Redis logs..."
390+
docker-compose -f docker-compose.dev.yaml logs redis-stack-server
391+
echo "Shutting down Redis..."
392+
docker-compose -f docker-compose.dev.yaml down -v
393+
exit 1
394+
else
395+
echo "Redis is ready!"
396+
fi
397+
398+
# Wait for TALAWA API to be healthy
399+
timeout=60
400+
until docker-compose -f docker-compose.dev.yaml exec -T talawa-api-dev curl -v -X OPTIONS "http://talawa-api-dev:4000/graphql" 2>&1 || [ $timeout -eq 0 ]; do
401+
echo "Waiting for API to start... ($timeout seconds remaining)"
402+
sleep 1
403+
((timeout--))
404+
done
405+
406+
if [ $timeout -eq 0 ]; then
407+
echo "Error: API failed to start within timeout"
408+
docker-compose -f docker-compose.dev.yaml logs
409+
docker-compose -f docker-compose.dev.yaml down -v
410+
exit 1
411+
fi
412+
413+
echo "API started successfully"
414+
415+
# Ensure cleanup runs even if the script fails
416+
cleanup() {
417+
local exit_code=$?
418+
echo "Cleaning up containers..."
419+
if ! docker-compose -f docker-compose.dev.yaml down -v; then
420+
echo "Warning: Failed to cleanup containers"
421+
fi
422+
exit $exit_code
423+
}
424+
trap cleanup EXIT
425+
env:
426+
HEALTH_CHECK_URL: http://localhost:4000
427+
COMPOSE_PROJECT_NAME: pr-${{ github.event.pull_request.number }}
428+
MONGO_DB_URL: mongodb://mongo:27017?replicaSet=rs0
429+
REDIS_HOST: redis-stack-server
430+
REDIS_PORT: 6379
431+
ACCESS_TOKEN_SECRET: ${{ secrets.GITHUB_TOKEN }}_${{ github.run_id }}_${{ github.run_number }}
432+
REFRESH_TOKEN_SECRET: ${{ secrets.GITHUB_TOKEN }}_${{ github.run_id }}_${{ github.run_attempt }}
433+
LAST_RESORT_SUPERADMIN_EMAIL: "[email protected]"
434+
COLORIZE_LOGS: "true"
435+
LOG_LEVEL: "info"
436+
RECAPTCHA_SITE_KEY: ${{secrets.RECAPTCHA_SITE_KEY}}
437+
RECAPTCHA_SECRET_KEY: ${{secrets.RECAPTCHA_SECRET_KEY}}
438+
MAIL_USERNAME: ${{secrets.MAIL_USERNAME}}
439+
MAIL_PASSWORD: ${{secrets.MAIL_PASSWORD}}
316440

317441
Test-Builds:
318442
name: Test Development and Production Builds
@@ -347,7 +471,7 @@ jobs:
347471
LAST_RESORT_SUPERADMIN_EMAIL: "[email protected]"
348472
COLORIZE_LOGS: "true"
349473
LOG_LEVEL: "info"
350-
474+
351475
steps:
352476
- name: Checkout repository
353477
uses: actions/checkout@v4
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
3+
4+
if ! command -v docker-compose &> /dev/null; then
5+
echo "Error: docker-compose is not installed"
6+
exit 1
7+
fi
8+
9+
docker-compose -f "${REPO_ROOT}/docker-compose.dev.yaml" down -v || {
10+
echo "Warning: Failed to stop containers. Continuing anyway..."
11+
}
12+
13+
echo "docker-compose.dev.yaml file found. Ready to proceed."

docker-compose.dev.yaml

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
version: "3.8"
2-
31
services:
4-
mongodb:
2+
mongo:
53
image: mongo:latest
4+
container_name: mongo
5+
restart: unless-stopped
6+
healthcheck:
7+
test: mongosh --eval "try { rs.status().ok } catch (e) { 0 }"
8+
interval: 30s
9+
timeout: 5s
10+
retries: 30
11+
deploy:
12+
resources:
13+
limits:
14+
memory: 1G
615
ports:
7-
- 27017:27017
16+
- "127.0.0.1:27017:27017"
817
volumes:
9-
- mongodb-data:/data/db
18+
- mongo_data:/data/db
19+
- ./init-mongo.sh:/init-mongo.sh
1020
networks:
1121
- talawa-network
22+
entrypoint: ["/bin/bash", "/init-mongo.sh"]
1223

1324
redis-stack-server:
1425
image: redis/redis-stack-server:latest
@@ -24,7 +35,7 @@ services:
2435
ports:
2536
- "9000:9000"
2637
- "9001:9001"
27-
environment:
38+
environment:
2839
- MINIO_ROOT_USER=${MINIO_ROOT_USER}
2940
- MINIO_ROOT_PASSWORD=${MINIO_ROOT_PASSWORD}
3041
command: server /data --console-address ":9001"
@@ -37,18 +48,25 @@ services:
3748
build:
3849
context: .
3950
dockerfile: Dockerfile.dev
51+
container_name: talawa-api-dev
52+
ports:
53+
- "4000:4000"
4054
volumes:
4155
- .:/usr/src/app
4256
- /usr/src/app/node_modules
4357
depends_on:
44-
- mongodb
45-
- redis-stack-server
46-
- minio
58+
mongo:
59+
condition: service_healthy
60+
redis-stack-server:
61+
condition: service_started
62+
minio:
63+
condition: service_started
4764
environment:
48-
- MONGO_DB_URL=mongodb://mongodb:27017
65+
- MONGO_DB_URL=mongodb://mongo:27017/talawa-api?replicaSet=rs0&directConnection=true
4966
- REDIS_HOST=redis-stack-server
5067
- REDIS_PORT=6379
51-
68+
networks:
69+
- talawa-network
5270
caddy:
5371
image: caddy/caddy:2.2.1-alpine
5472
container_name: caddy-service
@@ -61,13 +79,14 @@ services:
6179
- $PWD/site:/srv
6280
- caddy_data:/data
6381
- caddy_config:/config
82+
6483

6584
volumes:
66-
mongodb-data:
6785
redis-data:
86+
minio-data:
87+
mongo_data:
6888
caddy_data:
6989
caddy_config:
70-
minio-data:
7190

7291
networks:
7392
talawa-network:

init-mongo.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Handle cleanup on script exit
5+
cleanup() {
6+
echo "Shutting down MongoDB..."
7+
mongod --shutdown
8+
exit 0
9+
}
10+
trap cleanup SIGTERM SIGINT
11+
12+
mongod --replSet rs0 --bind_ip_all --dbpath /data/db &
13+
MONGOD_PID=$!
14+
15+
# Wait for MongoDB to be ready
16+
MAX_TRIES=30
17+
COUNTER=0
18+
echo "Waiting for MongoDB to start..."
19+
until mongosh --eval "db.adminCommand('ping')" >/dev/null 2>&1; do
20+
if [ $COUNTER -gt $MAX_TRIES ]; then
21+
echo "Error: MongoDB failed to start"
22+
exit 1
23+
fi
24+
let COUNTER=COUNTER+1
25+
sleep 1
26+
done
27+
28+
# Initialize the replica set
29+
mongosh --eval '
30+
config = {
31+
"_id" : "rs0",
32+
"members" : [
33+
{
34+
"_id" : 0,
35+
"host" : "mongo:27017",
36+
"priority": 1
37+
}
38+
]
39+
};
40+
41+
while (true) {
42+
try {
43+
rs.initiate(config);
44+
break;
45+
} catch (err) {
46+
print("Failed to initiate replica set, retrying in 5 seconds...");
47+
sleep(5000);
48+
}
49+
}
50+
'
51+
52+
# Keep container running
53+
wait $MONGOD_PID

sample_data/users.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"state": "State"
2222
},
2323
"email": "[email protected]",
24-
"password": "$2a$12$bSYpay6TRMpTOaAmYPFXku4avwmqfFBtmgg39TabxmtFEiz4plFtW",
24+
"password": "$2a$12$gawMkattI6b.HAAwW7khxOJNzGZ2/N6YFe387/2yG8Z0G6tKVtv2y",
2525
"image": null,
2626
"createdAt": "2023-04-13T04:53:17.742Z",
2727
"__v": 0

0 commit comments

Comments
 (0)