forked from supercharge/mongodb-github-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart-mongodb.sh
48 lines (38 loc) · 1.22 KB
/
start-mongodb.sh
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
#!/bin/sh
# Map input values from the GitHub Actions workflow to shell variables
MONGODB_VERSION=$1
MONGODB_REPLICA_SET=$2
if [ -z "$MONGODB_VERSION" ]; then
echo "Missing MongoDB version in the [mongodb-version] input. Received value: $MONGODB_VERSION"
exit 2
fi
if [ -z "$MONGODB_REPLICA_SET" ]; then
echo "Starting single-node instance, no replica set"
docker run --name mongodb --publish 27017:27017 --detach mongo:$MONGODB_VERSION
return
fi
echo "Starting as single-node replica set (in replica set [$MONGODB_REPLICA_SET])"
docker run --name mongodb --publish 27017:27017 --detach mongo:$MONGODB_VERSION mongod --replSet $MONGODB_REPLICA_SET
echo "Waiting for MongoDB to accept connections"
TIMER=0
until docker exec --tty mongodb mongo --eval "db.serverStatus()"
do
sleep 1
echo "."
TIMER=$((TIMER + 1))
if [[ $TIMER -eq 20 ]]; then
echo "MongoDB did not initialize within 20 seconds. Exiting."
exit 2
fi
done
echo "Initiating replica set [$MONGODB_REPLICA_SET]"
docker exec --tty mongodb mongo --eval "
rs.initiate({
\"_id\": \"$MONGODB_REPLICA_SET\",
\"members\": [ {
\"_id\": 0,
\"host\": \"localhost\"
} ]
})
"
echo "Check! Initiated replica set [$MONGODB_REPLICA_SET]"