Skip to content

Commit

Permalink
Merge pull request #51 from supercharge/wait-for-mongodb
Browse files Browse the repository at this point in the history
Wait for Single Instance MongoDB
  • Loading branch information
marcuspoehls committed Aug 7, 2023
2 parents fe0ae6b + b092ce4 commit 434c468
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 43 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# Changelog


## [1.10.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.9.0...v1.10.0) - 2023-08-07

### Added
- MongoDB single instance: wait for MongoDB to be ready

### Updated
- bump dependencies


## [1.9.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.8.0...v1.9.0) - 2023-02-10

### Added
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
node-version: [18.x, 20.x]
mongodb-version: ['4.2', '4.4', '5.0', '6.0']

steps:
Expand All @@ -58,7 +58,7 @@ jobs:
node-version: ${{ matrix.node-version }}

- name: Start MongoDB
uses: supercharge/mongodb-github-action@1.8.0
uses: supercharge/mongodb-github-action@1.10.0
with:
mongodb-version: ${{ matrix.mongodb-version }}

Expand All @@ -85,7 +85,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
node-version: [18.x, 20.x]
mongodb-version: ['4.2', '4.4', '5.0', '6.0']

steps:
Expand All @@ -98,7 +98,7 @@ jobs:
node-version: ${{ matrix.node-version }}

- name: Start MongoDB
uses: supercharge/mongodb-github-action@1.8.0
uses: supercharge/mongodb-github-action@1.10.0
with:
mongodb-version: ${{ matrix.mongodb-version }}
mongodb-replica-set: test-rs
Expand Down Expand Up @@ -129,7 +129,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
node-version: [18.x, 20.x]
mongodb-version: ['4.2', '4.4', '5.0', '6.0']

steps:
Expand All @@ -142,7 +142,7 @@ jobs:
node-version: ${{ matrix.node-version }}

- name: Start MongoDB
uses: supercharge/mongodb-github-action@1.8.0
uses: supercharge/mongodb-github-action@1.10.0
with:
mongodb-version: ${{ matrix.mongodb-version }}
mongodb-replica-set: test-rs
Expand Down Expand Up @@ -173,7 +173,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
node-version: [18.x, 20.x]
mongodb-version: ['4.2', '4.4', '5.0', '6.0']

steps:
Expand All @@ -186,7 +186,7 @@ jobs:
node-version: ${{ matrix.node-version }}

- name: Start MongoDB
uses: supercharge/mongodb-github-action@1.8.0
uses: supercharge/mongodb-github-action@1.10.0
with:
mongodb-version: ${{ matrix.mongodb-version }}
mongodb-username: supercharge
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
"url": "https://github.com/supercharge/mongodb-github-action/issues"
},
"devDependencies": {
"@supercharge/eslint-config": "~1.1.2",
"c8": "~7.12.0",
"eslint": "~8.33.0",
"expect": "~29.4.2",
"mongoose": "~6.9.1",
"@supercharge/eslint-config": "~2.0.0",
"c8": "~8.0.1",
"eslint": "~8.46.0",
"expect": "~29.6.2",
"mongoose": "~7.4.2",
"uvu": "~0.5.6"
},
"engines": {
Expand Down
75 changes: 47 additions & 28 deletions start-mongodb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ MONGODB_DB=$4
MONGODB_USERNAME=$5
MONGODB_PASSWORD=$6

# `mongosh` is used starting from MongoDB 5.x
MONGODB_CLIENT="mongosh --quiet"


if [ -z "$MONGODB_VERSION" ]; then
echo ""
Expand All @@ -20,15 +23,48 @@ fi

echo "::group::Selecting correct MongoDB client"
if [ "`echo $MONGODB_VERSION | cut -c 1`" = "4" ]; then
MONGO_CLIENT="mongo"
else
MONGO_CLIENT="mongosh --quiet"
MONGODB_CLIENT="mongo"
fi
echo " - Using [$MONGO_CLIENT]"
echo " - Using MongoDB client: [$MONGODB_CLIENT]"
echo ""
echo "::endgroup::"


# Helper function to wait for MongoDB to be started before moving on
wait_for_mongodb () {
echo "::group::Waiting for MongoDB to accept connections"
sleep 1
TIMER=0

MONGODB_ARGS=""

if [ -z "$MONGODB_REPLICA_SET" ]
then
if [ -z "$MONGODB_USERNAME" ]
then
MONGODB_ARGS=""
else
# no replica set, but username given: use them as args
MONGODB_ARGS="--username $MONGODB_USERNAME --password $MONGODB_PASSWORD"
fi
fi

# until ${WAIT_FOR_MONGODB_COMMAND}
until docker exec --tty mongodb $MONGODB_CLIENT --port $MONGODB_PORT $MONGODB_ARGS --eval "db.serverStatus()"
do
echo "."
sleep 1
TIMER=$((TIMER + 1))

if [[ $TIMER -eq 20 ]]; then
echo "MongoDB did not initialize within 20 seconds. Exiting."
exit 2
fi
done
echo "::endgroup::"
}


if [ -z "$MONGODB_REPLICA_SET" ]; then
echo "::group::Starting single-node instance, no replica set"
echo " - port [$MONGODB_PORT]"
Expand All @@ -37,15 +73,17 @@ if [ -z "$MONGODB_REPLICA_SET" ]; then
echo " - credentials [$MONGODB_USERNAME:$MONGODB_PASSWORD]"
echo ""

docker run --name mongodb --publish $MONGODB_PORT:27017 -e MONGO_INITDB_DATABASE=$MONGODB_DB -e MONGO_INITDB_ROOT_USERNAME=$MONGODB_USERNAME -e MONGO_INITDB_ROOT_PASSWORD=$MONGODB_PASSWORD --detach mongo:$MONGODB_VERSION
docker run --name mongodb --publish $MONGODB_PORT:$MONGODB_PORT -e MONGO_INITDB_DATABASE=$MONGODB_DB -e MONGO_INITDB_ROOT_USERNAME=$MONGODB_USERNAME -e MONGO_INITDB_ROOT_PASSWORD=$MONGODB_PASSWORD --detach mongo:$MONGODB_VERSION --port $MONGODB_PORT

if [ $? -ne 0 ]; then
echo "Error starting MongoDB Docker container"
exit 2
fi
echo "::endgroup::"

return
wait_for_mongodb

exit 0
fi


Expand All @@ -63,28 +101,11 @@ if [ $? -ne 0 ]; then
fi
echo "::endgroup::"


echo "::group::Waiting for MongoDB to accept connections"
sleep 1
TIMER=0

until docker exec --tty mongodb $MONGO_CLIENT --port $MONGODB_PORT --eval "db.serverStatus()" # &> /dev/null
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 "::endgroup::"

wait_for_mongodb

echo "::group::Initiating replica set [$MONGODB_REPLICA_SET]"

docker exec --tty mongodb $MONGO_CLIENT --port $MONGODB_PORT --eval "
docker exec --tty mongodb $MONGODB_CLIENT --port $MONGODB_PORT --eval "
rs.initiate({
\"_id\": \"$MONGODB_REPLICA_SET\",
\"members\": [ {
Expand All @@ -99,7 +120,5 @@ echo "::endgroup::"


echo "::group::Checking replica set status [$MONGODB_REPLICA_SET]"
docker exec --tty mongodb $MONGO_CLIENT --port $MONGODB_PORT --eval "
rs.status()
"
docker exec --tty mongodb $MONGODB_CLIENT --port $MONGODB_PORT --eval "rs.status()"
echo "::endgroup::"
2 changes: 1 addition & 1 deletion test/custom-port/custom-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { test } = require('uvu')
const Mongoose = require('mongoose')

test('connects to MongoDB on custom port 12345', async () => {
const connection = await Mongoose.createConnection('mongodb://localhost:12345').asPromise()
const connection = await Mongoose.createConnection('mongodb://localhost:12345')
await connection.close()
})

Expand Down
2 changes: 1 addition & 1 deletion test/single-instance/single-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test('connects to MongoDB', async () => {
pass: MONGODB_PASSWORD,
dbName: MONGODB_DB,
authSource: MONGODB_USERNAME && MONGODB_PASSWORD ? 'admin' : undefined
}).asPromise()
})

await connection.close()
})
Expand Down

0 comments on commit 434c468

Please sign in to comment.