From 92a11b4fc4c9dee430327b48c2a1a55ec42b2b27 Mon Sep 17 00:00:00 2001 From: Alexander Lukens Date: Tue, 12 Mar 2024 15:16:04 -0400 Subject: [PATCH 01/10] added option for container name --- action.yml | 8 +++++++- start-mongodb.sh | 9 +++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/action.yml b/action.yml index 7ecca50..e7427bf 100644 --- a/action.yml +++ b/action.yml @@ -36,6 +36,11 @@ inputs: required: false default: '' + container-name: + description: 'MongoDB container name (default: "mongodb")' + required: false + default: "mongodb" + runs: using: 'docker' image: 'Dockerfile' @@ -45,4 +50,5 @@ runs: - ${{ inputs.mongodb-port }} - ${{ inputs.mongodb-db }} - ${{ inputs.mongodb-username }} - - ${{ inputs.mongodb-password }} \ No newline at end of file + - ${{ inputs.mongodb-password }} + - ${{ inputs.container-name }} \ No newline at end of file diff --git a/start-mongodb.sh b/start-mongodb.sh index 79a0c47..554dde4 100644 --- a/start-mongodb.sh +++ b/start-mongodb.sh @@ -7,6 +7,7 @@ MONGODB_PORT=$3 MONGODB_DB=$4 MONGODB_USERNAME=$5 MONGODB_PASSWORD=$6 +MONGODB_CONTAINER_NAME=$6 # `mongosh` is used starting from MongoDB 5.x MONGODB_CLIENT="mongosh --quiet" @@ -73,7 +74,7 @@ if [ -z "$MONGODB_REPLICA_SET" ]; then echo " - credentials [$MONGODB_USERNAME:$MONGODB_PASSWORD]" echo "" - 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 + docker run --name $MONGODB_CONTAINER_NAME --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" @@ -93,7 +94,7 @@ echo " - version [$MONGODB_VERSION]" echo " - replica set [$MONGODB_REPLICA_SET]" echo "" -docker run --name mongodb --publish $MONGODB_PORT:$MONGODB_PORT --detach mongo:$MONGODB_VERSION --replSet $MONGODB_REPLICA_SET --port $MONGODB_PORT +docker run --name $MONGODB_CONTAINER_NAME --publish $MONGODB_PORT:$MONGODB_PORT --detach mongo:$MONGODB_VERSION --replSet $MONGODB_REPLICA_SET --port $MONGODB_PORT if [ $? -ne 0 ]; then echo "Error starting MongoDB Docker container" @@ -105,7 +106,7 @@ wait_for_mongodb echo "::group::Initiating replica set [$MONGODB_REPLICA_SET]" -docker exec --tty mongodb $MONGODB_CLIENT --port $MONGODB_PORT --eval " +docker exec --tty $MONGODB_CONTAINER_NAME $MONGODB_CLIENT --port $MONGODB_PORT --eval " rs.initiate({ \"_id\": \"$MONGODB_REPLICA_SET\", \"members\": [ { @@ -120,5 +121,5 @@ echo "::endgroup::" echo "::group::Checking replica set status [$MONGODB_REPLICA_SET]" -docker exec --tty mongodb $MONGODB_CLIENT --port $MONGODB_PORT --eval "rs.status()" +docker exec --tty $MONGODB_CONTAINER_NAME $MONGODB_CLIENT --port $MONGODB_PORT --eval "rs.status()" echo "::endgroup::" From 095a7d4395267a946807921a8c962ab0b59e02d0 Mon Sep 17 00:00:00 2001 From: Alexander Lukens Date: Tue, 12 Mar 2024 15:21:54 -0400 Subject: [PATCH 02/10] forgot to increment number for mongodb container name --- start-mongodb.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/start-mongodb.sh b/start-mongodb.sh index 554dde4..f4441bb 100644 --- a/start-mongodb.sh +++ b/start-mongodb.sh @@ -7,7 +7,7 @@ MONGODB_PORT=$3 MONGODB_DB=$4 MONGODB_USERNAME=$5 MONGODB_PASSWORD=$6 -MONGODB_CONTAINER_NAME=$6 +MONGODB_CONTAINER_NAME=$7 # `mongosh` is used starting from MongoDB 5.x MONGODB_CLIENT="mongosh --quiet" @@ -72,6 +72,7 @@ if [ -z "$MONGODB_REPLICA_SET" ]; then echo " - version [$MONGODB_VERSION]" echo " - database [$MONGODB_DB]" echo " - credentials [$MONGODB_USERNAME:$MONGODB_PASSWORD]" + echo " - container-name [$MONGODB_CONTAINER_NAME]" echo "" docker run --name $MONGODB_CONTAINER_NAME --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 @@ -94,7 +95,7 @@ echo " - version [$MONGODB_VERSION]" echo " - replica set [$MONGODB_REPLICA_SET]" echo "" -docker run --name $MONGODB_CONTAINER_NAME --publish $MONGODB_PORT:$MONGODB_PORT --detach mongo:$MONGODB_VERSION --replSet $MONGODB_REPLICA_SET --port $MONGODB_PORT +docker run --name $MONGODB_CONTAINER_NAME --publish $MONGODB_PORT:$MONGODB_PORT --detach mongo:$MONGODB_VERSION --replSet $MONGODB_REPLICA_SET if [ $? -ne 0 ]; then echo "Error starting MongoDB Docker container" From fa13afeb0a920f19491292b6819a6129c09fb42b Mon Sep 17 00:00:00 2001 From: Alexander Lukens Date: Tue, 12 Mar 2024 15:25:49 -0400 Subject: [PATCH 03/10] missed docker exec container name --- start-mongodb.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/start-mongodb.sh b/start-mongodb.sh index f4441bb..b9bb86a 100644 --- a/start-mongodb.sh +++ b/start-mongodb.sh @@ -51,7 +51,7 @@ wait_for_mongodb () { fi # until ${WAIT_FOR_MONGODB_COMMAND} - until docker exec --tty mongodb $MONGODB_CLIENT --port $MONGODB_PORT $MONGODB_ARGS --eval "db.serverStatus()" + until docker exec --tty $MONGODB_CONTAINER_NAME $MONGODB_CLIENT --port $MONGODB_PORT $MONGODB_ARGS --eval "db.serverStatus()" do echo "." sleep 1 From 6f3be9b57486016b58de65da801254aec4fe8454 Mon Sep 17 00:00:00 2001 From: Alexander Lukens Date: Tue, 12 Mar 2024 15:34:45 -0400 Subject: [PATCH 04/10] did not format my string the way the rest of the file is. Corrected --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index e7427bf..500eed1 100644 --- a/action.yml +++ b/action.yml @@ -39,7 +39,7 @@ inputs: container-name: description: 'MongoDB container name (default: "mongodb")' required: false - default: "mongodb" + default: 'mongodb' runs: using: 'docker' From d78991640412321cca957933284a27c97c1e9b73 Mon Sep 17 00:00:00 2001 From: Alexander Lukens Date: Tue, 12 Mar 2024 15:51:07 -0400 Subject: [PATCH 05/10] add typing for container-name to action-types.yml --- action-types.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/action-types.yml b/action-types.yml index 11bbc12..3171fce 100644 --- a/action-types.yml +++ b/action-types.yml @@ -12,3 +12,5 @@ inputs: type: string mongodb-password: type: string + container-name: + type: string \ No newline at end of file From 5548f2ad79d084e1e228a82452910682d7f79871 Mon Sep 17 00:00:00 2001 From: Alexander Lukens Date: Tue, 19 Mar 2024 09:39:45 -0400 Subject: [PATCH 06/10] want to remove conflicting container, if it exists, before attempting to start a new container --- start-mongodb.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/start-mongodb.sh b/start-mongodb.sh index b9bb86a..147322e 100644 --- a/start-mongodb.sh +++ b/start-mongodb.sh @@ -95,6 +95,12 @@ echo " - version [$MONGODB_VERSION]" echo " - replica set [$MONGODB_REPLICA_SET]" echo "" +# check if the container already exists and remove it +if [ "$(docker ps -q -f name=$MONGODB_CONTAINER_NAME)" ]; then + echo "Removing existing container [$MONGODB_CONTAINER_NAME]" + docker rm -f $MONGODB_CONTAINER_NAME +fi + docker run --name $MONGODB_CONTAINER_NAME --publish $MONGODB_PORT:$MONGODB_PORT --detach mongo:$MONGODB_VERSION --replSet $MONGODB_REPLICA_SET if [ $? -ne 0 ]; then From cfd62412ca92cd39081beae147ffa8afa9592a4b Mon Sep 17 00:00:00 2001 From: Alexander Lukens Date: Tue, 19 Mar 2024 09:47:11 -0400 Subject: [PATCH 07/10] check for existing docker container needs to be earlier, as there are multiple docker run commands --- start-mongodb.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/start-mongodb.sh b/start-mongodb.sh index 147322e..cd09243 100644 --- a/start-mongodb.sh +++ b/start-mongodb.sh @@ -65,6 +65,11 @@ wait_for_mongodb () { echo "::endgroup::" } +# check if the container already exists and remove it +if [ "$(docker ps -q -f name=$MONGODB_CONTAINER_NAME)" ]; then + echo "Removing existing container [$MONGODB_CONTAINER_NAME]" + docker rm -f $MONGODB_CONTAINER_NAME +fi if [ -z "$MONGODB_REPLICA_SET" ]; then echo "::group::Starting single-node instance, no replica set" @@ -95,11 +100,6 @@ echo " - version [$MONGODB_VERSION]" echo " - replica set [$MONGODB_REPLICA_SET]" echo "" -# check if the container already exists and remove it -if [ "$(docker ps -q -f name=$MONGODB_CONTAINER_NAME)" ]; then - echo "Removing existing container [$MONGODB_CONTAINER_NAME]" - docker rm -f $MONGODB_CONTAINER_NAME -fi docker run --name $MONGODB_CONTAINER_NAME --publish $MONGODB_PORT:$MONGODB_PORT --detach mongo:$MONGODB_VERSION --replSet $MONGODB_REPLICA_SET From 8d73f6349c1f11b9b1ea9cc8c5bf5f776cdf2f10 Mon Sep 17 00:00:00 2001 From: Alexander Lukens Date: Fri, 5 Apr 2024 12:06:54 -0400 Subject: [PATCH 08/10] increase timer to 40 seconds, for testing workflow timeout issue --- start-mongodb.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/start-mongodb.sh b/start-mongodb.sh index cd09243..7185bdb 100644 --- a/start-mongodb.sh +++ b/start-mongodb.sh @@ -57,8 +57,8 @@ wait_for_mongodb () { sleep 1 TIMER=$((TIMER + 1)) - if [[ $TIMER -eq 20 ]]; then - echo "MongoDB did not initialize within 20 seconds. Exiting." + if [[ $TIMER -eq 40 ]]; then + echo "MongoDB did not initialize within 40 seconds. Exiting." exit 2 fi done From 17cabbc881ada33a5e807191612ed219cd9cdd24 Mon Sep 17 00:00:00 2001 From: Alexander Lukens Date: Fri, 5 Apr 2024 12:07:16 -0400 Subject: [PATCH 09/10] add example usage for container-name to the readme --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/README.md b/README.md index cb795a5..768f468 100644 --- a/README.md +++ b/README.md @@ -202,6 +202,48 @@ jobs: CI: true ``` +### With Custom Container Name +The container name of the created MongoDB instance can be configured using the "container-name" input + +The following example will parameterize the MongoDB container name based on the node and mongodb versions being used: + +```yaml +name: Run with Custom Container Names + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + node-version: [18.x, 20.x] + mongodb-version: ['4.2', '4.4', '5.0', '6.0'] + + steps: + - name: Git checkout + uses: actions/checkout@v3 + + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + + - name: Start MongoDB + uses: supercharge/mongodb-github-action@1.10.0 + with: + mongodb-version: ${{ matrix.mongodb-version }} + container-name: mongodb-${{ matrix.node-version }}-${{ matrix.mongodb-version }} + + - name: Install dependencies + run: npm install + + - name: Run tests + run: npm test + env: + CI: true +``` + **Caveat:** due to [this issue](https://github.com/docker-library/mongo/issues/211), you **cannot enable user creation AND replica sets** initially. Therefore, if you use this action to setup a replica set, please create your users through a separate script. From 577b12f701c14b9b7ac31edf9b8353fc1aa45ca8 Mon Sep 17 00:00:00 2001 From: Alexander Lukens Date: Fri, 5 Apr 2024 12:09:30 -0400 Subject: [PATCH 10/10] consistency in formatting with README example for custom container name --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 768f468..dc81a19 100644 --- a/README.md +++ b/README.md @@ -203,9 +203,9 @@ jobs: ``` ### With Custom Container Name -The container name of the created MongoDB instance can be configured using the "container-name" input +The container name of the created MongoDB instance can be configured using the `container-name` input -The following example will parameterize the MongoDB container name based on the node and mongodb versions being used: +The following example will parameterize the MongoDB container name based on the `node-version` and `mongodb-version` being used from the matrix: ```yaml name: Run with Custom Container Names