Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user-defined container name #58

Merged
merged 11 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-version` and `mongodb-version` being used from the matrix:

```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/[email protected]
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.


Expand Down
2 changes: 2 additions & 0 deletions action-types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ inputs:
type: string
mongodb-password:
type: string
container-name:
type: string
8 changes: 7 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -45,4 +50,5 @@ runs:
- ${{ inputs.mongodb-port }}
- ${{ inputs.mongodb-db }}
- ${{ inputs.mongodb-username }}
- ${{ inputs.mongodb-password }}
- ${{ inputs.mongodb-password }}
- ${{ inputs.container-name }}
22 changes: 15 additions & 7 deletions start-mongodb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ MONGODB_PORT=$3
MONGODB_DB=$4
MONGODB_USERNAME=$5
MONGODB_PASSWORD=$6
MONGODB_CONTAINER_NAME=$7

# `mongosh` is used starting from MongoDB 5.x
MONGODB_CLIENT="mongosh --quiet"
Expand Down Expand Up @@ -50,30 +51,36 @@ 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
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
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"
echo " - port [$MONGODB_PORT]"
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 --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"
Expand All @@ -93,7 +100,8 @@ 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

if [ $? -ne 0 ]; then
echo "Error starting MongoDB Docker container"
Expand All @@ -105,7 +113,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\": [ {
Expand All @@ -120,5 +128,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::"
Loading